// JavaScript Document

//***Author: JMAMMOUS***
//***Date: Feb 11 2009***
//Client side validation of pafiliapartners login in data
function showAlert(messageID, langID) {
	var alertsPath = '../includes/alerts.php?transReq=';
	var langReq = '&langID=';	
	actionMain(genericAlertFunction, alertsPath + messageID + langReq + langID);
		
}
function checkLogin(emailVal, passVal, langPref) {
	//** these alert messages will need to live in the translations table!!!
	var alertsPath = '../includes/alerts.php?transReq=';
	var langReq = '&langID=';
	var validEmail = '33';
	var validPass = '34';
		
if (emailVal.value == "") {
	//** the passVal.value at the end of the string below needs to be set to the session langID once you have sorted out
	//** the login
	actionMain(loginEmailError, alertsPath + validEmail + langReq + langPref.value);
	return false;
}
		
if (emailVal.value != "") {
	var p;
		p=emailVal.value.indexOf('@');
		if (p<1 || p==(emailVal.value.length-1)){
		//** the passVal.value at the end of the string below needs to be set to the session langID once you have sorted out
	//** the login										
		 									actionMain(loginEmailError, alertsPath + validEmail + langReq + langPref.value);
											emailVal.focus();
											return false;
						 						}
					   }
					   
if (passVal.value == "") {
	
	//** the passVal.value at the end of the string below needs to be set to the session langID once you have sorted out
	//** the login
	actionMain(loginEmailError, alertsPath + validPass + langReq + langPref.value);
	return false;
}					   
// end function checkLogin
}


function checkAgentEdit(businessName, userName, businessAddress, postCode, telNumbers, MobNumbers, faxNumbers, agentBusinessUrl, timeTracker, langPref, isPost, agentRepresentative) {
	//alert('called');
	var isAllOK=1;
	var qString = '';
	//at each step we will create a string to pass through to the AJAX object for a POST method
	//the isAllOK variable is only stored incase we want to call a page that will action a submit request, otherwise i.e. myAccount.php
	//ignore and proceed. i.e. editAgent.php
	var alertsPath = '../includes/alerts.php?transReq=';
	var langReq = '&langID=';
	var validCompany = '37';
	var validAgentEmail = '33';
	var validAgentAddress = '47';
	var validAgentPostCode = '48';
	var validAgentTel = '49';
	var validAgentTelValid = '41';
	var validAgentUrl = '52';
	var validAgentPerson = '493';
	
	if(businessName.value == "") {
		
	actionMain(genericAlertFunction, alertsPath + validCompany + langReq + langPref);
	isAllOK=0;
	businessName.focus();
	return false;
		
	} else { qString+='&businessName=' + businessName.value; }
	if(agentRepresentative.value == "") {
		
	actionMain(genericAlertFunction, alertsPath + validAgentPerson + langReq + langPref);
	isAllOK=0;
	businessName.focus();
	return false;
		
	} else { qString+='&agentRepresentative=' + agentRepresentative.value; }
	
	if(userName.value == "") {
		
	actionMain(genericAlertFunction, alertsPath + validAgentEmail + langReq + langPref);
	isAllOK=0;
	userName.focus();
	return false;
		
	}
	else { qString+='&userName=' + userName.value; }
	
	if(businessAddress.value == "") {
		
	actionMain(genericAlertFunction, alertsPath + validAgentAddress + langReq + langPref);
	isAllOK=0;
	businessAddress.focus();
	return false;
		
	}
	{ qString+='&businessAddress=' + businessAddress.value; }
	
	if(postCode.value == "") {
		
	actionMain(genericAlertFunction, alertsPath + validAgentPostCode + langReq + langPref);
	isAllOK=0;
	postCode.focus();
	return false;
		
	} else { qString+='&postCode=' + postCode.value; }
	
	//check the validity of an entry
	if(telNumbers.value != "" | telNumbers.value == "") {
	//include the numeric check js file
	var ft = IsNumeric(telNumbers.value);
	if(ft == false){
	actionMain(genericAlertFunction, alertsPath + validAgentTelValid + langReq + langPref);
	isAllOK=0;
	telNumbers.focus();
	return false;
	} else { qString+='&telNumbers=' + telNumbers.value; }
	}
	if(mobNumbers.value != "" | mobNumbers.value == "") {
	//include the numeric check js file
	var ft = IsNumeric(mobNumbers.value);
	if(ft == false){
	actionMain(genericAlertFunction, alertsPath + validAgentTelValid + langReq + langPref);
	isAllOK=0;
	mobNumbers.focus();
	return false;
	} else { qString+='&mobNumbers=' + mobNumbers.value; }
	} 
	if(faxNumbers.value != "" | faxNumbers.value == "") {
	//include the numeric check js file
	var ft = IsNumeric(faxNumbers.value);
	if(ft == false){
	actionMain(genericAlertFunction, alertsPath + validAgentTelValid + langReq + langPref);
	isAllOK=0;
	faxNumbers.focus();
	return false;
	} else { qString+='&faxNumbers=' + faxNumbers.value; }
	}
	
	
	if(agentBusinessUrl.value == "") {
	actionMain(genericAlertFunction, alertsPath + validAgentUrl + langReq + langPref);
		isAllOK=0;
		agentBusinessUrl.focus();
		return false;	
		} else {
    		var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
    		if(RegExp.test(agentBusinessUrl.value)){
        						qString+='&agentURL=' + agentBusinessUrl.value;
    							}else{
									actionMain(genericAlertFunction, alertsPath + validAgentUrl + langReq + langPref);
									isAllOK=0;
									agentBusinessUrl.focus();
        							return false;
    								}
	} 
	
		
	//append non validated parameters
	qString+='&timeTracker=' + timeTracker.value;
	 //state which function to call
	 qString+='&funcCall=3';
	if(isAllOK==1 & isPost == 1) {
		//alert(isAllOK);
		//alert(qString);
		editMyAccount("../includes/agent_Procs.php", qString);
	}

	
}


