var willSubmit = false;
var eblank = false;

function trim(str) {
	return str.replace(/^\s+|\s+$/g, '');
}

function echeck(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	var msg = "";
	
	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 validateConsumer(obj) {
	var bmsg = "";

	if(trim(obj.ConsumerFirstName.value) == "") {
		bmsg=bmsg + "\n* Consumers First name cannot be blank.";
		willSubmit = false;
	}
	if(trim(obj.ConsumerLastName.value) == "") {
		bmsg=bmsg + "\n* Consumers Last name cannot be blank.";
		willSubmit = false;
	}
	if(trim(obj.ConsumerAddress.value) == "") {
		bmsg=bmsg + "\n* Consumers Address cannot be blank.";
		willSubmit = false;
	}
	if(trim(obj.ConsumerCity.value) == "") {
		bmsg=bmsg + "\n* Consumers City cannot be blank.";
		willSubmit = false;
	}
	/*
	if(trim(obj.ConsumerZipCode.value) == "") {
		bmsg=bmsg + "\n* Consumers Zip code cannot be blank.";
		willSubmit = false;
	}
	*/
	if(trim(obj.ConsumerPhone.value) == "") {
		bmsg=bmsg + "\n* Consumers Phone cannot be blank.";
		willSubmit = false;
	}
	if(trim(obj.ConsumerEmail1.value) == "") {
		bmsg=bmsg + "\n* Consumers Email cannot be blank.";
		willSubmit = false;
		eblank = true;
	}
	if(trim(obj.ConsumerEmail2.value) == "") {
		bmsg=bmsg + "\n* Consumers Confirmation email cannot be blank.";
		willSubmit = false;
		eblank = true;
	}
	if( trim(obj.ConsumerEmail1.value).toLowerCase() != trim(obj.ConsumerEmail2.value).toLowerCase() ) {
		bmsg=bmsg + "\n* Consumers Emails do not match.";
		willSubmit = false;
		eblank = true;
	}
	else {
		if (echeck(obj.ConsumerEmail1.value)==false){
			bmsg=bmsg + "\n* Invalid Consumers E-mail address";
			willSubmit = false;
		}
	}
	
	
	if (bmsg != "")	{
		bmsg = "Please fix the following fields:\n" + bmsg;
		alert(bmsg); 
		willSubmit = false;
	}
	
	
	if(willSubmit) {
		obj.submit();
	}
	else {
		return false;
	}
}



function validateProf(obj) {
	var bmsg = "";

	if(trim(obj.ProfFirstName.value) == "") {
		bmsg=bmsg + "\n* Professionals First name cannot be blank.";
		willSubmit = false;
	}
	if(trim(obj.ProfLastName.value) == "") {
		bmsg=bmsg + "\n* Professionals Last name cannot be blank.";
		willSubmit = false;
	}
	if(trim(obj.ProfCompany.value) == "") {
		bmsg=bmsg + "\n* Professionals Company cannot be blank.";
		willSubmit = false;
	}
	if(trim(obj.ProfAddress.value) == "") {
		bmsg=bmsg + "\n* Professionals Address cannot be blank.";
		willSubmit = false;
	}
	if(trim(obj.ProfCity.value) == "") {
		bmsg=bmsg + "\n* Professionals City cannot be blank.";
		willSubmit = false;
	}
	/*
	if(trim(obj.ProfZipCode.value) == "") {
		bmsg=bmsg + "\n* Professionals Zip code cannot be blank.";
		willSubmit = false;
	}
	*/
	if(trim(obj.ProfPhone.value) == "") {
		bmsg=bmsg + "\n* Professionals Phone cannot be blank.";
		willSubmit = false;
	}
	if(trim(obj.ProfEmail1.value) == "") {
		bmsg=bmsg + "\n* Professionals Email cannot be blank.";
		willSubmit = false;
		eblank = true;
	}
	if(trim(obj.ProfEmail2.value) == "") {
		bmsg=bmsg + "\n* Professionals Confirmation email cannot be blank.";
		willSubmit = false;
		eblank = true;
	}
	if( trim(obj.ProfEmail1.value).toLowerCase() != trim(obj.ProfEmail2.value).toLowerCase() ) {
		bmsg=bmsg + "\n* Professionals Emails do not match.";
		willSubmit = false;
		eblank = true;
	}
	else {
		if (echeck(obj.ProfEmail1.value)==false){
			bmsg=bmsg + "\n* Invalid Professionals E-mail address";
			willSubmit = false;
		}
	}
	
	
	if (bmsg != "")	{
		bmsg = "Please fix the following fields:\n" + bmsg;
		alert(bmsg); 
		willSubmit = false;
	}
	
	
	if(willSubmit) {
		obj.submit();
	}
	else {
		return false;
	}
}


function validateBroker(obj) {
	var bmsg = "";

	if(trim(obj.ProfFirstName.value) == "") {
		bmsg=bmsg + "\n* Brokers First name cannot be blank.";
		willSubmit = false;
	}
	if(trim(obj.ProfLastName.value) == "") {
		bmsg=bmsg + "\n* Brokers Last name cannot be blank.";
		willSubmit = false;
	}
	if(trim(obj.ProfCompany.value) == "") {
		bmsg=bmsg + "\n* Brokers Company cannot be blank.";
		willSubmit = false;
	}
	if(trim(obj.ProfAddress.value) == "") {
		bmsg=bmsg + "\n* Brokers Address cannot be blank.";
		willSubmit = false;
	}
	if(trim(obj.ProfCity.value) == "") {
		bmsg=bmsg + "\n* Brokers City cannot be blank.";
		willSubmit = false;
	}
	/*
	if(trim(obj.ProfZipCode.value) == "") {
		bmsg=bmsg + "\n* Brokers Zip code cannot be blank.";
		willSubmit = false;
	}
	*/
	if(trim(obj.ProfPhone.value) == "") {
		bmsg=bmsg + "\n* Brokers Phone cannot be blank.";
		willSubmit = false;
	}
	if(trim(obj.ProfEmail1.value) == "") {
		bmsg=bmsg + "\n* Brokers Email cannot be blank.";
		willSubmit = false;
		eblank = true;
	}
	if(trim(obj.ProfEmail2.value) == "") {
		bmsg=bmsg + "\n* Brokers Confirmation email cannot be blank.";
		willSubmit = false;
		eblank = true;
	}
	if( trim(obj.ProfEmail1.value).toLowerCase() != trim(obj.ProfEmail2.value).toLowerCase() ) {
		bmsg=bmsg + "\n* Brokers Emails do not match.";
		willSubmit = false;
		eblank = true;
	}
	else {
		if (echeck(obj.ProfEmail1.value)==false){
			bmsg=bmsg + "\n* Invalid Brokers E-mail address";
			willSubmit = false;
		}
	}
	
	
	if (bmsg != "")	{
		bmsg = "Please fix the following fields:\n" + bmsg;
		alert(bmsg); 
		willSubmit = false;
	}
	
	
	if(willSubmit) {
		obj.submit();
	}
	else {
		return false;
	}
}


function validate(obj) {
	var bmsg = "";

	
	if(trim(obj.targetCity.value) == "") {
		bmsg=bmsg + "\n* City cannot be blank.";
		willSubmit = false;
	}
	/*
	if(trim(obj.targetZipCode.value) == "") {
		bmsg=bmsg + "\n* Zip code cannot be blank.";
		willSubmit = false;
	}
	*/
	if(trim(obj.FirstName.value) == "") {
		bmsg=bmsg + "\n* First name cannot be blank.";
		willSubmit = false;
	}
	if(trim(obj.LastName.value) == "") {
		bmsg=bmsg + "\n* Last name cannot be blank.";
		willSubmit = false;
	}
	if(trim(obj.Address.value) == "") {
		bmsg=bmsg + "\n* Address cannot be blank.";
		willSubmit = false;
	}
	if(trim(obj.currentCity.value) == "") {
		bmsg=bmsg + "\n* City cannot be blank.";
		willSubmit = false;
	}
	/*
	if(trim(obj.currentZipCode.value) == "") {
		bmsg=bmsg + "\n* Zip code cannot be blank.";
		willSubmit = false;
	}
	*/
	if(trim(obj.Phone.value) == "") {
		bmsg=bmsg + "\n* Phone cannot be blank.";
		willSubmit = false;
	}
	if(trim(obj.Email1.value) == "") {
		bmsg=bmsg + "\n* Email cannot be blank.";
		willSubmit = false;
		eblank = true;
	}
	if(trim(obj.Email2.value) == "") {
		bmsg=bmsg + "\n* Confirmation email cannot be blank.";
		willSubmit = false;
		eblank = true;
	}
	if( trim(obj.Email1.value).toLowerCase() != trim(obj.Email2.value).toLowerCase() ) {
		bmsg=bmsg + "\n* Emails do not match.";
		willSubmit = false;
		eblank = true;
	}
	else {
		if (echeck(obj.Email1.value)==false){
			bmsg=bmsg + "\n* Invalid E-mail address";
			willSubmit = false;
		}
	}
	if(trim(obj.CaptchaResponse.value) == "") {
		bmsg=bmsg + "\n* Validate characters from image cannot be blank.";
		willSubmit = false;
	}
	
	if (bmsg != "")	{
		bmsg = "Please fix the following fields:\n" + bmsg;
		alert(bmsg); 
		willSubmit = false;
	}
	
	
	if(willSubmit) {
		obj.submit();
	}
	else {
		return false;
	}
}