//valitation
function validate()
{
//alert("hi")

//TITLE
		
	 if ( document.frm_contact.lstTitle.value =="")
		{
			alert ( "Title is a required field!" );        
			document.frm_contact.lstTitle.focus();
			return false;
		}

// NAME
	var l=document.frm_contact.txtName.value.length;
	var m=document.frm_contact.txtName.value;
	var p=document.frm_contact.txtName;
	for(var i=0; i<l-1; i++)
		{
		n=m.substring(i, i+2);
		if(n=="  " || n==".." || n=="''")
			{
			alert("Invalid Name, Please re-enter")
			p.select();
			return false;
			}
		}
	if(document.frm_contact.txtName.value.charAt(0)==' ') 
	{
		alert("Name is a required field!");
		p.select();
		return false;
	}  
		
	if(m=='') {
		alert("Name is a required field!");
		p.select();
		return false;
	}       
	
	for (var i=0; i<l; i++)
		{
		var q=m.substring(i, i + 1);
		if (((q < "a" || "z" < q) && (q < "A" || "Z" < q)) && q != ' ' && q!='.' && q!="'") {
			alert("\nThe Name field only accepts letters & spaces.\n\nPlease re-enter your Name.");
			p.select();
			return false;
		}
	}


	//EMAIL
	var m=document.frm_contact.txtEmail;
	var n=document.frm_contact.txtEmail.value;
	if ((n==null)||(n=="")){
		alert("Email is a required field!")
		m.select()
		return false
	}
	if (echeck(n)==false){
		alert('Please enter a valid email');
		m.select();
		return false;
	}
	function echeck(str) {
	at = str.indexOf("@");
	dot = str.lastIndexOf(".");
	lengt = str.length;
	con1 = str.substring(0,at);
	con2 = str.substring(at+1,dot);
	con3 = str.substring(dot+1,lengt);

	if(con1=='' || con2=='' || con3=='') return false;

	if(str.indexOf("  ") > -1 || str.indexOf("..") > -1 || str.indexOf("__") > -1 || str.indexOf("--") > -1) return false;
	
	if(at==-1 || dot==-1) return false;

	x = con1.substring(0,1);
	if ((x < "a" || "z" < x) && (x < "A" || "Z" < x) && isNaN(x)) return false;

	x = con1.substring((con1.length)-1,(con1.length));
	if ((x < "a" || "z" < x) && (x < "A" || "Z" < x) && isNaN(x)) return false;

	x = con1.substring(1,(con1.length)-1);
	for(i=0, y=0; i<con1.length-2; i++, y=x.substring(i, i+1)) if ((y < "a" || "z" < y) && (y < "A" || "Z" < y) && isNaN(y) && y!='.' && y!='_' && y!='-') return false;
	
	x = con2.substring(0,1);
	if ((x < "a" || "z" < x) && (x < "A" || "Z" < x) && isNaN(x)) return false;

	x = con2.substring((con2.length)-1,(con2.length));
	if ((x < "a" || "z" < x) && (x < "A" || "Z" < x) && isNaN(x)) return false;
	
	x = con2.substring(1,(con2.length)-1);
	for(i=0, y=0; i<con2.length-2; i++, y=x.substring(i, i+1)) if ((y < "a" || "z" < y) && (y < "A" || "Z" < y) && isNaN(y) && y!='.' && y!='_' && y!='-') return false;		
	
	for(i=0, x=0; i<con3.length; i++, x = con3.substring(i, i+1)) if ((x < "a" || "z" < x) && (x < "A" || "Z" < x)) return false;
	if ((con3.length)<2 || (con3.length)>4)  return false;

	for(i=0;i<str.length;i++)
	{
	var c = str.charAt(i);
	if (c == ' ') return false;
	}
	
}
  

  //MOBILE
	var m1=document.frm_contact.txtMobile.value;
	var m4=document.frm_contact.txtMobile.value.length;
	var p1=document.frm_contact.txtMobile;
	
	
	if(m1=='') {
		alert("Mobile is a required field!");
		p1.select();
		return false;
	}
	
	if(document.frm_contact.txtMobile.value.charAt(0)==' ') 
	{
		alert("Mobile is a required field!");
		p1.select();
		return false;
	}  
	
	if((m1!='') && isNaN(m1.substr(0,5)))
	{
		alert("Only numbers are allowed in Mobile");
		p1.select();
		return false;
	}

	//CITY
		
	 if ( document.frm_contact.lstCity.value =="" )
		{
			alert ( "City is a required field!" );        
			document.frm_contact.lstCity.focus();
			return false;
		}
//Age
		
	 if ( document.frm_contact.lstAge.value =="" )
		{
			alert ( "Age Group is a required field!" );        
			document.frm_contact.lstAge.focus();
			return false;
		}
//car Owned
		
		var p2=document.frm_contact.txtCarOwned;
		if(document.frm_contact.txtCarOwned.value.length==0) 
	{
		alert("Car owned field cannot be left blank!");
		p2.select();
		return false;
	}  

//Mode
		
	/* if ( document.frm_contact.selMode.value =="" )
		{
			alert ( "Mode of communication is a required field!" );        
			document.frm_contact.selMode.focus();
			return false;
		}
	if (document.frm_contact.selMode.value =="Email")
		{
			document.frm_contact.selCallingtime.value=""
		}
//Timing
	
if (document.frm_contact.selMode.value =="Telephone")
{	
	 if ( document.frm_contact.selCallingtime.value =="" )
		{
			alert ( "Convenient time for calling is a required field" );        
			document.frm_contact.selCallingtime.focus();
			return false;
		}

}	*/
  
}