function checkPersonnelEdit(personnelContactName, personnelUserName, personneladdressbookAddress, personneladdressbookPostCode, personneladdressbookTel, langPref) {

	var alertsPath = '../includes/alerts.php?transReq=';
	var langReq = '&langID=';
	var validPersonnel = '46';
	var validPersonnelEmail = '33';
	var validPersonnelAddress = '47';
	var validPersonnelPostCode = '48';
	var validPersonnelTel = '49';
	var validPersonnelTelValid = '41';

if(personnelContactName.value == "") {
		
	actionMain(genericAlertFunction, alertsPath + validPersonnel + langReq + langPref);
	personnelContactName.focus();
	return false;
		
	}
	
	if(personnelUserName.value == "") {
		
	actionMain(genericAlertFunction, alertsPath + validPersonnelEmail + langReq + langPref);
	personnelUserName.focus();
	return false;
		
	}
	
	if(personneladdressbookAddress.value == "") {
		
	actionMain(genericAlertFunction, alertsPath + validPersonnelAddress + langReq + langPref);
	personneladdressbookAddress.focus();
	return false;
		
	}
	
	if(personneladdressbookPostCode.value == "") {
		
	actionMain(genericAlertFunction, alertsPath + validPersonnelPostCode + langReq + langPref);
	personneladdressbookPostCode.focus();
	return false;
		
	}
	
	//check the validity of an entry
	if(personneladdressbookTel.value != "") {
	//include the numeric check js file
	var ft = IsNumeric(personneladdressbookTel.value);
	if(ft == false){
	actionMain(genericAlertFunction, alertsPath + validPersonnelTelValid + langReq + langPref);
	personneladdressbookTel.focus();
	return false;
	}
	}
	
	
	
}


