/*
	javascript development
	============================
	website 	: 	Canaves
	date 		: 	5-5-2010		
	author 		: 	mayra metaxa / developer
	company		: 	mozaik creative business solutions
	url			:	http://www.mozaik.com

*/

function windowopen_ga(url, trackerkey, target)
{	
	//category, action, opt_label, opt_value
	if (trackerkey!="")
		track_gac('', trackerkey);
	
	if (target == '_blank')
		window.open (url, "mywindow","");
	else
		window.location = url;
}

function track_ga(trackercat)
{
	//category, action, opt_label, opt_value
	_gaq.push(['_trackEvent', trackercat, 'click', tracking_page]);
}

function track_gac(trackercat, tracktag)
{
	//category, action, opt_label, opt_value
	if (trackercat!="")
		_gaq.push(['_trackEvent', trackercat, 'click', tracking_page]);
		
	if (tracktag!="")
		_gaq.push(['_trackPageview', tracktag]);
	
}

function click_clear(id) 
{
	if (document.getElementById(id).value == document.getElementById(id).defaultValue)
		document.getElementById(id).value = "";
	else if (document.getElementById(id).value == "")
		document.getElementById(id).value = document.getElementById(id).defaultValue;
}


/* ========= FORM functions ========= */


function show_error(id)
{
	document.getElementById(id).style.background="#EFE7E0";
}


function clear_errors(formid)
{
	var form = document.getElementById(formid);
	
	for (i=0;i<form.elements.length;i++)
	{
		if ( (form.elements[i].type != "button") && (form.elements[i].type != "reset") )
			form.elements[i].style.background="#fff";
	}
}


function checkEmail(elem)
{
   var field = document.getElementById(elem);
   var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
   if (field.value.length ==0)
		return false;

   if(field.value.match(emailExp))
		return true;
   else 
		return false;
   
}


function check_form_prefilled(formid, mandatory_fields, prefilled_fields, numericfields, emailid)
{
	clear_errors(formid);		
		
	//check mandatory fields
	var man = mandatory_fields.split(",");
	for (i=0;i<man.length;i++)
	{	
		if ( (document.getElementById(man[i]).type) && (document.getElementById(man[i]).type == "select-one") )
		{
			if (document.getElementById(man[i]).selectedIndex == 0)
			{
				show_error(man[i]);
				alert ("You must fill all the mandatory fields (*)");
				return false;
			}
		}
		else
		{
			if (document.getElementById(man[i]).value == "")
			{
				show_error(man[i]);
				alert ("You must fill all the mandatory fields (*)");
				return false;
			}
		}
	}		

	//prefilled
	if (prefilled_fields!="")
		var pref_f = prefilled_fields.split(",");
		
	//check prefilled:
	if (prefilled_fields!="")
	{
		for (i=0;i<pref_f.length;i++)
		{
			if (document.getElementById(pref_f[i]).value == document.getElementById(pref_f[i]).defaultValue)			
			{
				show_error(pref_f[i]);
				alert ("You must fill all the mandatory fields (*)");
				return false;
			}
		}
	}
	
	if (emailid!="")
	{
		if (!checkEmail(emailid))
		{
			show_error(emailid);
			alert ("Your email address is not valid.");
			return false;
		}
	}
	
	//check numeric fields
	if (numericfields != "")
	{
		var numeric = numericfields.split(",");
		for (i=0;i<numeric.length;i++)
		{
			document.getElementById(numeric[i]).value = document.getElementById(numeric[i]).value.replace(/,/,".");
			
			if (isNaN (document.getElementById(numeric[i]).value))
			{
				show_error(numeric[i]);
				alert (numeric[i]+": should be a number. Plase check and try again!");
				return false;
			}
		}
	}
	
	//category, action, opt_label, opt_value
	/*if (formid=='contform')
		_gaq.push(['_trackEvent', 'contact', 'submit', 'contact']);
	if (formid=='erform')
		_gaq.push(['_trackEvent', 'reservation-request', 'submit', 'email-request']);*/
	
	document.getElementById(formid).submit();
}


