// Search box validation used for both the search box and the main search page
function searchpagevalidate(ref) {
	if (ref.keyword.value != '') {
		return true;
	} else {
		alert('Need a keyword to \nsearch on!');
		return false;
	}
}

// automatic print function
// (C) 2000 www.CodeLifter.com
// http://www.codelifter.com
// Free for all users, but leave in this  header
function printWindow(){
   bV = parseInt(navigator.appVersion);
   if (bV >= 4) window.print();
}

function rollOvers(){
	sfHover = function() {
		var sfEls = document.getElementById("topleftnav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}	
	}
	if (window.attachEvent) window.attachEvent("onload", sfHover);
}

/**
* check and correct an email address.
* function expects the input field and value it will remove all spaces
* convert to lower case and then compare with a regexp.
* if everything is correct the field is updated with the formated value
* and the function returns true
*/
function chkEmail(form, input, value){
    var nValue = value.replace(/\s+/g,'').toLowerCase();
    if (nValue.search(/^[a-z0-9_\.-]+@([a-z0-9]+([\.\-][a-z0-9]+)*\.)+[a-z]{2,7}$/) == -1) {
        return false;
    }
    input.value = nValue;
    return true;
}

/**
* check, correct and compare two email address.
*/
function chkConfirmEmail(form, input, value){
    var strID = input.id.replace("CONFIRM","");
    var emailInput = form[strID];
    //first check this value
    if(!chkEmail(form, input, value)){
        return false;
    } else if(!emailInput){
        //no point in comparing if there is no email field
        return true;
    } else if(!chkEmail(form, emailInput, emailInput.value)){
        //the real email isn't valid so it will throw the error
        return true; 
    } else if ( input.value != emailInput.value ) {
        //both fields have been formated and checked and they don't match
        return false;
    }
    return true;
}

/**
* check and correct a UK PostCode.
* function expects the input field and value it will remove all spaces
* convert to upper case and then compare with a regexp.
* if everything is correct a space is inserted in the correct position,
* the field is updated with the formated value.
* there may be associated POUTCODE and PINCODE fields if these exist
* the appropriate values are inserted and the function returns true.
*/
function chkPostCode(form, input, value){
    var nValue = value.replace(/\s+/g,'').toUpperCase();
    if(nValue.search(/^[A-PR-UWYZ](([0-9][A-HJKS-UW0-9]?)|([A-HK-Y][0-9][ABEHMNPRVWXY0-9]?))([0-9]|([0-9][ABD-HJLNP-UW-Z]{2}))$/) == -1) {
        return false;
    }
    //split the string into an outcode and incode
    var pOut = nValue.slice(0, nValue.length-3);
    var pIn = nValue.slice(-3);
    //check if the outcode and incode fields exist, if they do set there value
    var strId = input.id.replace("POSTCODE", "POUTCODE");
    var nInput = form[strId];
    if(nInput) {
        nInput.value = pOut;
    }
    strId = strId.replace("POUTCODE", "PINCODE");
    nInput = form[strId];
    if(nInput) {
        nInput.value = pIn;
    }
    //finally set the formated value;
    input.value = pOut + " " + pIn;
    return true;
}

Ext.ns('Ext.ux.form'); // set up Ext.ux.form namespace

/**
 * @class Ext.ux.form.Spacer
 * @extends Ext.BoxComponent
 * Utility spacer class.
 * @constructor
 * @param {Number} height (optional) Spacer height in pixels (defaults to 22).
 */
Ext.ux.form.Spacer = Ext.extend(Ext.BoxComponent, {
  height: 25,
  autoEl: 'div'
});
Ext.reg('spacer', Ext.ux.form.Spacer);

/**
 * @class Ext.ux.form.Separator
 * @extends Ext.BoxComponent
 * Utility spacer class.
 * @constructor
 * @param {Number} height (optional) Spacer height in pixels (defaults to 22).
 */
Ext.ux.form.Separator = Ext.extend(Ext.BoxComponent, {
  height: 10,
  autoEl: 'div'
});
Ext.reg('separator', Ext.ux.form.Separator);

/**
 * display contact agent dialog
 */

var showAgentDetails;
var showContactAgent;

Ext.onReady(function() {
	// Create dialog boxes
	var agentDetailBox = newExtWindow('agentdetaildialog');

	// Fill the dialogs with relevant information, then show them 
	showAgentDetails = showAgentDialog(agentDetailBox, function(agent, dialog) {
		dialog.setHtml("detailframe", "<iframe width=\"680\" height=\"450\" src=\"" + agent.detail + "\" />");
	});
	showContactAgent = showAgentDialog(agentDetailBox, function(agent, dialog) {
		dialog.setHtml("detailframe", "<iframe width=\"680\" height=\"450\" src=\"" + agent.contact + "\" />");
	});
});

function showAgentDialog(dialog, prepfunc) {
	function showAndCenter(dialog) {
		showDialog(dialog);
		dialog.center();
	}

	// Set a particular property of a HTML element
	function setHtml(id, value, text) {
		var el = Ext.get(id);
		if (el) {
			if (value) {
				el.show();
				el.dom["innerHTML"] = text ? text : value;
			} else {
				el.hide();
			}
		}
	}

	return function(index) {
		// Make sure no other dialogs are active
		closeDialog();

		// Get the details of the selected agent
		var agent = getAgent(index);

		// Set the actual bits of data via a couple of accessors
		prepfunc(agent, {setHtml:setHtml});

		// Load the light box
		showAndCenter(dialog);
	};
}