function checkAddAgentForm(agentContactName, agentCompanyName, agentCompanyAddress, agentZipCode, pafiliaCountries, agentTelNumber,
						   agentMobNumber, agentFaxNumber, agentWebsite, agentEmailAddress, agentEmailAddress2, pafiliaLanguages, agentStatus, langPref, agentComments, pafiliaOffices) {
	var isAllOK=1;
	
	//at each step we will create a string to pass through to the AJAX object for a POST method
	var qString = '';
	var email1 = agentEmailAddress;
	var email2 = agentEmailAddress2;
	//** these alert messages will need to live in the translations table!!!
	var alertsPath = '../includes/alerts.php?transReq=';
	var langReq = '&langID=';
	var validContactName = '50';
	var validCompany = '37';
	var validEmail = '33';
	var validAgentAddress = '47';
	var validAgentPostCode = '48';
	var validAgentTel = '49';
	var validAgentTelValid = '41';
	var validAgentCountry = '51';
	var validAgentWebAddress = '52';
	var validEmailMatch = '53';
	var validAgentLanguage = '54';
	

	if (email1.value == "") {
	actionMain(loginEmailError, alertsPath + validEmail + langReq + langPref);
		isAllOK=0;
		email1.focus();
		return false;
	} 
	if (email1.value != "") {
	var p;
		p=email1.value.indexOf('@');
		if (p<1 || p==(email1.value.length-1)){
		//** the passVal.value at the end of the string below needs to be set to the session langID once you have sorted out
	//** the login										
		 									actionMain(genericAlertFunction, alertsPath + validEmail + langReq + langPref);
											isAllOK=0;
											email1.focus();
											return false;
						 					}
					   }	
	if (email1.value != email2.value) {
					
						
		 								actionMain(genericAlertFunction, alertsPath + validEmailMatch + langReq + langPref);
										isAllOK=0;
										email1.focus();
										return false;
	} 	else {  qString='agentEmail=' + email1.value; }
	
	if(agentContactName.value == '') {
			
	actionMain(genericAlertFunction, alertsPath + validContactName + langReq + langPref);
	isAllOK=0;
	agentContactName.focus();
	
	return false;
	} else { qString+='&agentCName=' + agentContactName.value; }
	
	if(agentCompanyName.value == '') {
			
	actionMain(genericAlertFunction, alertsPath + validCompany + langReq + langPref);
	isAllOK=0;
	agentCompanyName.focus();
	return false;
	} else { qString+='&agentCompName=' + agentCompanyName.value; }
	if(agentCompanyAddress.value == '') {
			
	actionMain(genericAlertFunction, alertsPath + validAgentAddress + langReq + langPref);
	isAllOK=0;
	agentCompanyAddress.focus();
	return false;
	} else { qString+='&agentCompAddress=' + agentCompanyAddress.value; }
	if(agentZipCode.value == '') {
			
	actionMain(genericAlertFunction, alertsPath + validAgentPostCode + langReq + langPref);
	isAllOK=0;
	agentZipCode.focus();
	return false;
	} else { qString+='&agentZCode=' + agentZipCode.value; }
	if(pafiliaCountries.value == '') {
	actionMain(genericAlertFunction, alertsPath + validAgentCountry + langReq + langPref);
	isAllOK=0;
	pafiliaCountries.focus();
	return false;
	} else { qString+='&agentCountry=' + pafiliaCountries.value; }
	if(agentTelNumber.value == "") {
	actionMain(genericAlertFunction, alertsPath + validAgentTel + langReq + langPref);
	isAllOK=0;
	agentTelNumber.focus();
	return false;	
	} 
	//dont set the qString for this object just yet
	//continue to the check below.
	if(agentTelNumber.value != "") {
	//include the numeric check js file
	var ft = IsNumeric(agentTelNumber.value);
	if(ft == false){
	actionMain(genericAlertFunction, alertsPath + validAgentTelValid + langReq + langPref);
	isAllOK=0;
	agentTelNumber.focus();
	return false;
	} else { qString+='&agentTNumber=' + agentTelNumber.value; }
	} 
	//mobile and fax options can be blank, so just check for consistency if they are filled in.
	if(agentMobNumber.value != "") {
	//include the numeric check js file
	var ft = IsNumeric(agentMobNumber.value);
	if(ft == false){
	actionMain(genericAlertFunction, alertsPath + validAgentTelValid + langReq + langPref);
	isAllOK=0;
	agentMobNumber.focus();
	return false;
	} else { qString+='&agentMNumber=' + agentMobNumber.value; }
	} 
	if(agentFaxNumber.value != "") {
	//include the numeric check js file
	var ft = IsNumeric(agentFaxNumber.value);
	if(ft == false){
	actionMain(genericAlertFunction, alertsPath + validAgentTelValid + langReq + langPref);
	isAllOK=0;
	agentFaxNumber.focus();
	return false;
	} else { qString+='&agentFNumber=' + agentFaxNumber.value; }
	}
	if(agentWebsite.value == "") {
	actionMain(genericAlertFunction, alertsPath + validAgentWebAddress + langReq + langPref);
		isAllOK=0;
		agentWebsite.focus();
		return false;	
		} else {
    		var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
    		if(RegExp.test(agentWebsite.value)){
        						qString+='&agentURL=' + agentWebsite.value;
    							}else{
									actionMain(genericAlertFunction, alertsPath + validAgentWebAddress + langReq + langPref);
									isAllOK=0;
									agentWebsite.focus();
        							return false;
    								}
	} 

	
	if (pafiliaLanguages.value == "") {
			
						actionMain(genericAlertFunction, alertsPath + validAgentLanguage + langReq + langPref.value);
						isAllOK=0;
						pafiliaLanguages.focus();
						return false;
	} else { qString+='&agentLanguages=' + pafiliaLanguages.value; }
	
	//some form values are set without the need to validate so append them to the querystring
	qString+='&agentStatus=' + agentStatus.value; 
	qString+='&agentComments=' + agentComments.value;
	qString+='&agentOffice=' + pafiliaOffices.value;
	
	if(isAllOK==1) {
		//alert(isAllOK);
		//alert(qString);
		addAgentInsert("addAgent.php", qString);
	}
//end function
}

