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

*/


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


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


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


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].type != "textarea") )
			form.elements[i].style.background="url(images/input_bg.png) repeat left top";
	}
}


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(formid, mandatory_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]).value == "")
		{
			show_error(man[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++)
		{
			var nv = $('#'+numeric[i]).val().replace(/,/,".");
			$('#'+numeric[i]).val(nv);
			
			if (isNaN ($('#'+numeric[i]).val()))
			{
				show_error(numeric[i]);
				alert (numeric[i]+": should be a number. Plase check and try again!");
				return false;
			}
		}
	}
	
	if (document.getElementById('flag_track'))
		track_ga(document.getElementById('flag_track').value, '');

	document.getElementById(formid).submit();
}

