/*********************************************************************	
	CBA form validation
	Author: Santosh Kompalwar (santosh@therealrecruiter.com)
	http://www.therealrecruiter.com
	Date: July 31, 2008
*********************************************************************/

	var currentElement = "";
	var current_field;
	var targ;
	var e;
	var fields = new Array(6);		
	var online;
	var referrals;
	var prospecting;
	var other;
			
	function clear_textarea(field){
		if(field.value == "Think carefully about your answer, does it really set you apart from most of your competition? Just CLICK THIS FIELD to enter your answer."
			|| field.value == "Trying to do more business by just working harder is not a successful strategy. Just CLICK THIS FIELD to enter your answer."
			|| field.value == "An Exit Strategy is a way to sell your business upon retirement, most agents don't have one. Just CLICK THIS FIELD to enter your answer."
			|| field.value == "Think about this one carefully, it is what is stopping you from reaching your ideal level of success. Just CLICK THIS FIELD to enter your answer."
			|| field.value == "You would be surprised as to how many agents actually have trouble answering this one! Just CLICK THIS FIELD to enter your answer."
			|| field.value == "Don't hold back, here is your chance to think through what kind of leadership would server you best! Just CLICK THIS FIELD to enter your answer."
			|| field.value == "Just CLICK THIS FIELD to enter your answer."){
			field.value = "";
		}	
		field.style.color = "#000000";
	}
	
	function set_field(e){
		var targ = e.target;
		
		//if (targ.nodeType == 3) // defeat Safari bug
		//	targ = targ.parentNode;
		
		current_field = targ;
	}

	
	function check_number(e){	
		if (!e) var e = window.event;
			
		targ = (window.event) ? e.srcElement : e.target;
		//if (targ.nodeType == 3) // defeat Safari bug
		//	targ = targ.parentNode;	
	
		var KeyID = (window.event) ? event.keyCode : e.keyCode;
		var field = targ.value;
	
		if(isNaN(field) && KeyID != 13){		
			setTimeout("targ.focus();",1);
			alert("You must enter a valid number");
		}

		if(targ.name == "int_col21" || targ.name == "int_col22" || targ.name == "int_col23" || 
				targ.name == "int_col24" || targ.name == "int_col25" || targ.name == "int_col26"){
			
			check_one(targ);
		}

	}
	
	function check_one(targ){
		
		fields[0] = document.cba.int_col21;
		fields[1] = document.cba.int_col22;
		fields[2] = document.cba.int_col23;
		fields[3] = document.cba.int_col24;
		fields[4] = document.cba.int_col25;
		fields[5] = document.cba.int_col26;
		
		if((targ.value < 1 || targ.value > 6) && (targ.value != "")){
			alert("The only accepted values are integers with values from 1 to 6");
			setTimeout("targ.focus();",1);	
		}		
		
		for(var i =0; i < fields.length; i++){
			
			if((fields[i].value == targ.value && fields[i].name != targ.name) && (targ.value != "")){
				alert("You have set the priority of frustration on the field to be the same as another field.  Please reevaluate this priority.");
				setTimeout("targ.focus();",1);	
			}

		}
		
	}
	
	function add_percent(){
		online = document.getElementById("txtBizSource_Online").value;
		referrals = document.getElementById("txtBizSource_Referrals").value;
		prospecting = document.getElementById("txtBizSource_Prospecting").value;
		other = document.getElementById("txtBizSource_Other").value;
		
		if(online == "") online = 0;
		if(referrals == "") referrals = 0;
		if(prospecting == "") prospecting = 0;
		if(other == "")	other = 0;
		
		var total = parseInt(online) + parseInt(referrals) + parseInt(prospecting) + parseInt(other);

		if(total != 100){
			alert("The items under the question 'Where does your business come from (must add up to 100%):' must add to 100.  The current total is " + total);
			setTimeout("document.getElementById(\"int_col4\").focus();",1);
			return false;
		}
		else {
			return true;
		}

	}
	
	function update_percent(calcCase,form){
		switch(calcCase){			
			case 1:
				var current = document.getElementById("txtThingsCurrent").value;
				var goal = document.getElementById("txtThingsGoal").value;
				var diff;
				
				if(current == ""){
					current = 0;
				}
				else{
					current = parseInt(current);
				}	
				if(goal == ""){
					goal = 0;
				} 
				else {
					goal = parseInt(goal);
				}
				
				if(goal != 0 && current != 0){
					diff = 100 * ((goal - current)/goal);
				}
				else{
					diff = "";
				}
				form.txtThingsDiff.value = Math.round(diff);
				break;
			case 2:
				var current = document.getElementById("txtAnnumNetCurrent").value;
				var goal = document.getElementById("txtAnnumNetGoal").value;
				var diff;
				
				if(current == ""){
					current = 0;
				}
				else{
					current = parseInt(current);
				}	
				if(goal == ""){
					goal = 0;
				} 
				else {
					goal = parseInt(goal);
				}
				
				if(goal != 0 && current != 0){
					diff = 100 * ((goal - current)/goal);
				}
				else{
					diff = "";
				}
				form.txtAnnumNetDiff.value = Math.round(diff);
				break;	
			case 3:
				var current = document.getElementById("txtHoursCurrent").value;
				var goal = document.getElementById("txtHoursGoal").value;
				var diff;
				
				if(current == ""){
					current = 0;
				}
				else{
					current = parseInt(current);
				}	
				if(goal == ""){
					goal = 0;
				} 
				else {
					goal = parseInt(goal);
				}
				
				if(current != 0 && goal != 0){
					diff = 100 * ((current - goal)/current);
				}
				else{
					diff = "";
				}
				form.txtHoursDiff.value = Math.round(diff);
				break;	
			case 4:
				var current = document.getElementById("txtWeeksCurrent").value;
				var goal = document.getElementById("txtWeeksGoal").value;
				var diff;
				
				if(current == ""){
					current = 0;
				}
				else{
					current = parseInt(current);
				}	
				if(goal == ""){
					goal = 0;
				} 
				else {
					goal = parseInt(goal);
				}
				
				if(current != 0 && goal != 0){
					diff = 100 * ((goal - current)/goal);
				}
				else{
					diff = "";
				}
				form.txtWeeksDiff.value = Math.round(diff);
				break;								
										
		}		 
	}
	