function validate_form()
{
 if (document.frm_contact.selMode.value =="Email")
		{
			document.frm_contact.selCallingtime.value=""
		}
}

function isKeyNav(c){
	switch(c){
		//case 10:	//CarriageReturn
		//case 13:	//LineFeed
		case 0:		//Invisible Characters
		case 8:		//Backspace
		//case 9:		//Tab
		//case 33:	//PageUp
		//case 34:	//PageDown
		//case 35:	//End
		//case 36:	//Home
		//case 45:	//Ins
		//case 46:	//Del
			return true;
	}
	return false;
}



function isKeyNumeric(c){
	if(c>=48 && c<=57){
		return true;
	}
	return false;
}

function isValidKey(fldType, evt, c) {
	keyEntered = false;
	if(c==null){
		c = getKey(evt);
		if(isKeyNav(c)){	
			setKey(evt, 0);
			return false;
		}
		if(c==34 || c==124 || c==126){   //Removed for Veyond  c==39 
	
			setKey(evt, 0);
			return false;
		}		
		keyEntered = true;
	} else {
		c = ascii(c);
	}
	validKey = false;
	switch(fldType){
		case "phoneNumber":
			validKey = isValidPhoneNumberKey(c);
			break;
		case "phoneExtn":
			validKey = isValidPhoneExtnKey(c);
			break;
	}
	if(!validKey && keyEntered){
		if(evt.preventDefault){
			evt.preventDefault();
		}
		evt.returnValue = false;	
		setKey(evt, 0);
	}
	return validKey;
}

function isValidPhoneExtnKey(c){
	if(isKeyNumeric(c)){ //Numbers
		return true;
	}
	return false;
}

function isValidPhoneNumberKey(c){
	if(isKeyNumeric(c)){ //Numbers
		return true;
	}
	return false;
}

function isValidPhoneExtn(element, blnRequired){
	isValid = true;srcElement = element;
	if(element!=null
	   && element.value!=null){
	   element.value = trim(element.value);
	   if(element.value.length>0){
	   	for(i=0;i<element.value.length;i++){
	   		if(!isValidZipCodeKey(element.value.charCodeAt(i))){
	   			isValid = false;
	   			break;
	   		}
	   	}
	   } else {
	   	if(blnRequired) {
	   		isValid = false;
	   	}
	   }
	}
	return isValid;
}

function isValidPhoneNumber(element, blnRequired){
	isValid = true;srcElement = element;
	if(element!=null
	   && element.value!=null){
	   element.value = trim(element.value);
	   if(element.value.length>0){
	   	if(element.value.length!=14){
	   		return false;
	   	}
	   	for(i=0;i<element.value.length;i++){
			switch(i){
				case 0:
					if(element.value.charCodeAt(i)!=40){
						return false;
					}
					break;
				case 4:
					if(element.value.charCodeAt(i)!=41){
						return false;
					}
					break;
				case 5: 
					if(element.value.charCodeAt(i)!=32){
						return false;
					}
					break;
				case 9:
					if(element.value.charCodeAt(i)!=45){
						return false;
					}
					break;
				default:
					if(!isValidPhoneNumberKey(element.value.charCodeAt(i))){
						return false;
					}
					break;
			}
	   	}
	   } else {
	   	if(blnRequired) {
	   		isValid = false;
	   	}
	   }
	}
	return isValid;
}



 



/**
 * DHTML date validation script
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The Date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid Month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid Day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 Digit Year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid Date")
		return false
	}
return true
}

function ValidateForm() {

	var dt2 = document.frmSearch.txtFromDate;
	if (isDate(dt.value)==false){
		dt.focus();
		return false
	}
    
	var dt2 = document.frmSearch.txtToDate;
	if (isDate(dt2.value)==false){
		dt2.focus();
		return false
	}
    return true
 }

  
  