/*
 * Revaa - Javascript simplified
 *
 */

//trim lefthand spaces
var allowsubmit=false;
function LTrim (s){	
	while(''+s.charAt(0)==' '){
		s = s.substring(1,s.length);
	}
	return s;
}

//trim righthand spaces
function RTrim (s){
	if (s.length>1){
		while(''+s.charAt(s.length-1)==' '){
			s= s.substring(0,s.length-1);
		}
		return s;
	}
}

//clear the field if undefined
function clear(item){
	if (item.value=="undefined"){
		item.value="";
	}
	return item;
}

//trim the field
function trim (s){
	s.value=LTrim(s.value);	
	s.value=RTrim(s.value);
	s=clear(s);
	return s;
}

//function to check mail format	
function checkEmail(s){
	var i;
	var found1 = false;
	var found2 = false;
	var count1 = 0;	

	for (i = 0; i < s.value.length; i++){   
      	  // Check that current character isn't whitespace.
	      if(s.value.charAt(i)== ' ') {found1=true;found2=true;count1=2;}
		  if(s.value.charAt(i)== '@') {found1=true;count1++;}
      	  if(s.value.charAt(i)== '.'){ found2=true; }
	   }
	if(found1==true && found2==true)
		{	
			if(count1 > 1)
				{
				alert ("Please Enter a Valid Email Address");
				return true;
				}
			else
				return false;
		}
		else
			{
			alert ("Please Enter a Valid Email Address");
			return true;
			}
		return false;
}

//checks for mandatory condition
function isMandatory(field, message){
	if(trim(field).value==""){			
		alert("Please Enter "+message);
		field.value="";
		field.focus();
		return true;		
	}	
	return false;
}

function isAdditionalCheck(field)
{
	if (field.getAttribute("isAdditonalCheck"))
	{
		var tmpStr = field.getAttribute("isAdditonalCheck");
		if(tmpStr != ""){
			return performCheck(field, tmpStr);
		}
	}
	return false;	
}

function performCheck(field, fieldvalue)
{
	switch (fieldvalue)
	{
		case "email":
			return checkEmail(field);
			break;
		default:
// we will add more cases here like num check
	}

}

function validateForm(x){		
for (i=0;i<x.elements.length;i++ )
	{
		if (x[i].getAttribute("reqfield")=="true")
		{
			if(isMandatory(x[i], x[i].name)){
				return false;
			}
			if(isAdditionalCheck(x[i]))
			{
				return false;
			}

		}
	}
	allowsubmit=true;
//x.submit();
}	

//used to move the cursor between the 3 phone fields, and to allow ONLY mumeric values
function checkPhone(field, altField, lastField){
	if((window.event.keyCode > 57) || (window.event.keyCode < 48)){
		return false;
	}	
	if(!lastField && field.value.length==3){
		altField.focus();
	}
}

//function to check mail format	
function checkEmail2(s){
	var i;
	var found1 = false;
	var found2 = false;
	var count1 = 0;	
	for (i = 0; i < s.value.length; i++){   
      	  // Check that current character isn't whitespace.
	      if(s.value.charAt(i)== ' ') {found1=true;found2=true;count1=2;}
		  if(s.value.charAt(i)== '@') {found1=true;count1++;}
      	  if(s.value.charAt(i)== '.'){ found2=true; }
	   }
	if(found1==true && found2==true)
		{
		if(count1 > 1)
			{
			alert ("Please Enter a Valid Email Address");
			return false;
			}
		}
		else
		{
		alert ("Please Enter a Valid Email Address");
		return false;
		}
return true;
}

function frmsubmit(x)
{

var frmObj = document.getElementById(x);
emailObj = document.getElementById("txtemail");
if (checkEmail2(emailObj))
	{
		frmObj.submit();
	}
}


function submitfrm(frmname)
{
	frmobj = document.getElementById(frmname);
	validateForm(frmobj);
	if (allowsubmit==true)
	{
			frmobj.submit();
	}
}