// AJAX call to do an inner HTML replace (using Prototype)
function ajaxReturnResponse(where,params,id,message)
{
	if(message)
		document.getElementById(id).innerHTML = message;

	var response = new Ajax.Request(where,
    {
      method: 'post',
      parameters: params,
      onSuccess: function(transport) {
		if(id)
        	document.getElementById(id).innerHTML = transport.responseText;
      }
    });
}

function write_encoded_email()
{
	var args = write_encoded_email.arguments;
	
	var str = "";
	
	for(var i=0;i<args.length;i++)
		str += args[i];
	
	document.write("<a class='orange' href='mailto:"+str+"'>"+str+"</a>");
}

function clear_populate(default_val, object, event)
{
	switch(event)
	{
		case "clear":
			if(object.value == default_val)
				object.value = "";
			break;
		case "populate":
			if(object.value == "")
				object.value = default_val;
			break;
	}
}
//submit quiz answer
function submitAnswer(formId,qId,uid,text,type) 
{	
	if(type=='checkbox'){
		if(text.toLowerCase()=='none of the above'){
			jQuery("input[name="+formId+"]:not(.none)").attr("checked", false);
		}else{
			jQuery(".none").attr("checked", false);
		}
	}
	var url='/sites/all/modules/ggtquizlive/process.php'
	var str = jQuery('input[name='+formId+']').serialize();
	jQuery.post(url,{ans: str,qId:qId,uid:uid},
    function(data){
		jQuery('#triggeredQ'+qId).html(data);
   	});
}

jQuery(document).ready(function(){
	jQuery("#ajaxedForm").validate({
		errorPlacement: function(error, element) {
			error.appendTo( element.parent().parent().prev("div") );
		}
	})
});

// popup and blanket functions -start
function toggle(div_id) {
	var el = document.getElementById(div_id);
	if ( el.style.display == 'none' ) {	el.style.display = 'block';}
	else {el.style.display = 'none';}
}
function blanket_size(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	var myOff;
	if(window.pageYOffset){
	myOff=window.pageYOffset;
	}
	else if(document.body.scrollTop){
	myOff=document.body.scrollTop;
	}else{
	myOff=document.documentElement.scrollTop;
	}
	var blanket = document.getElementById('blanket');
	blanket.style.height = blanket_height + 'px';
	var popUpDiv = document.getElementById(popUpDivVar);
	//popUpDiv_height=blanket_height/2-250;//150 is half popup's height
	popUpDiv_height=myOff-100;
	popUpDiv.style.top = popUpDiv_height + 'px';
	
	blanket.style.width=document.body.offsetWidth+'px';
	
}
function window_pos(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	var popUpDiv = document.getElementById(popUpDivVar);
	window_width=window_width/2-400;//150 is half popup's width
	//popUpDiv.style.left = window_width + 'px';
	popUpDiv.style.left = '10px';
}

function popup(windowname) {
	blanket_size(windowname);
	window_pos(windowname);
	toggle('blanket');
	toggle(windowname);		
}
// money order popup and blanket functions - end