// JavaScript Document

function setPCode(){
	var pcode = getValue(pcodeArea);
	getValue(pCode) = pcode;
}

function setPCodeArea(el){
	var pcode = getElement('pcodeArea');
	var postcode = el.value;
	var i = 0;
	var pArea = "";
	while((postcode.charAt(i) < '0' || postcode.charAt(i) > '9') && i < postcode.length){
		pArea += postcode.charAt(i);
		i++;
	}
	var midAreas = ["B","CV","DE","DY","HR","LE","NG","NN","ST","TF","WR","WS","WV"];
	for(var i = 0; i < midAreas.length; i++){
		if (pArea == midAreas[i]){
			pArea = "MID";	
		}
	}
	pcode.value = pArea;
}

function compare(ela, elb){
	if(getValue(ela)!=getValue(elb)){
		alert('Please confirm you email address');
		//getElement(elb).focus();
		getElement(ela).style.borderColor="red";
		getElement(ela).style.borderWidth="1px";
		getElement(ela).style.borderStyle="solid";
		getElement(elb).style.borderColor="red";
		getElement(elb).style.borderWidth="1px";
		getElement(elb).style.borderStyle="solid";
		return false;
	}else{
		getElement(ela).style.borderColor="green";
		getElement(ela).style.borderWidth="1px";
		getElement(ela).style.borderStyle="solid";
		getElement(elb).style.borderColor="green";
		getElement(elb).style.borderWidth="1px";
		getElement(elb).style.borderStyle="solid";
		return true;
	}	
}


function popUp(URL, w, h) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=1,statusbar=0,menubar=0,resizable=1,width=" + w  + ",height=" + h + "');");
}

function popUpAbout(URL, w, h) {
	day = new Date();
	id = day.getTime();
	eval("aboutMyPlace = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=1,statusbar=0,menubar=0,resizable=1,width=" + w  + ",height=" + h + "');");
}
function getEnqType(){
	var enquiryType = getValue('enqType');
	//alert(enquiryType);
	if(enquiryType == "Other query"){
		getElement('queryRqd').style.display="";	
	}else{
		getElement('queryRqd').style.display="none";	
	}
	if(enquiryType=="Information about a development"){
		getElement('development').style.display="";
		getElement('devName').disabled="";
	}else{
		getElement('development').style.display="none";
		getElement('devName').disabled="disabled";
	}
	
}