function validCBA() 
{
	
	if(!checkBlank(document.frmCBA.txtFullTimeYrs,"Years in the real estate sales business - "))
		return false;
	if(!checkBlank(document.frmCBA.txtPrivCareer,"Your previous career "))
		return false;
	if(!checkBlank(document.frmCBA.txtAnnualIncome,"Current Annual Income - "))
		return false;	
	if(!checkBlank(document.frmCBA.txtTransactionPerYr,"Transaction sides / yr - "))
		return false;
	if(!checkBlank(document.frmCBA.txtBizSource_Online,"% of business through online sources - "))
		return false;
	if(!checkBlank(document.frmCBA.txtBizSource_Referrals,"% of business through referrals - "))
		return false;
	if(!checkBlank(document.frmCBA.txtBizSource_Prospecting,"% of business through prospecting - "))
		return false;
	if(!checkBlank(document.frmCBA.txtBizSource_Other,"% of business through other sources - "))
		return false;

	online = document.getElementById("txtBizSource_Online").value;
	referrals = document.getElementById("txtBizSource_Referrals").value;
	prospecting = document.getElementById("txtBizSource_Prospecting").value;
	other = document.getElementById("txtBizSource_Other").value;
	
	if(online == "") online = 0;
	if(referrals == "") referrals = 0;
	if(prospecting == "") prospecting = 0;
	if(other == "")	other = 0;
	
	var total = parseInt(online) + parseInt(referrals) + parseInt(prospecting) + parseInt(other);

	if(total != 100){
		alert("The items under the question 'Where does your business come from (must add up to 100%):' must add to 100.  The current total is " + total);
		setTimeout("document.getElementById(\"int_col4\").focus();",1);
		document.frmCBA.txtBizSource_Online.focus();
		return false;
	}
//	else {
//		return true;
//	}

		
//	if(!add_percent())
//		document.frmCBA.txtBizSource_Online.focus();
//		return false;
	
	if(!checkBlank(document.frmCBA.txtDesignation,"Designations you hold - "))
		return false;
	if(!checkBlank(document.frmCBA.cmbBizStrength,"What is your greatest business strength - "))
		return false;
	if(!checkBlank(document.frmCBA.cmbBizWeakness,"What is your greatest business weakness - "))
		return false;
	if(!checkBlank(document.frmCBA.txtDifferentiate,"How do you currently differentiate yourself from all your competitors - "))
		return false;
	if(!checkBlank(document.frmCBA.txtTargetMarket,"What target markets do you specialize in - "))
		return false;
	if(!checkBlank(document.frmCBA.cmbBizPlan,"Do you have a written business plan - "))
		return false;
	if(!checkBlank(document.frmCBA.txtWebsites,"How many Websites do you have - "))
		return false;
	if(!checkBlank(document.frmCBA.txtBizOverwhelm,"How do you plan on increasing your business without overwhelm - "))
		return false;
	if(!checkBlank(document.frmCBA.txtExitStrategy,"Briefly describe your “exit strategy” for when you eventually retire - "))
		return false;
	if(!checkBlank(document.frmCBA.txtThingsCurrent,"% of things you love to do (Current) - "))
		return false;
	if(!checkBlank(document.frmCBA.txtThingsGoal,"% of things you love to do (Goal) - "))
		return false;
	if(!checkBlank(document.frmCBA.txtAnnumNetCurrent,"Annual Net Income (Current) - "))
		return false;
	if(!checkBlank(document.frmCBA.txtAnnumNetGoal,"Annual Net Income (Goal) - "))
		return false;
	if(!checkBlank(document.frmCBA.txtHoursCurrent,"Hours worked/week (Current) - "))
		return false;
	if(!checkBlank(document.frmCBA.txtHoursGoal,"Hours worked/week (Goal) - "))
		return false;
	if(!checkBlank(document.frmCBA.txtWeeksCurrent,"Weeks of vacation/year (Current) - "))
		return false;
	if(!checkBlank(document.frmCBA.txtWeeksGoal,"Weeks of vacation/year (Goal) - "))
		return false;
	if(!checkBlank(document.frmCBA.txtNoTime,"priority the things that frustrate you the most in your business - "))
		return false;
	if(!checkBlank(document.frmCBA.txtNoBiz,"priority the things that frustrate you the most in your business - "))
		return false;
	if(!checkBlank(document.frmCBA.txtNoThings,"priority the things that frustrate you the most in your business - "))
		return false;
	if(!checkBlank(document.frmCBA.txtStaffDeal,"priority the things that frustrate you the most in your business - "))
		return false;
	if(!checkBlank(document.frmCBA.txtOverwhelm,"priority the things that frustrate you the most in your business - "))
		return false;
	if(!checkBlank(document.frmCBA.txtOther,"priority the things that frustrate you the most in your business - "))
		return false;
	if(!checkBlank(document.frmCBA.txtCareerStopper,"What do you feel is the number 1 thing stopping you from reaching your ideal career? - "))
		return false;
	if(!checkBlank(document.frmCBA.txtCareerJoy,"What are the things in your career give you the most pleasure or joy? - "))
		return false;
	if(!checkBlank(document.frmCBA.txtImpThing,"What is the most important thing you look for in the company you work for? - "))
		return false;
	if(!checkBlank(document.frmCBA.txtAssistant,"How many assistants are you currently using? - "))
		return false;
	if(!checkBlank(document.frmCBA.txtAssistantJob,"What do your assistants primarily help you with? - "))
		return false;
	if(!checkBlank(document.frmCBA.txtStaffBudget,"How much is your annual staffing budget? - "))
		return false;
	if(!checkBlank(document.frmCBA.cmbAsstCompetitor,"Have you ever trained an assistant who then became your competitor? - "))
		return false;
	if(!checkBlank(document.frmCBA.txtAsstNewJob,"If you were to hire another assistant, what would you have them do? - "))
		return false;
	if(!checkBlank(document.frmCBA.cmbCurrentTask_CM,"How are the [Contact Management] tasks currently handled within your business?  - "))
		return false;
	if(!checkBlank(document.frmCBA.cmbCurrentTask_WPA,"How are the [Writing Property Ads] tasks currently handled within your business? - "))
		return false;
	if(!checkBlank(document.frmCBA.cmbCurrentTask_TC,"How are the [Transaction Coordination] tasks currently handled within your business? - "))
		return false;
	if(!checkBlank(document.frmCBA.cmbCurrentTask_LM,"How are the [Listing Management] tasks currently handled within your business? - "))
		return false;
	if(!checkBlank(document.frmCBA.cmbCurrentTask_OM,"How are the [Online Marketing] tasks currently handled within your business? - "))
		return false;
	if(!checkBlank(document.frmCBA.cmbCurrentTask_CPM,"How are the [Create Print Materials] tasks currently handled within your business? - "))
		return false;
	if(!checkBlank(document.frmCBA.cmbCurrentTask_CMA,"How are the [Create CMAs] tasks currently handled within your business? - "))
		return false;
	if(!checkBlank(document.frmCBA.cmbCurrentTask_UW,"How are the [Update Website] tasks currently handled within your business? - "))
		return false;
	if(!checkBlank(document.frmCBA.cmbCurrentTask_MDE,"How are the [Manage Drip E-mail] tasks currently handled within your business? - "))
		return false;
	if(!checkBlank(document.frmCBA.cmbCurrentTask_MOL,"How are the [Manage Online Leads] tasks currently handled within your business? - "))
		return false;
	if(!checkBlank(document.frmCBA.cmbNeedControl,"How would you rate your need to be &ldquo;in control&rdquo;? - "))
		return false;
	if(!checkBlank(document.frmCBA.cmbManageStaff,"How would you rate your ability to manage staff? - "))
		return false;
	if(!checkBlank(document.frmCBA.cmbHandleDetails,"How would you rate your ability to handle details? - "))
		return false;
	if(!checkBlank(document.frmCBA.cmbCoachability,"How would you rate your coachability? - "))
		return false;
	if(!checkBlank(document.frmCBA.cmbDealChange,"How would you rate your ability to deal with change? - "))
		return false;
	if(!checkBlank(document.frmCBA.cmbDiskProfile,"Have you had a DISC profile done on you? - "))
		return false;
//	if(!checkBlank(document.frmCBA.txtYourProfile,"If so, what is your profile? - "))
//		return false;

	if(!checkBlank(document.frmCBA.txtFirstName,"First Name"))
		return false;
	if(!checkBlank(document.frmCBA.txtLastName,"Last Name"))
		return false;
	if(!checkBlank(document.frmCBA.txtEmail,"Email Address "))
		return false;
        if(document.frmCBA.txtEmail.value)  {
		if(!isEmail(document.frmCBA.txtEmail.value)){
			alert("Please enter valid Email Address");
			document.frmCBA.txtEmail.select();
			document.frmCBA.txtEmail.focus();
			return false;
		}
    }

	if(!checkBlank(document.frmCBA.txtMobile,"Mobile "))
		return false;
		
		
	if (document.frmCBA.txtMobile.value.length >0 && checkPhone(document.frmCBA.txtMobile.value)==false)
	{
		alert("Please enter only 0-9,-,+ for Mobile ")
		document.frmCBA.txtMobile.value=""
		document.frmCBA.txtMobile.focus()
		return false;
	}

	if(!checkBlank(document.frmCBA.txtCompany,"Company "))
		return false;
		
	//if(!checkBlank(document.frmCBA.txtHomeAddress,"Home Address "))
//		return false;
		
	//if(!checkBlank(document.frmCBA.txtPhone,"Phone Number"))
//		return false;
		
	if (document.frmCBA.txtPhone.value.length >0 && checkPhone(document.frmCBA.txtPhone.value)==false)
	{
		alert("Please enter only 0-9,-,+ for Phone Number")
		document.frmCBA.txtPhone.value=""
		document.frmCBA.txtPhone.focus()
		return false;
	}

	if(!checkBlank(document.frmCBA.txtCity,"City "))
		return false;
	if(!checkBlank(document.frmCBA.cmbState,"State "))
		return false;
	if(!checkBlank(document.frmCBA.txtZip,"Zip/Postal Code "))
		return false;
	if(!checkBlank(document.frmCBA.cmbCountry,"Country "))
		return false;
	if(!checkBlank(document.frmCBA.txtPrimeWebsite,"Primary Website "))
		return false;
	if(!checkBlank(document.frmCBA.cmbPreferContact,"Preferred way for us to contact you -  "))
		return false;
	if(!checkBlank(document.frmCBA.txtHowHear,"How did you hear about us? - "))
		return false;
		

	//  Text fields should not have ' . ' Will be replace by `
	replaceQuotes(document.frmCBA.txtFullTimeYrs);
	replaceQuotes(document.frmCBA.txtPrivCareer);
	replaceQuotes(document.frmCBA.txtAnnualIncome);
	replaceQuotes(document.frmCBA.txtTransactionPerYr);
	replaceQuotes(document.frmCBA.txtBizSource_Online);
	replaceQuotes(document.frmCBA.txtBizSource_Referrals);
	replaceQuotes(document.frmCBA.txtBizSource_Prospecting);
	replaceQuotes(document.frmCBA.txtBizSource_Other);
	replaceQuotes(document.frmCBA.txtDesignation);
	replaceQuotes(document.frmCBA.cmbBizStrength);
	replaceQuotes(document.frmCBA.cmbBizWeakness);
	replaceQuotes(document.frmCBA.txtDifferentiate);
	replaceQuotes(document.frmCBA.txtTargetMarket);
	replaceQuotes(document.frmCBA.cmbBizPlan);
	replaceQuotes(document.frmCBA.txtWebsites);
	replaceQuotes(document.frmCBA.txtBizOverwhelm);
	replaceQuotes(document.frmCBA.txtExitStrategy);
	replaceQuotes(document.frmCBA.txtThingsCurrent);
	replaceQuotes(document.frmCBA.txtThingsGoal);
	replaceQuotes(document.frmCBA.txtAnnumNetCurrent);
	replaceQuotes(document.frmCBA.txtAnnumNetGoal);
	replaceQuotes(document.frmCBA.txtHoursCurrent);
	replaceQuotes(document.frmCBA.txtHoursGoal);
	replaceQuotes(document.frmCBA.txtWeeksCurrent);
	replaceQuotes(document.frmCBA.txtWeeksGoal);
	replaceQuotes(document.frmCBA.txtNoTime);
	replaceQuotes(document.frmCBA.txtNoBiz);
	replaceQuotes(document.frmCBA.txtNoThings);
	replaceQuotes(document.frmCBA.txtStaffDeal);
	replaceQuotes(document.frmCBA.txtOverwhelm);
	replaceQuotes(document.frmCBA.txtOther);
	replaceQuotes(document.frmCBA.txtCareerStopper);
	replaceQuotes(document.frmCBA.txtCareerJoy);
	replaceQuotes(document.frmCBA.txtImpThing);
	replaceQuotes(document.frmCBA.txtAssistant);
	replaceQuotes(document.frmCBA.txtAssistantJob);
	replaceQuotes(document.frmCBA.txtStaffBudget);
	replaceQuotes(document.frmCBA.cmbAsstCompetitor);
	replaceQuotes(document.frmCBA.txtAsstNewJob);
	replaceQuotes(document.frmCBA.cmbCurrentTask_CM);
	replaceQuotes(document.frmCBA.cmbCurrentTask_WPA);
	replaceQuotes(document.frmCBA.cmbCurrentTask_TC);
	replaceQuotes(document.frmCBA.cmbCurrentTask_LM);
	replaceQuotes(document.frmCBA.cmbCurrentTask_OM);
	replaceQuotes(document.frmCBA.cmbCurrentTask_CPM);
	replaceQuotes(document.frmCBA.cmbCurrentTask_CMA);
	replaceQuotes(document.frmCBA.cmbCurrentTask_UW);
	replaceQuotes(document.frmCBA.cmbCurrentTask_MDE);
	replaceQuotes(document.frmCBA.cmbCurrentTask_MOL);
	replaceQuotes(document.frmCBA.cmbNeedControl);
	replaceQuotes(document.frmCBA.cmbManageStaff);
	replaceQuotes(document.frmCBA.cmbHandleDetails);
	replaceQuotes(document.frmCBA.cmbCoachability);
	replaceQuotes(document.frmCBA.cmbDealChange);
	replaceQuotes(document.frmCBA.cmbDiskProfile);
	replaceQuotes(document.frmCBA.txtYourProfile);
	replaceQuotes(document.frmCBA.txtFirstName);
	replaceQuotes(document.frmCBA.txtLastName);
	replaceQuotes(document.frmCBA.txtEmail);
	replaceQuotes(document.frmCBA.txtMobile);
	replaceQuotes(document.frmCBA.txtCompany);
	replaceQuotes(document.frmCBA.txtHomeAddress);
	replaceQuotes(document.frmCBA.txtPhone);
	replaceQuotes(document.frmCBA.txtCity);
	replaceQuotes(document.frmCBA.cmbState);
	replaceQuotes(document.frmCBA.txtZip);
	replaceQuotes(document.frmCBA.cmbCountry);
	replaceQuotes(document.frmCBA.txtPrimeWebsite);
	replaceQuotes(document.frmCBA.cmbPreferContact);
	replaceQuotes(document.frmCBA.txtHowHear);

	
	document.frmCBA.action = "cba_program.php?mode=2";
	document.frmCBA.method = "post";
	document.frmCBA.submit(); 
	//return true;

}

function processDiff1(pValue){	
	document.getElementById('txtThingsDiff').value = Math.round(100 * (document.frmCBA.txtThingsGoal.value - document.frmCBA.txtThingsCurrent.value) / document.frmCBA.txtThingsGoal.value);
}

function processDiff2(pValue){
	document.getElementById('txtAnnumNetDiff').value = Math.round(100 * (document.frmCBA.txtAnnumNetGoal.value - document.frmCBA.txtAnnumNetCurrent.value) / document.frmCBA.txtAnnumNetGoal.value);
}

function processDiff3(pValue){
	document.getElementById('txtHoursDiff').value = Math.round(100 * (document.frmCBA.txtHoursCurrent.value - document.frmCBA.txtHoursGoal.value) / document.frmCBA.txtHoursGoal.value);
}

function processDiff4(pValue){
	document.getElementById('txtWeeksDiff').value = Math.round(100 * (document.frmCBA.txtWeeksCurrent.value - document.frmCBA.txtWeeksGoal.value) / document.frmCBA.txtWeeksGoal.value);
}