function checkAddPersonnelForm(personnelContactName, personnelCompanyName, personnelCompanyAddress, personnelZipCode, pafiliaCountries, personnelTelNumber, personnelMobNumber, personnelWebsite, personnelEmailAddress, personnelEmailAddress2, pafiliaLanguages, personnelStatus, langPref, userType) {

	var isAllOK=1;
	//at each step we will create a string to pass through to the AJAX object for a POST method
	var qString = '';
	var email1 = personnelEmailAddress;
	var email2 = personnelEmailAddress2;
	//** these alert messages will need to live in the translations table!!!
	var alertsPath = '../includes/alerts.php?transReq=';
	var langReq = '&langID=';
	var validContactName = '50';
	var validCompany = '37';
	var validEmail = '33';
	var validAgentAddress = '47';
	var validAgentPostCode = '48';
	var validAgentTel = '49';
	var validAgentTelValid = '41';
	var validAgentCountry = '51';
	var validAgentWebAddress = '52';
	var validEmailMatch = '53';
	var validAgentLanguage = '54';
	var validAgentMobile = '57';
	if(personnelCompanyName.value == ""){
	actionMain(genericAlertFunction, alertsPath + validCompany + langReq + langPref);
	isAllOK=0;
	personnelCompanyName.focus();
	
	return false;
	} else { qString+='&personnelCompanyName=' + personnelCompanyName.value; }

	if (email1.value == "") {
	actionMain(loginEmailError, alertsPath + validEmail + langReq + langPref);
		isAllOK=0;
		personnelEmailAddress.focus();
		return false;
	} 
	if (email1.value != "") {
	var p;
		p=email1.value.indexOf('@');
		if (p<1 || p==(email1.value.length-1)){
		//** the passVal.value at the end of the string below needs to be set to the session langID once you have sorted out
	//** the login										
		 									actionMain(loginEmailError, alertsPath + validEmail + langReq + langPref);
											isAllOK=0;
											personnelEmailAddress.focus();
											return false;
						 					}
					   }	
	if (email1.value != email2.value) {
					
						
		 								actionMain(loginEmailError, alertsPath + validEmailMatch + langReq + langPref);
										isAllOK=0;
										personnelEmailAddress2.focus();
										return false;
	} 	else {  qString+='&personnelEmail=' + email1.value; }
	
	
	if(personnelContactName.value == '') {
			
	actionMain(genericAlertFunction, alertsPath + validContactName + langReq + langPref);
	isAllOK=0;
	personnelContactName.focus();
	
	return false;
	} else { qString+='&personnelCName=' + personnelContactName.value; }
	
	if(personnelCompanyAddress.value == '') {
			
	actionMain(genericAlertFunction, alertsPath + validAgentAddress + langReq + langPref);
	isAllOK=0;
	personnelCompanyAddress.focus();
	
	return false;
	} else { qString+='&personnelCompanyAddress=' + personnelCompanyAddress.value; }
	
	if(personnelZipCode.value == '') {
			
	actionMain(genericAlertFunction, alertsPath + validAgentPostCode + langReq + langPref);
	isAllOK=0;
	personnelZipCode.focus();
	return false;
	} else { qString+='&personnelZCode=' + personnelZipCode.value; }
	
	if(pafiliaCountries.value == '') {
	actionMain(genericAlertFunction, alertsPath + validAgentCountry + langReq + langPref);
	isAllOK=0;
	pafiliaCountries.focus();
	return false;
	} else { qString+='&personnelCountry=' + pafiliaCountries.value; }
	
	//they can opt to leave tel and mobile numbers blank so only check if populated
	if(personnelTelNumber.value != "") {
	//include the numeric check js file
	var ft = IsNumeric(personnelTelNumber.value);
	if(ft == false){
	actionMain(genericAlertFunction, alertsPath + validAgentTelValid + langReq + langPref);
	isAllOK=0;
	personnelTelNumber.focus();
	return false;
	} else { qString+='&personnelTelNumber=' + personnelTelNumber.value; }
	}
	
	if(personnelMobNumber.value != "") {
	//include the numeric check js file
	var ft = IsNumeric(personnelMobNumber.value);
	if(ft == false){
	actionMain(genericAlertFunction, alertsPath + validAgentTelValid + langReq + langPref);
	isAllOK=0;
	personnelMobNumber.focus();
	return false;
	} else { qString+='&personnelMobNumber=' + personnelMobNumber.value; }
	} 
	
	if(personnelWebsite.value == "") {
	actionMain(genericAlertFunction, alertsPath + validAgentWebAddress + langReq + langPref);
		isAllOK=0;
		personnelWebsite.focus();
		return false;	
		} else {
    		var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
    		if(RegExp.test(personnelWebsite.value)){
        						qString+='&personnelURL=' + personnelWebsite.value;
    							}else{
									actionMain(genericAlertFunction, alertsPath + validAgentWebAddress + langReq + langPref);
									isAllOK=0;
									personnelWebsite.focus();
        							return false;
    								}
	} 
	
	if (pafiliaLanguages.value == "") {
			
						actionMain(genericAlertFunction, alertsPath + validAgentLanguage + langReq + langPref.value);
						isAllOK=0;
						pafiliaLanguages.focus();
						return false;
	} else { qString+='&personnelLanguages=' + pafiliaLanguages.value; }
	
	//some form values are set without the need to validate so append them to the querystring
	qString+='&personnelStatus=' + personnelStatus.value; 

	if(isAllOK==1) {
		//alert(isAllOK);
		//alert(qString);
		//validateAddAgent("addAgent.php", qString);
		//**add your insert code here to add personnel
		if(userType == 1) {
			//call admin inserter
		addPersonnelInsert("addPersonnel.php", qString);
		} else { 
		//call partner inserter
		addPersonnelInsert("addMyPersonnel.php", qString);
		}
	}
//end function
}