function validate(type){
	if (type=="enquiry"){
		var message = 'Please fill all of the required fields\n';
		var firstfocus = null;
		var status = true;
		
		if(checkEmpty('uName')){
			message += 'Name\n';
			firstfocus = 'uName';
			status = false;
		}
		if(checkEmpty('uEmail')){
			message += 'Email\n';
			if(firstfocus == null){
				firstfocus = 'uEmail';
			}
			status = false;
		}
		if(checkEmpty('confEmail')){
			message += 'Confirm Email\n';
			if(firstfocus == null){
				firstfocus = 'confEmail';
			}
			status = false;
		}
		if(checkEmail('uEmail', 'confEmail')){
			message += 'Please ensure your email and confirm email addresses match\n';
			if(firstfocus == null){
				firstfocus = 'confEmail';
			}
			status = false;
		}else if(checkEmailValid('uEmail')){
			message += 'Please make sure you have entered a valid email address\n';
			if(firstfocus == null){
				firstfocus = 'uEmail';
			}
			status = false;
		}
		if(!checkPCodeArea()){
			message += 'Post Code is invalid\n';
			if(firstfocus == null){
				firstfocus = 'pCode';
			}
			status = false;
		}
		if(checkEmpty('pCode')){
			message += 'Post Code\n';
			if(firstfocus == null){
				firstfocus = 'pCode';
			}
			status = false;
		}
		if(checkEmpty('contNum')){
			message += 'Contact Number\n';
			if(firstfocus == null){
				firstfocus = 'contNum';
			}
			status = false;
		}
		if(checkEmpty('query')){
			if(getValue('enqType') == "Other query"){
				message += 'Please enter your query or choose a different query type.\n';
				if(firstfocus == null){
					firstfocus = 'query';
				}
				status = false;
			}
			
		}
		if(status == false){
			alert(message);
			getItem(firstfocus).focus();
		}
		//return false;
		return status;
	}
	if(type == "appForm"){
		var message = 'Please fill all of the required fields\n';
		var firstfocus = null;
		var status = true;
		
		if(checkEmpty('uName')){
			message += 'Name\n';
			firstfocus = 'uName';
			status = false;
		}
		if(checkEmpty('dob')){
			message += 'Date of Birth\n';
			if(firstfocus == null){
				firstfocus = 'dob';
			}
			status = false;
		}
		if(!checkPCodeArea()){
			message += 'Post Code is invalid\n';
			if(firstfocus == null){
				firstfocus = 'pCode';
			}
			status = false;
		}
		if(checkEmpty('pCode')){
			message += 'Post Code\n';
			if(firstfocus == null){
				firstfocus = 'pCode';
			}
			status = false;
		}
		if(checkEmpty('daytel')){
			message += 'Daytime Telephone Number\n';
			if(firstfocus == null){
				firstfocus = 'daytel';
			}
			status = false;
		}
		if(checkEmpty('eveningtel')){
			message += 'Evening Telephone Number\n';
			if(firstfocus == null){
				firstfocus = 'eveningtel';
			}
			status = false;
		}
		if(checkEmpty('uEmail')){
			message += 'Email\n';
			if(firstfocus == null){
				firstfocus = 'uEmail';
			}
			status = false;
		}else if(checkEmailValid('uEmail')){
			message += 'Please make sure you have entered a valid email address\n';
			if(firstfocus == null){
				firstfocus = 'uEmail';
			}
			status = false;
		}
		if(checkEmpty('job')){
			message += 'Job\n';
			if(firstfocus == null){
				firstfocus = 'job';
			}
			status = false;
		}
		if(checkEmpty('duration')){
			message += 'Duration of work\n';
			if(firstfocus == null){
				firstfocus = 'duration';
			}
			status = false;
		}
		if(checkEmpty('income')){
			message += 'Income\n';
			if(firstfocus == null){
				firstfocus = 'income';
			}
			status = false;
		}
		if(checkEmpty('benefits')){
			message += 'Benefit\n';
			if(firstfocus == null){
				firstfocus = 'benefits';
			}
			status = false;
		}
		if(checkEmpty('deposit')){
			message += 'Deposit\n';
			if(firstfocus == null){
				firstfocus = 'deposit';
			}
			status = false;
		}
		if(checkEmpty('outgoings')){
			message += 'Outgoings\n';
			if(firstfocus == null){
				firstfocus = 'outgoings';
			}
			status = false;
		}
		
		if(status == false){
			alert(message);
			getItem(firstfocus).focus();
		}
		return status;
		//return false;
	}
}

function checkEmail(email, emailConf){
	if(getValue(email)!=getValue(emailConf)){
		return true;
	}else{
		return false;
	}
}

function getElement(el){
	return document.getElementById(el);
}

function getItem(el){
	return document.getElementById(el);
}

function getValue(el){
	return getItem(el).value;
}

function checkEmpty(el){
	if(getValue(el)==""){
		return true;
	}else{
		return false;
	}
}

function checkEmailValid(emailAddr){
	var emailValid = false;
	var email = getValue(emailAddr);	
	var containsAt = false;
	for(var j = 0; j < email.length; j++){
		if(email.charAt(j) == '@'){
			containsAt = true;
		}
	}
	if(containsAt){
		if(email.charAt(email.length - 3) == '.' || email.charAt(email.length - 4) == '.'){
			if(!(email.charAt(email.length - 3) == '.' && email.charAt(email.length - 4) == '.')){
				emailValid = true;	
			}
		}
	}
	return !emailValid;
}

function checkPCodeArea(){
	var pCodeArea = document.getElementById('pcodeArea').value.toUpperCase();
	var areas =["AB","AL","B","BA","BB","BD","BH","BL","BN","BR","BS","BT","CA","CB","CF","CH","CM","CO","CR","CT","CV","CW","DA","DD","DE","DG","DH","DL","DN","DT","DY","E","EC","EH","EN","EX","FK","FY","G","GL","G","HA","HD","HG","HP","HR","HS","HU","HX","IG","IP","IV","KA","KT","KW","KY","L","LA","LD","LE","LL","LN","LS","LU","M","ME","MK","ML","N","NE","NG","NN","NP","NR","NW","OL","OX","PA","PE","PH","PL","PO","PR","RG","RH","RM","S","SA","SE","SG","SK","SL","SM","SN","SO","SP","SR","SS","ST","SW","SY","TA","TD","TF","TN","TQ","TR","TS","TW","UB","W","WA","WC","WD","WF","WN","WR","WS","WV","YO","ZE","MID"];
	var valid = false;
	for(var i = 0; i < areas.length; i++){
		if(pCodeArea == areas[i]){
			valid = true;	
		}
	}
	return valid;
}

function swapImage(url){
	document.getElementById('mainimage').src=url;
}