/*
	javascript development
	============================
	website 	: 	vivre cote ouest
	date 		: 	13-1-2009		
	author 		: 	mayra metaxa / developer
	company		: 	mozaik creative business solutions
	url			:	http://www.mozaik.com

*/


function make_cols_equal(numofcols)
{	
	var h;	
	var maxheight = 100;
		
	for (i=1;i<=numofcols;i++)
	{
		h = document.getElementById('coldiv_'+i).offsetHeight;
		/*h = h.replace(/px/i, "");	//take off "px"
		h = Number(h);*/

		if (h > maxheight)
			maxheight = h;
	}
	
	for (i=1;i<=numofcols;i++)
	{
		document.getElementById('coldiv_'+i).style.height = maxheight+"px";	
	}
}


/*
function click_clear(id, text)
{
	if (document.getElementById(id).value == text)
		document.getElementById(id).value = "";
}

function prefill(id, text)
{
	if (document.getElementById(id).value == "")
		document.getElementById(id).value = text;
}*/


/* ========= FORM functions ========= */

function show_error(id)
{
	document.getElementById(id).style.border="solid 1px #EC008C";
}

function clear_errors(which)
{
	for (i=0;i<document.forms[which].elements.length;i++)
	{
		if ( (document.forms[which].elements[i].type != "button") && (document.forms[which].elements[i].type != "reset") && (document.forms[which].elements[i].id!="fieldset") )
			document.forms[which].elements[i].style.border="none";
	}
}


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, whichform, mandatory_fields, numericfields, emailid, equal_fields)
{
	clear_errors(whichform);
	var ok = 1;
	
	//check mandatory fields
	var man = mandatory_fields.split(",");
	for (i=0;i<man.length;i++)
	{
		if ( (document.getElementById(man[i]).value == "") && (ok==1) )
		{
			show_error(man[i]);
			alert ("You must fill all the mandatory fields (*)");
			ok=0;
		}
	}		

	if ( (emailid!="") && (ok==1) )
	{
		if (!checkEmail(emailid))
		{
			show_error(emailid);
			alert ("Your email address is not valid.");
			ok=0;
		}
	}
	
	//check numeric fields
	if ( (numericfields != "") && (ok==1) )
	{
		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)) && (ok==1) )
			{
				show_error(numeric[i]);
				alert (numeric[i]+": should be a number. Plase check and try again!");
				ok=0;
			}
		}
	}
	
	if ( (equal_fields != "") && (ok==1) )
	{
		var eq = equal_fields.split(",");
		if (document.getElementById(eq[0]).value != document.getElementById(eq[1]).value)
		{
			show_error(eq[0]);
			show_error(eq[1]);
			alert ("Those fields must have the same content!");
			ok=0;
		}
	}
	
	if (ok==1)
		document.getElementById(formid).submit();
}



/*


function find_key_pressed(e)
{
	var numCharCode;
   
   // get event if not passed
   if (!e) var e = window.event;

   // get character code of key pressed
   if (e.keyCode) 
   		numCharCode = e.keyCode;
   else if (e.which) 
   		numCharCode = e.which;
   
   if ( numCharCode == 13 )
   		return true;
	else 
		return false;
}*/