function checkEmailUniqueness(emailVal, userLang) {
genericInnerHTMLFunction('../includes/db_Procs.php?funcCall=1&tbl=1&row=agentUserName&emailValue=' + emailVal.value, userLang);
}

function checkPersonnelEmailUniqueness(emailVal, userLang, elemID) {
genericPersonnelInnerHTMLFunction('../includes/db_Procs.php?funcCall=1&tbl=2&row=personnelUserName&emailValue=' + emailVal.value, userLang, elemID);
}

function copyCompanyAddress(personnelCompanyAddress, personnelZipCode, pafiliaCountries, agentBusinessTel, personnelWebsite, langPref, findAgentText, pafiliaAgentsList) {
	var alertsPath = '../includes/alerts.php?transReq=';
	var langReq = '&langID=';
	var selectCompany = '56';
		
	if(pafiliaAgentsList.value == "") {
	actionMain(genericAlertFunction, alertsPath + selectCompany + langReq + langPref);
	findAgentText.focus();	
	return false;
	} else {
	globalResponseWriter('../includes/db_Procs.php?funcCall=2&agentIDVal=' + pafiliaAgentsList.value, 'personnelCompanyAddress',  'personnelZipCode' , 'pafiliaCountries', 'personnelTelNumber', 'personnelWebsite', 'value')
	}
	
}

function returnThisValue(formObject, onOrOff)
{
	if(onOrOff == 0) {
  	formObject.disabled = true;
	} else if (onOrOff == 1) {
	formObject.disabled = false;
	}
}

//***END JMAMMOUS FEB 11 2009***

//********************************Author:Marianne Angelides**************************************************
//***********************************Date: 10 February 2009****************************************************/

function checkFinanceProp()  {
var propCheck = true;	

if(document.getElementById("project_name").value == ""){
showAlert(257, 1);
document.getElementById("project_name").focus();
propCheck =  false;
return false;
	  
}

if(document.getElementById("property_unit").value == ""){
showAlert(258, 1);
document.getElementById("property_unit").focus();
propCheck =  false;
return false;
	  
}

if(document.getElementById("contract_price").value == "" | isNaN(document.getElementById("contract_price").value)){
showAlert(259, 1);
document.getElementById("contract_price").focus();
document.getElementById("contract_price").value = "";	
propCheck =  false;
return false;
  
}

if(document.getElementById("contract_date").value == "" | document.getElementById("contract_date").value == "click here" ){
showAlert(260, 1);
document.getElementById("contract_date").focus();
propCheck =  false;
return false;
	  
}

if(propCheck) {
	$('#contract_date').datepicker( 'disable' )
	document.getElementById('changeOfPrice').innerHTML="";
	document.getElementById('stageOne').disabled = true;
	
	for(i=0; i<document.forms[0].elements.length; i++){
		if (document.forms[0].elements[i].type != "button")
		{
		
	document.forms[0].elements[i].disabled = true;
		}
	//document.getElementById(i).disabled=true;
	}
	actionMain(proceedToClient, 'client_info.php');
}

//end function!!!!!!	
}

function checkFinanceClient() {
	var clientCheck = true;	
	if(document.getElementById("client_currency").value == ""){
	showAlert(261, 1);
	document.getElementById("client_currency").focus();
	clientCheck =  false;
	return false;
	  
}
	
	if(document.getElementById("monthly_income").value == "" | isNaN(document.getElementById("monthly_income").value)){
	showAlert(262, 1);
	document.getElementById("monthly_income").focus();
	clientCheck =  false;
	return false;
	  
}

	if(document.getElementById("other_mortgage_install").value == "" | isNaN(document.getElementById("other_mortgage_install").value)){
	showAlert(263, 1);
	document.getElementById("other_mortgage_install").focus();
	clientCheck =  false;
	return false;
  
}

	var otherMortInst = '';
	var monthlyInc = '';
	otherMortInst = Number(document.getElementById("other_mortgage_install").value);
	monthlyInc = Number(document.getElementById("monthly_income").value);


if(otherMortInst > monthlyInc)
	{
		showAlert(264, 1);
		document.getElementById("other_mortgage_install").focus();
		clientCheck =  false;
		return false;
		
	}
	
	if(document.getElementById("clients_age").value == ""){
	showAlert(265, 1);
	document.getElementById("clients_age").focus();
	clientCheck =  false;
	return false;
	  
}
	
	
if(clientCheck) {
	document.getElementById("editConfirm").disabled=true;
	document.getElementById("client_name").disabled=true;
	document.getElementById("client_properties").disabled=true;
	//document.getElementById("maximum_duration").disabled=true;
	document.getElementById("client_currency").disabled=true;
	document.getElementById("monthly_income").disabled=true;
	document.getElementById("other_mortgage_install").disabled=true;
	//document.getElementById("clients_age").disabled=true;
	actionMain(loanInfo, 'loan_details.php?devID=' + document.getElementById('project_name').value);
}	
	
	
//end function	
}