function check_form_prefilled_file(formid, mandatory_fields, prefilled_fields, numericfields, emailid, fileid)
{
	//prefilled_values
	
	clear_errors(formid);		
		
	//check mandatory fields
	var man = mandatory_fields.split(",");
	for (i=0;i<man.length;i++)
	{	
		if ( (document.getElementById(man[i]).type) && (document.getElementById(man[i]).type == "select-one") )
		{
			if (document.getElementById(man[i]).selectedIndex == 0)
			{
				show_error(man[i]);
				alert ("You must fill all the mandatory fields (*)");
				return false;
			}
		}
		else
		{
			if (document.getElementById(man[i]).value == "")
			{
				show_error(man[i]);
				alert ("You must fill all the mandatory fields (*)");
				return false;
			}
		}
	}		

	//prefilled
	if (prefilled_fields!="")
		var pref_f = prefilled_fields.split(",");

	
	//check prefilled:
	if (prefilled_fields!="")
	{
		for (i=0;i<pref_f.length;i++)
		{
			if (document.getElementById(pref_f[i]).value == document.getElementById(pref_f[i]).defaultValue)			
			{
				show_error(pref_f[i]);
				alert ("You must fill all the mandatory fields (*)");
				return false;
			}
		}
	}
	
	if (emailid!="")
	{
		if (!checkEmail(emailid))
		{
			show_error(emailid);
			alert ("Your email address is not valid.");
			return false;
		}
	}
	
	//check numeric fields
	if (numericfields != "")
	{
		var numeric = numericfields.split(",");
		for (i=0;i<numeric.length;i++)
		{
			document.getElementById(numeric[i]).value = document.getElementById(numeric[i]).value.replace(/,/,".");
			
			if (isNaN (document.getElementById(numeric[i]).value))
			{
				show_error(numeric[i]);
				alert (numeric[i]+": should be a number. Plase check and try again!");
				return false;
			}
		}
	}
	
	if (!file_upload(fileid))
	{
		show_error(fileid);
		return false;
	}
	
	document.getElementById(formid).submit();
}



function file_upload(file_id)	
{
	var file = document.getElementById(file_id);
	if (file.value == "")
	{
		alert("You have to attach a file!");
		return false;
	}
	
	var file1 = file.value.split(".");
	
	if (file1.length < 2)
	{
		alert("Invalid file path!");
		return false;
	}
	if ( (file1[file1.length-1] != "txt") && (file1[file1.length-1] != "doc") && (file1[file1.length-1] != "docx") && (file1[file1.length-1] != "rtf") && (file1[file1.length-1] != "pdf") && (file1[file1.length-1] != "zip") )
	{
		alert("Invalid file type! Accepted file types: .txt, .doc, .rtf, .pdf, .docx, .zip");
		return false;
	}

	return true;
		
}


function resize_gal()
{
	var img_w = 1450;
	var img_h = 900;
	
	var ratio_h = img_h/img_w;
	var ratio_w = img_w/img_h;
	
	var w = $(".slidediv_inner").width();
	var h = $(window).height();
	
	if ((h/w)<ratio_h)
	{
		$('.slidediv_inner img').width(w);
		$('.slidediv_inner img').height(ratio_h*w);
		var nw = w;
		var nh = ratio_h*w;
	}
	else
	{
		$('.slidediv_inner img').height(h);
		$('.slidediv_inner img').width(ratio_w*h);
		var nw = ratio_w*h;
		var nh = h;
	}

	var margintop = Math.abs(nh/2);
	var marginleft = Math.abs(nw/2);
	$('.slidediv_inner img').css({marginTop: "-"+margintop+"px", marginLeft: "-"+marginleft+"px"});
				
}

