// Removes leading whitespaces
function LTrim(value) {
	
	//alert(value);
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	//alert(value);
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

function alphaspecial(frmObject)
{
	//alert("alpha");return false;
	frmObject.value = frmObject.value.trim(); 
	var str= frmObject.value; 
	//alert(str);
	var re = /^[A-Za-z\'\-\s]*$/; 
	if (! str.match(re)) 
	{  
		return false; 
	} 
	return true;
}

function alphanumeric(alphane)
{
	// Validation for Alpha Numeric Values which Contains Valid Characters , Numbers and Space
	
	var numaric = alphane;
	for(var j=0; j<numaric.length; j++)
		{
		  var alphaa = numaric.charAt(j);
		  var hh = alphaa.charCodeAt(0);
		  if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123) || (hh ==32))
		  {
		 	
		  }
		  else
		 {
			 return false;
		  }
		}
 return true;
}

function validateString( strValue ) {
  var strMatchPattern =  /^[a-zA-Z\'\- ]+$/;

  var objRegExp = new RegExp( strMatchPattern);
	
	//check if string matches pattern
	return objRegExp.test(strValue);
}


function rmspaces(x) 
{
var leftx = 0;
var rightx = x.length -1;
while ( x.charAt(leftx) == ' ') leftx++;
while ( x.charAt(rightx) == ' ') --rightx;
var q = x.substr(leftx,rightx-leftx + 1); 
if ( (leftx == x.length) && (rightx == -1) ) q ='';
return(q)
};
function isnumeric(data)
 {
 var flag = ( (rmspaces(data)).length > 0);
 var isdot = 0;
  if (flag)
  {
       	for (i=0;i<data.length && flag;i++)
 		if ( data.charAt(i) > '9' || data.charAt(i) < '0')
 		   {
 		     if (data.charAt(i) != '.' || isdot > 0)
 		        {
 		          flag = false;
			}
		       else
		        {
			   isdot = 1;
			}
 		   };
  }
 return flag
 }

function checkfield(data)
{
if (rmspaces(data) == "") return false
else return true;
};

function isemail(data)
{
var flag = false;
if (  (data.indexOf('@',0)  == -1) || (data.indexOf('\\',0)  != -1) || (data.indexOf('/',0)  != -1) ||!checkfield(data) || ( data.indexOf('.',0)  == -1 ) || ( data.indexOf('@')  == 0 ) || ( data.lastIndexOf('.') < data.lastIndexOf('@')  ) || ( data.lastIndexOf('.') == (data.length - 1)  ) ||        ( data.lastIndexOf('@')   !=   data.indexOf('@') ) || (data.indexOf(',',0)  != -1) ||  (data.indexOf(':',0)  != -1) || (data.indexOf(';',0)  != -1)  )return flag
else         {
             var temp = rmspaces(data);
             if (temp.indexOf(' ',0) != -1) flag = true;
             var d3 = temp.lastIndexOf('.') + 4;
             var d4 = temp.substring(0,d3);
             var e2 = temp.length  -  temp.lastIndexOf('.')  - 1 ;
             var i1 = temp.indexOf('@') ;
          if (  (temp.charAt(i1+1) == '.') || ( e2 > 3 )  ||  ( e2 < 2 )    ) flag = true;
          return !flag;
                   };
};
function todate(fday,fmon,fyear , tday,tmon,tyear)
{
var fromdt;
var todt;

   fm = parseInt(fmon) - 1;
   tm = parseInt(tmon) - 1;
   var fromdt = new Date(fyear,fm,fday);
   var todt = new Date(tyear,tm,tday);
 
   if (todt < fromdt)
    	    return false;
       else
    	    return true;
};
function req_eMail(e)
{
				eMail = e.value;
				if ((eMail.indexOf("@") != -1) && (eMail.indexOf(".") != -1)) 
				{
					return true;
				}
				else {
				  		return false;
					}
}


function todate(fday,fmon,fyear , tday,tmon,tyear)
{
var fromdt;
var todt;

   fm = parseInt(fmon) - 1;
   tm = parseInt(tmon) - 1;
   var fromdt = new Date(fyear,fm,fday);
   var todt = new Date(tyear,tm,tday);
 
   if (todt < fromdt)
    	    return false;
       else
    	    return true;
};



//----------------- Get a quick quote functions 

	function check(name_of_field)
	{
		//alert("check");
		if(name_of_field=="name1")
		{
			if(document.formindex.name1.value=="")
			{
				document.formindex.name1.value="name";
			}
		}
		if(name_of_field=="email")
		{
			if(document.formindex.email.value=="")
			{
				document.formindex.email.value="e-mail";
			}
		}
		if(name_of_field=="request")
		{
			if(document.formindex.request.value=="")
			{
				document.formindex.request.value="request";
			}
		}
	}

	function erase(name_of_field)
	{
		//alert(name_of_field);
		if(name_of_field=="name1")
		{
			if(document.formindex.name1.value=="name")
			{
				document.formindex.name1.value="";
			}
		}
		if(name_of_field=="email")
		{
			if(document.formindex.email.value=="e-mail")
			{
				document.formindex.email.value="";
			}
		}
		if(name_of_field=="request")
		{
			if(document.formindex.request.value=="request")
			{
				document.formindex.request.value="";
			}
		}
	}

	function isValidEmail(emailStr) 
	{
			var checkTLD=1;
			var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
		//	var emailPat=/^(.+)@(.+)$/;
			var emailPat=/^(.+)@(.+)$/;
			var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
			var validChars="\[^\\s" + specialChars + "\]";
			var quotedUser="(\"[^\"]*\")";
			var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
			var atom=validChars + '+';
			var word="(" + atom + "|" + quotedUser + ")";
			var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
			var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
			var matchArray=emailStr.match(emailPat);
			if (matchArray==null) {
				//alert("Email address seems incorrect (check @ and .'s)");
				alert("Please enter valid Email address.");
				return false;
			}
			var user=matchArray[1];
			var domain=matchArray[2];
			// Start by checking that only basic ASCII characters are in the strings (0-127).
			for (i=0; i<user.length; i++) {
				//alert(user.charCodeAt(i))
				if (user.charCodeAt(i)<65) {
					if ((user.charCodeAt(i)<45)|| (user.charCodeAt>57)) {
				//	alert("Ths Email address contains invalid characters.");
					alert("Please enter valid Email address.");
					return false;
					}
				}
				
			}
			for (i=0; i<domain.length; i++) {
				if (domain.charCodeAt(i)>127) {
				//	alert("Ths Email address contains invalid characters.");
					alert("Please enter valid Email address.");
					return false;
				}
			}
			if (user.match(userPat)==null) {
			//	alert("The Email address doesn't seem to be valid.");
				alert("Please enter valid Email address.");
				return false;
			}
			var IPArray=domain.match(ipDomainPat);
			if (IPArray!=null) {
				for (var i=1;i<=4;i++) {
					if (IPArray[i]>255) {
						//alert("Email address is invalid!");
						alert("Please enter valid Email address.");
						return false;
					}
				}
				return true;
			}
			var atomPat=new RegExp("^" + atom + "$");
			var domArr=domain.split(".");
			var len=domArr.length;
			for (i=0;i<len;i++) {
				if (domArr[i].search(atomPat)==-1) {
					//alert("Email address does not seem to be valid.");
					alert("Please enter valid Email address.");
					return false;
			   }	
			}
			if (checkTLD && domArr[domArr.length-1].length!=2 && 
				domArr[domArr.length-1].search(knownDomsPat)==-1) {
				//alert("The address must end in a well-known domain or two letter " + "country.");
				alert("Please enter valid Email address.");
				return false;
			}
		
		// Make sure there's a host name preceding the domain.
		
			if (len<2) {
				//alert("This Email address is missing a hostname!");
				alert("Please enter valid Email address.");
				return false;
			}	
			return true;
	}


function howManyChecked(whichForm,whichCheckBoxArray,myMax,myMin,whichQuestion)
/*
  This function takes 5 paramaters:
  whichForm -- the NAME of the form to be validated, a string
  whichCheckBoxArray -- the NAME of the checkbox to be checked, a string
  myMax -- the most you want the user to be able to check, an integer
  myMin -- the least you want the user to be able to check, an integer
  whichQuestion -- a short description of the question, a string
  
  example use:
  howManyChecked('myform','cb_industry',6,1,'Industry');
*/
{
	var _countChecked = 0;
	var err = 0;
	/* iterate through all the elements in the checkbox array */
	for(i=0;i<document[whichForm][whichCheckBoxArray].length;i++)
	{
		/* and check to see if each is checked */
		   if(document[whichForm][whichCheckBoxArray][i].checked==true)
				/* if it is, increment a counter */
				      { _countChecked++; }
				      }
				      /* is the count too high? */
				      if(_countChecked > myMax)
						       { alert('Limit '+myMax+' checks for the '+whichQuestion+' question.');
								      err = 1;}
								      /* of is the count too low */
								      else if(_countChecked < myMin)
									   { alert('You must fill out at least '+myMin+' entry(s) for the '+whichQuestion+' question.');
											err = 1;}
											if (err == 1) { return false; }
											else { return true;  }
}