//this is the validation function for the last screen
function checkLoanScreen()  
{
	var loanCheck = true;	

	if(document.getElementById("bank_name").value == ""){
	showAlert(267, 1);
	document.getElementById("bank_name").focus();
	loanCheck =  false;
	return false;
	}

	if(document.getElementById("bank_currency").value == ""){
	showAlert(268, 1);
	document.getElementById("bank_currency").focus();
	loanCheck =  false;
	return false;
	}

	if(document.getElementById("client_contribution_perc").value == "" ){
	showAlert(269, 1);
	document.getElementById("client_contribution_perc").focus();
	loanCheck =  false;
	return false;
	}

	if(document.getElementById("loan_years").value == ""){
	showAlert(271, 1);
	document.getElementById("loan_years").focus();
	loanCheck =  false;
	return false;
	}
	
	if(document.getElementById("loan_start_date").value == ""){
	showAlert(274, 1);
	document.getElementById("loan_start_date").focus();
	loanCheck =  false;
	return false;
	}
	
	if(document.getElementById("grace_period").value == ""){
	showAlert(275, 1);
	document.getElementById("grace_period").focus();
	loanCheck =  false;
	return false;
	}
	
	if(document.getElementById("grace_prinicipal").value == ""){
	showAlert(276, 1);
	document.getElementById("grace_prinicipal").focus();
	loanCheck =  false;
	return false;
	}

	if(document.getElementById("grace_scheduled_payments").value == ""){
	showAlert(277, 1);
	document.getElementById("grace_scheduled_payments").focus();
	loanCheck =  false;
	return false;
	}

	if(loanCheck) 
	{
	loanCalculations();
	//here is where we will and send them to screen4
	}

//end function!!!!!!	
}


function resetStepOne(myForm)

{
	
	$('#contract_date').datepicker( 'enable' )
	document.getElementById("project_name").selectedIndex = 0;
	document.getElementById("showProps").innerHTML = "";
	document.getElementById("showType").innerHTML = "";
	//document.getElementById("reservation_fee").value = "";
	document.getElementById("contract_date").value = "";
	document.getElementById("loanDiv").innerHTML = "";
	document.getElementById("displayClient").innerHTML = "";
	
	
	for(i=0; i<document.forms[0].elements.length; i++){
	document.forms[0].elements[i].disabled = false;
		}
	
	
//end function	
}

function resetMe(){
	
	globalClearDiv('displayClient', proceedToClient, 'client_info.php');
	document.getElementById("loanDiv").innerHTML = "";
	
}

function resetStepThree(divName, funcName, targetVal)
{
	document.getElementById(divName).innerHTML = "";
	actionMain(funcName, targetVal);
		
//end function	
}

function globalClearDiv(divName, funcName, targetVal)

{
	document.getElementById(divName).innerHTML = "";
	actionMain(funcName, targetVal);
	
	//for(i=0; i<document.forms[0].divName.elements.length; i++){
	//document.forms[0].divName.elements[i].disabled = false;
	//}
	/*document.getElementById("client_currency").selectedIndex = 0;
	document.getElementById("client_name").value = "";
	document.getElementById("client_properties").value = "";
	document.getElementById("currency").value = "";
	document.getElementById("monthly_income").value = "";
	document.getElementById("cur").value = "";
	document.getElementById("other_mortgage_install").value = "";
	document.getElementById("clients_age").value = "";
	document.getElementById("maximum_duration").value = "";	
	*/
	
//end function	
}

function validateClientAge(cAge) {
if (document.getElementById("bank_name").value != "")
{
	
findBankCurrency('../includes/db_Procs.php?bankID=' +  document.getElementById("bank_name").value + '&procName=findBankCurrency', cAge);
}
else {
		document.getElementById("showBankCurrency").innerHTML = "";
		document.getElementById("showBankMaxAge").innerHTML = "";
		
}


//**this needs testing!!! 
//ideally we dont want to set a timeout, but placing the call to db_Procs to create the innerHTML in the same functon
//that gets the element values causes issues.
//setTimeout(function(){validateEnteredAge(cAge);}, 50);
//alternative method is make the function call to validateEnteredAge from within ajax after innerHTML written to div
//which contains the elements we want to reference
}

