//Constants used in the script
 var otherComment="Other";

 
function checkComment()
{
	var oTemp = document.getElementById("commentch");
	if (oTemp == null) return (0);
	var slectedComment = oTemp.options[oTemp.selectedIndex].value;
	oTemp = document.getElementById("OtherRelated");
	if (oTemp == null) return (0);
	if(slectedComment == otherComment)
	{
		oTemp.style.display = 'inline';
		oTemp.style.backgroundColor= oTemp.style.background ;
		oTemp.focus();
	}
	else
	{
		oTemp.style.display = 'none';
		oTemp.style.backgroundColor=13421772;
	}
}

function checkName(nameToCheck)
{
	if (nameToCheck.length == 0) return false;
	var pattern = "^[A-Za-zÀ-ÖØ-öø-ÿ'\-\. ]{" + nameToCheck.length + "}";
	var oRe = new RegExp(pattern);
	return (oRe.test(nameToCheck));
}

function validate()
{	
	//First name validator
	var fTemp = false;
	var oTemp = null;
	var AllTrue  = true;
	oTemp = document.getElementById("fname");
	if (oTemp != null)
	{
		var sTempName = oTemp.value;
		fTemp = checkName(sTempName);
		if(fTemp==false)
		{
			document.getElementById("lbfname").style.color = "red"
			AllTrue = false;
		}else
		{
			document.getElementById("lbfname").style.color = "black"
		}
	}
	//Last name validator
	oTemp = document.getElementById("lname");
	if (oTemp != null)
	{
		sTempName = oTemp.value;
		fTemp = checkName(sTempName);
		if(fTemp==false)
		{
			document.getElementById("lblname").style.color = "red"
			AllTrue = false;
		}else
		{
			document.getElementById("lblname").style.color = "black"
		}
	}
	//Middle name validator
	//oTemp = document.getElementById("mname");
	//if (oTemp != null)
	//{
		//sTempName = oTemp.value;
		//fTemp = checkName(sTempName);
		//if(fTemp==false)
		//{
			// document.getElementById("lbmname").style.color = "red"
			//AllTrue = false;
		//}else
		//{
		//	document.getElementById("lbmname").style.color = "black"
		//}
	//}
	
	//comment
	var oTemp = document.getElementById("comment").value;
	if (oTemp.length == 0 || oTemp.length > 3000)
	{
		document.getElementById("lbyourcom").style.color = "red"
		AllTrue = false;
	}else
	{
		document.getElementById("lbyourcom").style.color = "black"
	}
	//email
	if (checkEmail() == false)
	{
		//alert ("Inavlid email id name..Plz type again");
		document.getElementById("lbemail").style.color = "red"
		var oEmail = document.getElementById("emailid");
		AllTrue = false;
	}else
	{
		document.getElementById("lbemail").style.color = "black"
	}
	//make sure user has entered country in case of other
	var oTemp = document.getElementById("country");
	var slectedCountry = oTemp.options[oTemp.selectedIndex].value;
	if(slectedCountry == 0)
	{
		AllTrue = false;
	}
	
	//comment is related to 
	var oTemp = document.getElementById("commentch");
	if (oTemp == null){
		document.getElementById("lbothcomm").style.color = "red"
		AllTrue = false;
	}
	var slectedComment = oTemp.options[oTemp.selectedIndex].value;
		if( slectedComment == otherComment)
		{
			oTemp = document.getElementById("com");
			if (oTemp == null)
			{
				document.getElementById("lbothcomm").style.color = "red"
				AllTrue = false;
			}
		
		if (oTemp.value.length == 0 )
		{
			//other  comment
			document.getElementById("lbothcomm").style.color = "red"
			AllTrue = false;
		}else
		{
			document.getElementById("lbothcomm").style.color = "black"
		}
	}
	if (AllTrue == false) {
		document.getElementById("lblErr").style.display = "block";
	}
	return AllTrue;
 }

function checkEmail() 
{
	var oEmail = document.getElementById("emailid");
	if (oEmail == null) 
		return false;
	var sEmailValue =oEmail.value;
	if (sEmailValue.length == 0)
		return false;
	if (RegExp) {
		var sinvalidPattern = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";	//invalid email patterns
		var sValidPattern = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";		//valid email pattern
		var reg1 = new RegExp(sinvalidPattern);
		var reg2 = new RegExp(sValidPattern);
		if (!reg1.test(sEmailValue) && reg2.test(sEmailValue)) 
			return true;
		return false;
	}
	else {	//to support browsers not having the support for RegExp
		var iAtPos = sEmailValue.indexOf("@");
		var iDotPos = sEmailValue.indexOf(".");
		if(iAtPos >= 0 && iDotPos >= 0 && iAtPos < iDotPos)
			return true;
		return false;
	}
	return true;
}


//to clear all the fields

function blank()
{
	var fname = document.getElementById("fname");
	fname.value ="";
	var oTemp = document.getElementById("lname");
	oTemp.value ="";
	var oTemp = document.getElementById("mname");
	oTemp.value ="";
	var oTemp = document.getElementById("company");
	oTemp.value ="";
	var oTemp = document.getElementById("designation");
	oTemp.value ="";
	var oTemp = document.getElementById("emailid");
	oTemp.value ="";
	var oTemp = document.getElementById("add1");
	oTemp.value ="";
	var oTemp = document.getElementById("add2");
	oTemp.value ="";
	var oTemp = document.getElementById("state");
	oTemp.value ="";
	var oTemp = document.getElementById("zipcode");
	oTemp.value ="";
	var oTemp = document.getElementById("comment");
	oTemp.value="";
	var oTemp = document.getElementById("com");
	oTemp.value="";

}
