//Added by Caldia Team on 15-05-09
function clearText(thefield) {
  if (thefield.defaultValue==thefield.value) { thefield.value = "" }
} 
function replaceText(thefield) {
  if (thefield.value=="") { thefield.value = thefield.defaultValue }
}
function trim_text(str) {
	str = str.replace(/^\s+|\s+$/g, '');
	return str;
}

function trim(str)
{
	str = str.replace(/^\s+|\s+$/g, '');
	return str;
}

function onlyAlphaSpace(str)
{
	var regexLetter = /^[a-zA-z ]*.$/;
	str = regexLetter.test(str);

	if(!str)
	{ return false; }
	else
	{ return true; }
}

function onlyAlpha(str)
{
	var regexLetter = /^[a-zA-z]*.$/;
	str = regexLetter.test(str);

	if(!str)
	{ return false; }
	else
	{ return true; }
}

function onlyNum(str)
{
	var regexLetter = /^[0-9]*.$/;
	str = regexLetter.test(str);

	if(!str)
	{ return false; }
	else
	{ return true; }
}

function phonePattern(str)
{
	var regexLetter = /(\d{3}-\d{3}-\d{4})/;
	str = regexLetter.test(str);

	if(!str)
	{ return false; }
	else
	{ return true; }
}
function checkEmail(str){	
    var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_-])+\.([a-zA-Z])+([a-zA-Z])+/;
    if(pattern.test(str))
	{ return true;  }
	else
	{ return false; }
}

function isTel(val1,val2,val3,valName) {
	inv=0;
	v=val1.value+val2.value+val3.value;
	if (v!="") {
		if (v.length<10)
			inv=1;
		for (var i=0;i<v.length && inv==0;i++) {
			if ( v.charAt(i)<"0" || v.charAt(i)>"9")
				inv=1;
		}
		if (inv==1) {
			//alert (valName + " is invalid")
			alert("Please enter "+ valName + " in the format 999-999-9999");
			val1.focus();
			val1.select();
			return false;
		}
	}
	return true;
}

function checkLen(x,y)
{
	if (y.length==x.maxLength)
		{
			if(x.name=="telephone1")
				document.contactform.telephone2.focus();
				if(x.name=="telephone2")
				document.contactform.telephone3.focus();
		}
}
function checkAlphaChar(strval) {
	var retVal = true;
	var strval = strval;
	myRegExp = new RegExp("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$", "i"); 
	if(myRegExp.test(strval)) {
		retVal = false;
	} else {
		myRegExp = new RegExp("^[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$", "i"); 
		if(myRegExp.test(strval)) {
			retVal = false;
		}
	}
	/*Here checking the field contains only alpha characters*/
   if (!strval) return false;
   var iChars = "!*|,\":<>[]{}.`;()@&$#%^+-_=?/'%~'\\1234567890";

   for (var i = 0; i < strval.length; i++) {
	  if (iChars.indexOf(strval.charAt(i)) != -1)
		 return false;
   }
	/*Checking the value is blank*/
	var len = strval.length ;
	var foundBlank = 1;
	while(foundBlank == 1 && len > 0) 
	{
		var indx = strval.indexOf(" ");
		if(indx == -1) 
			foundBlank = 0 ;
		else
			strval = strval.substring(0,indx) + strval.substring(indx+1,len);
		len = strval.length;
	}
	retVal = len;
	return retVal;
}

function checkemail(email){
	var re = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
	if(trim_text(email) !="") {
		if (!re.test(email))
		{
			return false;
		}
	}	
	return true;
}

function checkBlanks(entry)
{
	var len = entry.length ;
	var foundBlank = 1;
	while(foundBlank == 1 && len > 0) 
	{
		var indx = entry.indexOf(" ");
		if(indx == -1) 
			foundBlank = 0 ;
		else
			entry = entry.substring(0,indx) + entry.substring(indx+1,len);
		len = entry.length;
	}
	return entry;
}

//Function using to enter numbers only

function numbersonly(e, decimal) {
	var key;
	var keychar;
  
	 if (window.event) {
		key = window.event.keyCode;
	  }
	  else if (e) {
		 key = e.which;
	  }
	  else {
		 return true;
	  }
	  
	  keychar = String.fromCharCode(key);
  
	  if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) ) {
		 return true;
	  }
	  else if ((("0123456789").indexOf(keychar) > -1)) {
		 return true;
	  }
	  else if (decimal && (keychar == ".")) { 
		return true;
	  }
	  else
		 return false;
}


function redirect()
{

	var fname = document.contactform.fname.value;
	var email = document.contactform.email.value;
	var telephone1 = document.contactform.telephone1.value;
	var telephone2 = document.contactform.telephone2.value;
	var telephone3 = document.contactform.telephone3.value;

	if(!checkAlphaChar(fname)) {
		alert("Please enter valid name");
		document.contactform.fname.focus();
		return false;
	}
	
	if(!checkemail(email) || !checkBlanks(email)) {
		alert("Please enter valid email");
		document.contactform.email.focus();
		return false;
	}

	//if(frm.telephone1.value!="" || frm.telephone1.value!="" || frm.telephone1.value!="")
	/*{
		if (trim(frm.telephone1.value)=='' || frm.telephone1.value=='Phone' )
		{
			alert ('Area Code cannot be blank !!');
			frm.telephone1.value='';
			frm.telephone1.focus(); 
			return false;
		}
		if (trim(frm.telephone2.value)=='')
		{
			alert ('Phone cannot be blank !!');
			frm.telephone2.focus(); 
			return false;
		}
		if (trim(frm.telephone3.value)=='')
		{
			alert ('Phone cannot be blank !!');
			frm.telephone3.focus(); 
			return false;
		}
		if (!isTel(frm.telephone1,frm.telephone2,frm.telephone3, 'Phone Number'))
		{ return false; }
	}
	*/
	return true;
}