function validateEnteredAge(cAge){

var clientAge = '';
var minimumAge = '';
var maxAge = '';
var maxLoandDurationVal = '';

clientAge = Number(document.getElementById("clients_age").value);
minimumAge = Number(document.getElementById("minBankClientAge").value);
maxAge = Number(document.getElementById("maxBankClientAge").value);
maxLoandDurationVal = Number(document.getElementById("maxBankLoanDuration").value);

if(minimumAge > clientAge | maxAge < clientAge) {
	
	
	document.getElementById("showBankCurrency").innerHTML = "";
	document.getElementById("showBankMaxAge").innerHTML = "";
//document.getElementById("maximum_duration").innerHTML = "";
	//document.getElementById("mpi").innerHTML = "";
	document.getElementById("clients_age").disabled= false;
	setTimeout(function(){cAge.focus();}, 10);
	var bankName = getSelectedValue('bank_name');
	showAlert(266, 1);
	document.getElementById("bank_name").selectedIndex = 0;
	document.getElementById("clients_age").value = "";
	
	return false;
} else {
	//age is ok, proceed.
	//alert(document.getElementById("installment_to_income").value);
	document.getElementById("clients_age").disabled= true;
	copyIt('client_currency', 'ClientMaxInstallCurr');
	var getLoanAge = max_loan_dur(clientAge, maxAge, maxLoandDurationVal);
	document.getElementById("mld").innerHTML = getLoanAge;
	var maxInstallmentPeriod = max_poss_installment(document.getElementById("monthly_income").value, document.getElementById("other_mortgage_install").value, document.getElementById("installment_to_income").value);
	document.getElementById("maximum_instalments_client").value = maxInstallmentPeriod;
	document.getElementById("showInterestRate").innerHTML = "";
	
	
	
}
	
}

function enableElement(whichSelected, whichElement) {
if(	whichSelected != 0) {
document.getElementById(whichElement).disabled = false;	
	
} else {
	document.getElementById(whichElement).value = "";
	document.getElementById(whichElement).disabled = true;	
}
}

function fillInitalContr(propPrice, percIC, clientContr, loanVal, loanPerc, whichElement)
{
	
	//check if we have a number
	var n = IsNumeric(percIC.value);
	
	//alpha
	//ensure any contraints are ok
	//only run the check if the calling object is the client_contribution_perc for element
	if(whichElement == document.getElementById('whichElement').value) {
		//alert('checking contribution constraint');
		var constraintField = document.getElementById('schemeAppliesTo').value;
		var constraintFieldType = document.getElementById('fieldType').value;
	
		var constraintSymbol = new String();
		constraintSymbol = document.getElementById('constraintSymbol').value;
	
		var schemeConstraint = new Object();
		schemeConstraint = document.getElementById('schemeRule').value;
	
		var constraintString = document.getElementById(constraintField);
		var constraintStringVal = new Object();
		if(constraintString) {
			if(constraintFieldType == 'value') {
			constraintStringVal = constraintString.value;
			} 
			if(constraintFieldType == 'innerHTML') {
			constraintStringVal = constraintString.innerHTML;
			}
	
			var formulaConstraint = eval(constraintStringVal + '' + constraintSymbol + '' + schemeConstraint);
			if(!formulaConstraint) {
			alert(document.getElementById('constraintComment').innerHTML + ' proceeding will give erroneous results');
			//document.getElementById('errorHolder').innerHTML = '<img width="20" height="20" src="../images/warning1.png">';
			return false;
			} else {
		
			//nothing to evaluate.
			}
		}
		
	}
	//alpha

	if (percIC.value < 0 | percIC.value >100 | n == false)
	{

		showAlert(270, 1);
		setTimeout(function(){percIC.focus();}, 10);
		percIC.value = "";
		return false;
		
	}
	else
	{
		
		
		var valClientContr =  clientsInitalContr(propPrice, percIC);	
		document.getElementById(clientContr).innerHTML = valClientContr;
		
		var valLoanContr =  loanAmountVal(propPrice, percIC);
		document.getElementById(loanVal).innerHTML = valLoanContr;
		
		var valLoanPerc = loanAmountPerc(percIC);
		document.getElementById(loanPerc).value = valLoanPerc;
		//document.getElementById('errorHolder').innerHTML = '';
			
		
	}
	
}


//this function is used to alert the user when they have changed the contract price so that they are aware it is not the true value of the propert price
function checkPriceChange(enteredPrice, validPrice)

