function ValidateForm(){
isFn = check_empty($('#first_name'))
isLn = check_empty($('#last_name'))
isTitle = check_empty($('#title'))
isCompany = check_empty($('#company'))
isPhone = check_empty($('#phone'))
isStreet =check_empty( $('#street'))
isCity = check_empty($('#city'))
isZip = check_empty($('#zip'))
isEmail = isValidEmail($('#email'))
isValidCountry = CheckCountry();
isValidState = CheckState();
isSelectbox = checkSelect($('#00N80000003zl3X'))

function checkSelect(which){
	if(which.val()=='none'){
		$('#selectbox').addClass('error');return false;
	}
	$('#selectbox').removeClass('error');return true;
}
	if(isEmail){
		$('#email').removeClass('error')
	}
	else {
	$('#email').addClass('error')
	}
	if( isFn && isLn && isTitle && isCompany && isPhone && isStreet && isCity && isZip && isEmail && isValidCountry && isValidState && isSelectbox ){
	return true;
	}
	return false

}

function isNumeric(which){
var val=$(which).attr('value');
if($.trim(val).length < 1){$(which).addClass('error');return false}
	val=val.replace(/[0-9]*/g, "");

		if(val=="")		
		{$(which).removeClass('error');return true;}
		else{	
		$(which).addClass('error');
		return false
		}
}
function check_empty(which){
	value = $(which).attr('value')
	if($.trim(value).length > 0){
	$(which).removeClass('error')
	return true
	}
	else{$(which).addClass('error');return false}
}

function isValidEmail(which){
	//alert('email')
	str = $(which).attr('value')
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					

} 

function CheckCountry(){
	validCountry = false;
	countryText = $('#country').attr('value');
	nosCountries = countries.length
	for(loopcount = 0;loopcount<=nosCountries;loopcount++){
		if (countryText == countries[loopcount]){
			validCountry = true;
			break;
		}
	}
	if(validCountry){
		$("#country").removeClass('error');
		$("#state").removeClass('error');
	}
	else{
		$("#country").addClass('error');
		return false;
	}
	setStatesValue(countryText)
	CheckState()
	return true;
}
function CheckState(){
	var validState = false;
	var stateArray = []
	stateText = $('#state').attr('value');
	whichCountry = $('#country').val();
		switch(whichCountry){
		case 'USA':
			nosStates = usastates.length
			stateArray = usastates
			break;
		case 'Canada':
			nosStates = canadastates.length
			stateArray = canadastates
			break;
		case 'India':
			nosStates = indiastates.length
			stateArray = indiastates
			break;
		default:
			nosStates = 0
			validState = true;
			break;
	}
	nosCountries = countries.length;
	for(loopcount = 0;loopcount<=nosCountries;loopcount++){
		if (stateText == stateArray[loopcount]){
			validState = true;
			break;
		}
	}
	stateval = $("#state").attr('value')
	if($.trim(stateval)==""){
		$("#state").addClass('error');
		return false;
	}
	if(validState){
		$("#state").removeClass('error')
	}
	else{
		$("#state").addClass('error');
		return false;
	}
		return true;
}

function setStatesValue(whichCountry){
	switch(whichCountry){
		case 'USA':
			$("#state").flushCache();
			$("#state").autocomplete(usastates);
			break;
		case 'Canada':
			$("#state").flushCache();
			$("#state").autocomplete(canadastates);
			break;
		case 'India':
			$("#state").flushCache();
			$("#state").autocomplete(indiastates);
			break;
		default:
			$("#state").flushCache()
			break;
	}

}