if(
	document.getElementById
){
	var uk; // Required in GLOBAL scope - does not overwrite any exisiting objects!
	if(!uk){ uk = {}; } else if(typeof uk != "object"){ eval('throw new Error("uk already exists and is not an object");'); }
	if(!uk.co){ uk.co = {}; } else if(typeof uk.co != "object"){ eval('throw new Error("uk.co already exists and is not an object");'); }
	if(!uk.co.pntestbed){ uk.co.pntestbed = {}; } else if(typeof uk.co.pntestbed != "object"){ eval('throw new Error("uk.co.pntestbed already exists and is not an object");'); }
	if(!uk.co.pntestbed.core){ eval('throw new Error("uk.co.pntestbed.core does not exist");'); }
	var PNC = uk.co.pntestbed.core;
	var CJ = {};
	
	PNC.window.onload(function(){
		if(PNC.$('page-menu-hidden')){
			CJ.PageMenuObject = new CJ.PageMenu('page-menu-hidden', 'menu-a-property-services', 'CJ.PageMenuObject');
		}
		var location_args = getArgs();
		//---------------------------------------------------------------------
		//---------------------------------------------------------------------
		// Property Search Form -----------------------------------------------
		var aImageBoxData = ['1','2','3','4','all'];
		var aImageBoxText = ['0 - 1,500 sq ft','1,500 - 3,000 sq ft','3,000 - 5,000 sq ft','5,000+ sq ft','All sizes'];
		var a = PNC.$$('imagebox');
		if(a.length > 0){
			for(var i = 0; i < a.length; i++){
				var id = 'band-'+aImageBoxData[i];
				var classname = 'image-band';
				var src = './style/form/property-search-'+aImageBoxData[i]+'.jpg';
				var result = 'band-'+aImageBoxData[i];
				var name = 'band';
				var value = aImageBoxData[i];
				var sArgs = '\''+name+'\', \''+id+'\', \''+value+'\'';
				var html = '<a id="'+id+'" class="'+classname+'" onmouseover="CJ.ImageOver('+sArgs+');" onmouseout="CJ.ImageOut('+sArgs+');" onclick="CJ.ImageClick('+sArgs+'); return false;"><img src="'+src+'"/></a>';
				a[i].innerHTML = html;
			}
			PNC.$('property-search-form').innerHTML += '<input type="hidden" name="band" value=""/>';
		}
		
		var a = PNC.$$('submitbox');//alert(location_args['band']);
		if(a.length > 0){
			for(var i = 0; i < a.length; i++){
				var id = 'band-'+aImageBoxData[i];
				var classname = 'class="image-band' + (location_args['band'] == aImageBoxData[i]? ' selected': '') + '"';
				var src = './style/form/property-search-'+aImageBoxData[i]+'.jpg';
				var result = 'band-'+aImageBoxData[i];
				var name = 'band';
				var value = aImageBoxData[i];
				var txt = aImageBoxText[i];
				var sArgs = '\''+name+'\', \''+id+'\', \''+value+'\'';
				var html = '<a id="'+id+'"'+classname+'" onmouseover="CJ.SubmitOver('+sArgs+');" onmouseout="CJ.SubmitOut('+sArgs+');" onclick="CJ.SubmitClick('+sArgs+'); return false;">'+txt+'</a>';
				a[i].innerHTML = html;
			}
			PNC.$('property-search-form').innerHTML += '<input type="hidden" name="band" value=""/>';
		}
		
		
		var aRadioBoxData = ['1','2','3','all','1','2','all'];	
		var a = PNC.$$('radiobox');
		if(a.length > 0){
			var aChecked = [];
			for(var i = 0; i < a.length; i++){
				var sType = i < 4? 'type': 'tenure';
				var id = sType+'-'+aRadioBoxData[i];
				var name = sType;
				var value = aRadioBoxData[i];
				if(location_args[sType] && aRadioBoxData[i] == location_args[sType]){
					aChecked.push([name, id, value]);
				}
				var sArgs = '\''+name+'\', \''+id+'\', \''+value+'\'';
				a[i].innerHTML = '<a id="'+id+'" onmouseover="CJ.RadioOver('+sArgs+');" onmouseout="CJ.RadioOut('+sArgs+');" onclick="CJ.RadioClick('+sArgs+'); return false;"></a>';
			}
			PNC.$('property-search-form').innerHTML += '<input type="hidden" name="type" value=""/><input type="hidden" name="tenure" value=""/>';
			if(!location_args['type']){
				aChecked.push(['type', 'type-all', 'all']);
			}
			if(!location_args['tenure']){
				aChecked.push(['tenure', 'tenure-all', 'all']);
			}
			for(var i = 0; i < aChecked.length; i++){
				CJ.RadioClick(aChecked[i][0], aChecked[i][1], aChecked[i][2]);
			}
		}
		//---------------------------------------------------------------------
		//---------------------------------------------------------------------


		//---------------------------------------------------------------------
		//---------------------------------------------------------------------
		// Property Search Item page thumbs -----------------------------------
		var a = PNC.$$('item-thumb-link');
		if(a.length > 0){
			for(var i = 0; i < a.length; i++){
				a[i].onclick = function(e){ 
					this.blur(); 
					if(this.className.match(/\bselected\b/g) == null){
						CJ.SelectThumb(this.id.split('-').pop());
					}
					return false;
				};
			}
		}
		//if(window.print && PNC.$('property-search-item-column-right')){
			//PNC.$('property-search-item-column-right').innerHTML += '<a id="item-print" title="Print this page" href="#" onclick="window.print(); return false;">Print</a>';
		//}
		//---------------------------------------------------------------------
		//---------------------------------------------------------------------
		
		
		// This is for the contact page form
		function check_email(src) {
			var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
			return regex.test(src);
		}
		function check_telephone(src) {
			var regex = /[^0-9+\-() ]/g;
		  	if(regex.test(src)){
		  		return false;
		  	} else {
				regex = /[0-9]/g;
				return regex.test(src);
			}
		}
		if(PNC.$('formcontact')){
			PNC.$('formcontact').onsubmit = function(e){
				if(this.name.value.length == 0){						// Check name
					alert("Name has to be filled in."); 				this.name.focus();		return false;	
				}
				if(this.email.value.length == 0){						// Check Email address
					alert("Email has to be filled in.");				this.email.focus();		return false;
				} else if(!check_email(this.email.value)){
					alert("Email does not contain a valid address.");	this.email.focus();		return false;
				}
				return true;
			};
		}
		//---------------------------------------------------------------------
		//---------------------------------------------------------------------
		
		
		
		
	});
	////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
CJ.SelectThumb = function(sId){
	var a = PNC.$$('item-thumb-link');
	for(var i = 0; i < a.length; i++){
		a[i].className = 'item-thumb-link' + (a[i].id == 'item-thumb-link-' + sId? ' selected': '');
	}
	PNC.$('item-thumb-img').src = './manager/jpg/property/'+ sId.substr(0,1) + '/cj_' + PadDigits(sId, 6) + '.jpg';
}

//---------------------------------------------------------------------
//---------------------------------------------------------------------
CJ.SubmitClick = function(name, id, value){
	PNC.$('property-search-form').band.value = value;
	PNC.$('property-search-form').submit();
}
CJ.SubmitOver = function(name, id, value){ }//PNC.$(id).parentNode.parentNode.style.backgroundColor = '#F7941E'; }
CJ.SubmitOut = function(name, id, value){  }//PNC.$(id).parentNode.parentNode.style.backgroundColor = '#ADAFA7'; }

CJ.ImageClick = function(name, id, value){
	PNC.$('property-search-form').band.value = value;
	PNC.$('property-search-form').submit();
}
CJ.ImageOver = function(name, id, value){ PNC.$(id).parentNode.parentNode.style.backgroundColor = '#F7941E'; }
CJ.ImageOut = function(name, id, value){  PNC.$(id).parentNode.parentNode.style.backgroundColor = '#ADAFA7'; }

CJ.RadioClick = function(name, id, value){
	var i = 0;
	while(true){
		var sID = name + '-' + (++i);
		if(PNC.$(sID)) PNC.$(sID).style.backgroundPosition = '0px 0px'; else break;
	}
	PNC.$(name + '-all').style.backgroundPosition = '0px 0px';
	PNC.$(id).style.backgroundPosition = '-32px 0px';
	PNC.$('property-search-form')[name].value = value;
}
CJ.RadioOver = function(name, id, value){ if(PNC.$('property-search-form')[name].value != value){ PNC.$(id).style.backgroundPosition = '-16px 0px'; } }
CJ.RadioOut = function(name, id, value){  if(PNC.$('property-search-form')[name].value != value){ PNC.$(id).style.backgroundPosition = '0px 0px';   } }
//---------------------------------------------------------------------
//---------------------------------------------------------------------

//---------------------------------------------------------------------
/*
	*	--------------------------
	* 	Constructor
	*	--------------------------
*/
CJ.PageMenu = function(sIDTrg, sIDSrc, sObjectName){
	this.m_oTrg = PNC.$(sIDTrg);
	this.m_oSrc = PNC.$(sIDSrc);
	this.m_sObjectName = sObjectName;
	
	this.m_timer = false;
	this.m_msDelay = 300;
	
	var _this = this;
	this.m_oTrg.onmouseover = function(e){ _this.show(); };
	this.m_oTrg.onmouseout = function(e){ _this.startTimer(); };
	this.m_oSrc.onmouseover = function(e){ _this.show(); };
	this.m_oSrc.onmouseout = function(e){ _this.startTimer(); };
};
CJ.PageMenu.prototype.show = function(){
	this.clearTimer();
	this.m_oTrg.style.display = 'block';
	this.m_oSrc.className = 'selected';

};
CJ.PageMenu.prototype.hide = function(){
	this.clearTimer();
	this.m_oTrg.style.display = 'none';
	this.m_oSrc.className = '';
};
CJ.PageMenu.prototype.startTimer = function(){
	var _this = this;
	if(!this.m_timer){
		//this.m_timer = setTimeout(_this.hide, this.m_msDelay);
		this.m_timer = setTimeout(this.m_sObjectName + ".hide()" , this.m_msDelay);
	}
};
CJ.PageMenu.prototype.clearTimer = function(){
	if(this.m_timer){
		clearTimeout(this.m_timer);
		this.m_timer = false;
	}
};


/* Courtesy of: http://classicasp.aspfaq.com/general/how-do-i-pad-digits-with-leading-zeros.html */
function PadDigits(n, totalDigits){
	n = n.toString(); 
	var pd = ''; 
	if(totalDigits > n.length){ 
		for(var i = 0; i < (totalDigits - n.length); i++){ 
			pd += '0'; 
		} 
	} 
	return pd + n.toString(); 
} 

function getArgs(){
	var args = new Object();
	var query = location.search.substring(1);
	var pairs = query.split('&');
	for(var i = 0; i < pairs.length; i++){
		var pos =pairs[i].indexOf('=');
		if(pos == -1) continue;
		var argname = pairs[i].substring(0, pos);
		var value = pairs[i].substring(pos+1);
		value = decodeURIComponent(value);
		args[argname] = value;
	}
	return args;
}