{
	var priceEnt = '';
	var priceOrig = '';
	
	 priceEnt = Number(document.getElementById(enteredPrice).value);
	 priceOrig = Number(document.getElementById(validPrice).value);
	
	if (priceEnt != priceOrig)
	{
		var textP = 'Note: Price optionally changed';
		textP += '<br><span class="mouse" onclick="myOnClick(\''+ enteredPrice +'\', '+ priceOrig +')" >Reset Contract Price</span>';
		document.getElementById("changeOfPrice").innerHTML = textP;

	}
	
	
}

function myOnClick(enteredPrice,validPrice) {


document.getElementById(enteredPrice).value = validPrice;

}


function checkLoanYears(enteredLoan, maxPossLoanDur, whichElement)

{
	

//checks whether value is numeric
	if(IsNumeric(document.getElementById(enteredLoan).value))
	{
		
		
		//alpha
		//ensure any contraints are ok
		//only run the check if the calling object is the client_contribution_perc for element
		if(whichElement == document.getElementById('whichElement').value) {
			var constraintField = document.getElementById('schemeAppliesTo').value;
		var constraintFieldType = document.getElementById('fieldType').value;
	
		var constraintSymbol = new String();
		constraintSymbol = document.getElementById('constraintSymbol').value;
	
		var schemeConstraint = new Object();
		schemeConstraint = document.getElementById('schemeRule').value;
	
			var constraintString = document.getElementById(constraintField);
		var constraintStringVal = new Object();
		if(constraintString) {
			if(constraintFieldType == 'value') {
			constraintStringVal = constraintString.value;
			} 
			if(constraintFieldType == 'innerHTML') {
			constraintStringVal = constraintString.innerHTML;
			}
			var formulaConstraint = eval(constraintStringVal + '' + constraintSymbol + '' + schemeConstraint);
			if(!formulaConstraint) {
			alert(document.getElementById('constraintComment').innerHTML + ' proceeding will give erroneous results');
			//document.getElementById('errorHolder').innerHTML = '<img width="20" height="20" src="../images/warning1.png">';
			return false;
			} else {
		
			//nothing to evaluate.
			}
		}
		
		}
		//alpha
	
		var enteredLoanYears = '';
		var maxPossLoanDurYears = '';
	
	 	enteredLoanYears = Number(document.getElementById(enteredLoan).value);
	 	maxPossLoanDurYears = Number(document.getElementById(maxPossLoanDur).innerHTML);
		
	
	 	//"Entered loan duration exceeds maximum allowed of " + maxPossLoanDurYears  + "\nUse maximum loan duration?"
		//checks whether the entered loan duration exceeds maximum Loan Duration from bank
	 		if (enteredLoanYears > maxPossLoanDurYears)
		 	{
		 		var loanD = confirm("Entered loan duration exceeds maximum allowed of " + maxPossLoanDurYears  + "\nUse maximum loan duration?"); 
				
		 		if (loanD)
		 		{
				 	document.getElementById(enteredLoan).value = document.getElementById(maxPossLoanDur).innerHTML;
			 		getLoanEndDate();
					//calcLoanMonths(enteredLoan, maxPossLoanDur);
		 		}
		 		else 
		 		{
					//fills box with enter loan duration and highlights it so user can enter the loan duration
					enterMaxCalcLoanPeriod(enteredLoan); 
					
		 		}
		
	 		}
			else
			{
				if (document.getElementById('loan_start_date').value != '')
				{							
					getLoanEndDate();
				}
			}
	
			  
	}
	else
	{
		showAlert(272, 1);
		document.getElementById(enteredLoan).value = "";
		setTimeout(function(){document.getElementById(enteredLoan).focus();}, 10);
		return false;
	}
	
		
//end checkLoanYears function	
}



function enterMaxCalcLoanPeriod(loanDur)
{
	        document.getElementById(loanDur).value = "Enter preferred loan period";	
			setTimeout(function(){document.getElementById(loanDur).select();}, 10);	
}


//this function ia called when the user clicks in the box (Loan in years)
//it will ask the user whether they want to fill the box with the maximum loan duration and if not it will call the enterMaxCalcLoanPeriod function which will then let them enter in a value.
function fillLoanDuration(loanDur, maxLoanDur)

{
	if (document.getElementById(loanDur).value == "")
	{	var maxLoanDuration  = Number(document.getElementById(maxLoanDur).innerHTML);
		var loanQ = confirm("Enter maximum possible loan duration of " + maxLoanDuration +" years?");
		if(loanQ)
		{
			document.getElementById(loanDur).value = document.getElementById(maxLoanDur).innerHTML;
			
		}
		else 
		{	
			enterMaxCalcLoanPeriod(loanDur);
			
		}
	}
	
}


//*************************************End:Marianne Angelides**************************************************/

