// Set local domain (dynamically)
// Browser detection
var b=navigator.userAgent.toLowerCase();
n_ie_win=((b.indexOf('msie')>0)&&(b.indexOf('windows')>0))?true:false;
n_ie_mac=((b.indexOf('msie')>0)&&(b.indexOf('mac')>0))?true:false;
n_ff_win=((b.indexOf('firefox')>0)&&(b.indexOf('windows')>0))?true:false;
n_ff_mac=((b.indexOf('firefox')>0)&&(b.indexOf('mac')>0))?true:false;
n_s_mac=((b.indexOf('safari')>0)&&(b.indexOf('mac')>0))?true:false;

// Set local domain (dynamically)
var theURL=document.URL.toString();
var domain=theURL.split('/');
var myDomain="http://"+domain[2];
var sPage="";
var menObj="";
//var isSub=false;

// Get query string data and set as page variables
if(!n_ie_mac){
	var sData = new Array();
	var sTemp;
	var sess = '';
	var sQueryString = document.location.search.substr(1);
	var doubles =  sQueryString.split("&");
	for (var i = 0; i < doubles.length; i ++){
		sTemp = doubles[i].split("=");
		window[sTemp[0]] = sTemp[1];
		if (doubles[i].indexOf('PHPSESSID')!=-1){
			sess = doubles[i];
		}
	}
}

/*window.onunload=function(){
	if(sPage!=""){
		var tempURL=document.URL.toString();
		if(tempURL.indexOf(myDomain)!=-1){
		//	var searchstring=(document.location.search.substr(1))?"&":"?";
			//document.location.search=searchstring+"sPage="+sPage+"&isSub="+isSub+"&menObj="+menObj;
			document.location.search="sPage="+sPage+"&menObj="+menObj;
		}
	}
}*/

//Send search data to search script within Iframe.
window.onload=function(){
	if(!(n_ie_win)||(n_ie_mac)){
		if (document.forms.length>0){
			for (i=0; i < document.siteform.elements.length;i++ ) {
				if (document.siteform.elements[i].type=='') {
					document.siteform.elements[i].type='text';
				}
			}
		}
	}
	//Add mouseover/mouseout to the table cells of the drop down menus
	var type=new Array('vpp','os','uk');
	var tds=document.getElementsByTagName('TD');
	var styType="";
	for (i=0;i<type.length;i++){
		//alert(tds.length);
		for(j=0;j<tds.length;j++){
			//alert(tds[j].className.match(type[i]));
			if(tds[j].className.match(type[i])==type[i]){
				styType=type[i];
				break;
			}
		}
	}
	var as=document.getElementsByTagName('A');
	for(i=0;i<as.length;i++){
		if(as[i].className==styType+'link'){
			as[i].onmouseover=function(){
				var parentTD=this.parentNode;
				var parentCN=parentTD.className;
				parentTD.className=parentCN+" "+styType+"menuabg";
			}
			as[i].onmouseout=function(){
				var parentTD=this.parentNode;
				parentTD.className=styType+"menu";
			}
		}
		var theRef=document.location.toString();
		var theHref=theRef.split('?');
		if(as[i].href==theHref[0]){
			as[i].style.color='#A45220';
		}
	}
	if(sPage!=""){
		showMenu();
	}
}



function openTerms() {
	window.open(myDomain+'/TermsandConditions.html','_new','width=550,height=600,scrollbars=auto');
}

// Member authentification - set cookie and send to members page (if cookie not present they will be denied access)
var ckie="";
if(document.cookie){
	ckie=document.cookie.substring(document.cookie.indexOf('member='),document.cookie.length);
}
if((window.location.pathname=="/members.html")&&(ckie!="member=OK")){
	document.location=myDomain;
}

function goHome()  {
	document.location=myDomain;
}

// Members Login - change form action to send to test username/password
function login(){
	document.siteform.action="memberlogin.php";
	document.siteform.method="POST";
	document.siteform.submit();
}

// Image Pop-up (onclick) // Also used for gallery images
/*function popUp(page, winname, width, height, text ){
	var theWidth=(text!="")?width+20:width;
	var theScroll=(theWidth==width)?'no':'yes';
	var imageWindow=window.open('about:blank', winname, 'width='+theWidth+', height='+height+',scrollbars='+theScroll);
	imageWindow.document.write('<body style="padding:0px;margin:0px">');
	imageWindow.document.write('<img align="left" style="margin-right:10px;" src='+page+' border=0>');
	var theText="</body>";
	if(text) theText=(text!="")?'<div style="font-family:arial;font-size:11px;color:black">'+text+'</div></body>':'</body>';
	imageWindow.document.write(unescape(theText));
	imageWindow.document.close();
}*/
function popUp(page, winname, dims ){
	var WH=dims.split(',');
	var W=WH[0].split('=');
	var H=WH[1].split('=');
	//var theWidth=(text!="")?width+20:width;
	var theScroll=(parseInt(W[1])<780)?'no':'yes';
	var imageWindow=window.open(page, winname, 'width='+(parseInt(W[1])+20)+', height='+(parseInt(H[1])+20)+',scrollbars='+theScroll);
	/*imageWindow.document.write('<body style="padding:0px;margin:0px">');
	imageWindow.document.write('<img align="left" style="margin-right:10px;" src='+page+' border=0>');
	var theText="</body>";
	if(text) theText=(text!="")?'<div style="font-family:arial;font-size:11px;color:black">'+text+'</div></body>':'</body>';
	imageWindow.document.write(unescape(theText));
	imageWindow.document.close();*/
}

// Image Rollovers
var image;//Save image state
function chngImOver(image1, id){
	image=document.images[id].src;
	document.images[id].src=myDomain+"/images/"+image1;
}

function chngImOut(image2, id){
	document.images[id].src=myDomain+"/images/"+image2;
}

//Set image acording to which page is being viewed
function URLimg(img, url, theIm){
	theIm.onload='return false;';
	var loc=document.location.toString();
	var pageURL=loc.split('#');
	var pURL=(loc.indexOf('#')==-1)?pageURL:pageURL[0];
	if(pURL==url){ 
		theIm.src=myDomain+"/images/"+img;
		//if(theIm.parentElement.onmouseout) theIm.parentElement.onmouseout="chngImOut('"+img+"','"+theIm.id+"');";
	}
	else if(url==(document.location+"index.html")){ 
		theIm.src=myDomain+"/images/"+img;
		//if(theIm.parentElement.onmouseout) theIm.parentElement.onmouseout="chngImOut('"+img+"','"+theIm.id+"');";
	}
}

// Gallery - open large image window
function showIm(image){
	window.open("viewimage.html?image="+image, "_imageview");
}

// Gallery - Move through pages and re-populate the Gallery table
function chgIms(page){
	var siteurl = myDomain+"/images/";
	page = (page == '' || page == 'NULL' || !(page)) ? 0 : page;
	var totalpages=(theGallery.length%limit==0)?theGallery.length/limit:Math.floor(theGallery.length/limit)+1;
	for (i=0;i < limit ; i++){
		var arPos=page*limit+i;
		if(!theGallery[arPos]){
			document.all['space_'+i].innerHTML = '';
		}
		else{
			document.all['space_'+i].innerHTML = '<a href="#" onclick="popUp(\''+siteurl+theGallery[arPos]['image_'+arPos+'_large']+'\',\'Gallery_image\','+theGallery[arPos]['image_'+arPos+'_width']+','+theGallery[arPos]['image_'+arPos+'_height']+',\''+theGallery[arPos]['image_'+arPos+'_text']+'\');return false;"><img src="'+siteurl+theGallery[arPos]['image_'+arPos+'_thumb']+'" border="0"></a>';		
		}
	}
	pagelinks = "<font face='arial' size='2'>Page </font>";
	for (k=1;k <= totalpages ;k++ ){
		var arraynum = k - 1;
		pagelinks += (arraynum == page) ? '<a href="#" onclick="chgIms('+arraynum+');return false;">['+k+']</a>&nbsp;&nbsp;' : '<a href="#" onclick="chgIms('+arraynum+');return false;">'+k+'</a>&nbsp;&nbsp;';
	}
	document.all.pages.innerHTML = pagelinks;
}

// check links and add session data
function chkLinks() {
	for (i=0;i<=document.links.length-1;i++){
		if ((document.links[i].host.indexOf(document.location.host)!=-1)&&(sess!='')){
			document.links[i].href+= ((document.links[i].pathname.indexOf("display.php")!=-1)||(document.links[i].pathname.indexOf("reserve.php")!=-1)) ? "&"+sess : "?"+sess;
		}
	}
}

// Functionality for Drop Down Menus
function show(id,menobj,issub){
	document.getElementById(id).style.display='block';
	if (!issub){
		document.getElementById('td_'+id).style.backgroundColor=menobj.mainstyles.bg_plus;
	}
	if (document.images['pm_'+id]){
		chngImOver(menobj.mainstyles.im_minus, 'pm_'+id);	
	}
}
function hide(id,menobj,issub){
	
	setTimeout(document.getElementById(id).style.display='none', 1000);
	if (!issub){
		document.getElementById('td_'+id).style.backgroundColor=menobj.mainstyles.bg_minus;
	}
	if (document.images['pm_'+id]){
		chngImOut(menobj.mainstyles.im_plus, 'pm_'+id)	
	}
}

//function toggleMenu(id) {
//	document.getElementById(id).style.display=(document.getElementById(id).style.display=='none')?'block':'none';
//}

function toggleMenu(id,menobj,issub) {
	var tabs=document.getElementsByTagName('TABLE');
	for(i=0;i<tabs.length;i++){
		if(tabs[i].id.substring(0,8)=="m_tplate"){
			if(tabs[i].id!=id){
				tabs[i].style.display="none";
				if(document.getElementById('td_'+tabs[i].id)){
					document.getElementById('td_'+tabs[i].id).style.backgroundColor=menobj.mainstyles.bg_plus;
					document.images['pm_'+tabs[i].id].src = myDomain+"/images/"+menobj.mainstyles.im_plus;
				}
			}
		}
	}
	sPage=id;
	var temp=sPage.split('submenu');
	menObj=temp[0];
	//isSub=issub;
	showMenu();
}

function showMenu(){
	document.getElementById(sPage).style.display=(document.getElementById(sPage).style.display=='none')?'block':'none';
	//document.getElementById('pm_'+id).innerHTML=(document.getElementById('pm_'+id).innerHTML=='+')?'-':'+';
	//if (!isSub){
		document.getElementById('td_'+sPage).style.backgroundColor=(document.getElementById(sPage).style.display=="none") ? window[menObj].mainstyles.bg_plus : window[menObj].mainstyles.bg_minus;
	//}
	if (document.images['pm_'+sPage]){
		document.images['pm_'+sPage].src = (document.images['pm_'+sPage].src==myDomain+"/images/"+window[menObj].mainstyles.im_plus) ? myDomain+"/images/"+window[menObj].mainstyles.im_minus : myDomain+"/images/"+window[menObj].mainstyles.im_plus;
	}
	var as=document.getElementsByTagName("A");
	for(i=0;i<as.length;i++){
		if((as[i].href.indexOf(myDomain)!=-1)&&(as[i].href.indexOf('?')!=-1)){
			var tempHref=as[i].href.split('?');
			as[i].href=tempHref[0]+"?sPage="+sPage+"&menObj="+menObj;
		}else{
			as[i].href+="?sPage="+sPage+"&menObj="+menObj;
		}
	}
}


//var plink = "cgi-bin/display.pl?showprods=1";
//var glink = "cgi-bin/gold.pl";
var plink="PropertyGallery.html";
var glink="GoldForm.html";
var loginCheck=0;
var track="";

//window.onload = function(){
//	if(track!=""){
//		window.content.location='http://www.viceroyparkproperties.co.uk/memberform.html';
//	}
//}

function goldmember() {
	if (glink.indexOf("memid=")!=-1) {
		var it = "";
		while (confirm(it+"Do you have a concession code?")) {
			cd = prompt("Please enter your code");
			match = cd == "" ? "" : cd.substring(0,3).toUpperCase() + cd.substring(3,5)
			if ( match == "HPA10" ) {
				document.location=glink+"&conn=1";
			}
			it = "INVALID CODE!\n";
		}
		document.location=glink
	}
	else {
		document.location=glink
	}
}

function login(){
	document.location='SiteLogin.html?user_name='+document.getElementById('user_name').value+'&user_pass='+document.getElementById('user_pass').value
}

function loginNow() {
	if(loginCheck==0){
		loginCheck++;
		setTimeout('loginCheck=0', 1000);
		var locStr=new String(window.content.location);
		var locArr=locStr.split("/");
		document.forms[0].gold.value=(locArr[locArr.length-1]=='gold.pl')?1:0;
		if ((document.forms[0].user_name.value != "") && (document.forms[0].user_pass.value != "")){	
			return true;
			//document.forms[0].submit();
			//document.forms[0].user_name.value = "";
			//document.forms[0].user_pass.value = "";
		}
		else{
			alert("You must enter a user name and password to login");
			return false;
		}
	}else{
		alert('You have tried to submit this form more than once.\n\nIf you are experiencing login problems,\nplease check your username and password and only click the login button once.\n\nIf the problem persists, please contact us at info@viceroyparkproperties.co.uk');
		window.location="http://www.viceroyparkproperties.co.uk/home.html";
		return false;
	}
}

function join(){
	//original destination
	content.location='memberform.html';
}


function move(uk) {
	if (uk=='uk') {
		//document.location=plink+"&ukonly=1";
		document.location=plink
	}
	else {
		document.location=plink;
	}
}

function go(num){
	history.go(num);
}

function chkContact() {
	var email=document.siteform.email.value;
	if (document.siteform.name.value == ""){
		alert("You have to enter your name. Thank you.");
		document.siteform.name.focus()
		document.siteform.name.select()
		return false;
	}
	else if (document.siteform.address.value == ""){
		alert("You have to enter an Address. Thank you.");
		document.siteform.address.focus()
		document.siteform.address.select()
		return false;
	}
	else if (document.siteform.town.value == ""){
		alert("You have to enter a Town. Thank you.");
		document.siteform.town.focus()
		document.siteform.town.select()
		return false;
	}
	else if (document.siteform.county.value == ""){
		alert("You have to enter a County. Thank you.");
		document.siteform.county.focus()
		document.siteform.county.select()
		return false;
	}
	else if (document.siteform.postcode.value == ""){
		alert("You have to enter a Postcode. Thank you.");
		document.siteform.postcode.focus()
		document.siteform.postcode.select()
		return false;
	}
	else if (document.siteform.telephone.value == ""){
		alert("You have to enter a telephone number. Thank you.");
		document.siteform.telephone.focus()
		document.siteform.telephone.select()
		return false;
	}
	else if (!validEmail(email)) {  // check to see if the email's valid
		alert("Invalid email address! You must enter a Valid e-mail address. Thank you.")
		document.siteform.email.focus()
		document.siteform.email.select()
		return false
	}
	else if (document.siteform.comments.value == ""){
		alert("You have to enter an enquiry. Thank you.");
		document.siteform.comments.focus()
		document.siteform.comments.select()
		return false;
	}
	else {
		return true;
	}
}


var hasclicked=0;

function checkFields() {
	if (hasclicked == 1) {
		return false;
	}
	else {
		var email=document.siteform.email.value;
		if (document.siteform.company.value == ""){
			alert("You have to enter a Company Name. Thank you.");
			document.siteform.company.focus()
			document.siteform.company.select()
			return false;
		}
		else if (document.siteform.lname.value == ""){
			alert("You have to enter a Contact name. Thank you.");
			document.siteform.lname.focus()
			document.siteform.lname.select()
			return false;
		}
		else if (document.siteform.postcode.value == ""){
			alert("You have to enter a Postcode. Thank you.");
			document.siteform.postcode.focus()
			document.siteform.postcode.select()
			return false;
		}
		else if (document.siteform.tel_work.value == ""){
			alert("You have to enter a telephone number. Thank you.");
			document.siteform.tel_work.focus()
			document.siteform.tel_work.select()
			return false;
		}
		else if (document.siteform.address.value == ""){
			alert("You have to enter an Address. Thank you.");
			document.siteform.address.focus()
			document.siteform.address.select()
			return false;
		}
		else if (document.siteform.mobile.value == ""){
			alert("You have to enter a mobile number. Thank you.");
			document.siteform.mobile.focus()
			document.siteform.mobile.select()
			return false;
		}
		else if (document.siteform.town.value == ""){
			alert("You have to enter a Town. Thank you.");
			document.siteform.town.focus()
			document.siteform.town.select()
			return false;
		}
		else if (document.siteform.fax.value == ""){
			alert("You have to enter a fax number. Thank you.");
			document.siteform.fax.focus()
			document.siteform.fax.select()
			return false;
		}
		else if (document.siteform.county.value == ""){
			alert("You have to enter a County. Thank you.");
			document.siteform.county.focus()
			document.siteform.county.select()
			return false;
		}
		else if (document.siteform.devel_location.value == ""){
			alert("You have to enter a Develpoment Location. Thank you.");
			document.siteform.devel_location.focus()
			document.siteform.devel_location.select()
			return false;
		}
		else if (document.siteform.heardabout.selected =="") {
			alert("Please tell us how you heard about Viceroy. Thank you.");
			return false;
		}
		else if ((document.siteform.prange1.value =="")||(document.siteform.prange2.value =="")||(document.siteform.prange3.value =="")||(document.siteform.prange4.value =="")) {
			alert("Please tell us what price ranges you work in. Thank you.");
			return false;
		}
		else if ((document.siteform.ptype1.value =="")||(document.siteform.ptype2.value =="")||(document.siteform.ptype3.value =="")||(document.siteform.ptype4.value =="")) {
			alert("Please tell us what property types you offer. Thank you.");
			return false;
		}
		else if (document.siteform.heardabout.value == "other") {
			if (document.siteform.heard_other.value == "") {
				alert("Please tell us which other way you heard about us. Thank you.");
				document.siteform.heard_other.focus();
				document.siteform.heard_other.select();
				return false;
			}
		}
		else if (document.siteform.off_plan.value =="") {
			alert("Please tell us whether your off plan. Thank you.");
			return false;
		}
		else if (document.siteform.chk_email.value!=document.siteform.email.value) {
			alert("Your email confirmation does not match your original email!");
			document.siteform.email.focus();
			return false;
		}
		else if (!validEmail(email)) {  // check to see if the email's valid
			alert("Invalid email address! You must enter a Valid e-mail address. Thank you.")
			document.siteform.email.focus()
			document.siteform.email.select()
			return false
		}
		else {
			hasclicked = 1;

			//document.siteform.submit();
			return true;
		}
	}
}

function checkMember() {
	var act=true;
	if (hasclicked == 1) {
		
		return false;
	}
	else {
		hasclicked = 1;
		setTimeout('hasclicked=0',1000);
		var email=document.siteform.email.value;
		if (document.siteform.fname.value == ""){
			alert("You have to enter a First name or initials. Thank you.");
			document.siteform.fname.focus()
			document.siteform.fname.select()
			act=false;
		}
		if (document.siteform.lname.value == ""){
			alert("You have to enter a Surname. Thank you.");
			document.siteform.lname.focus()
			document.siteform.lname.select()
			act=false;
		}
		if (document.siteform.address.value == ""){
			alert("You have to enter a Address. Thank you.");
			document.siteform.address.focus()
			document.siteform.address.select()
			act=false;
		}
		if (document.siteform.town.value == ""){
			alert("You have to enter a Town. Thank you.");
			document.siteform.town.focus()
			document.siteform.town.select()
			act=false;
		}
		if (document.siteform.county.value == ""){
			alert("You have to enter a County. Thank you.");
			document.siteform.county.focus()
			document.siteform.county.select()
			act=false;
		}
		if (document.siteform.postcode.value == ""){
			alert("You have to enter a Postcode. Thank you.");
			document.siteform.postcode.focus()
			document.siteform.postcode.select()
			act=false;
		}
		if (document.siteform.tel_home.value == ""){
			alert("You have to enter a Telephone number. Thank you.");
			document.siteform.tel_home.focus()
			document.siteform.tel_home.select()
			act=false;
		}
		if (document.siteform.tel_mobile.value == ""){
			alert("You have to enter a Mobile number. Thank you.");
			document.siteform.tel_mobile.focus()
			document.siteform.tel_mobile.select()
			act=false;
		}
		if (document.siteform.heardabout.value == "") {
			alert("Please tell us how you heard about Viceroy. Thank you.");
			act=false;
		}
		if (document.siteform.heardabout.value == "other") {
			if (document.siteform.heard_other.value == "") {
				alert("Please tell us which other way you heard about us. Thank you.");
				document.siteform.heard_other.focus();
				document.siteform.heard_other.select();
				act=false;
			}
		}

		if (document.siteform.chk_email.value!=email) {
			alert("Your email confirmation does not match your original email!");
			document.siteform.email.focus();
			return false;
		}

		// check to see if the email's valid
		if (!validEmail(email)) {
			alert("Invalid email address! You must enter a Valid e-mail address. Thank you.")
			document.siteform.email.focus()
			document.siteform.email.select()
			act=false
		}
		if(!act){
			return false;
		}
	}

	if(document.all.radiohpa.checked==true){
		match = document.all.heard_hpa.value.substring(0,3).toUpperCase() + document.all.heard_hpa.value.substring(3,5)
		if(match=="HPA10"){
			return true;
		}else{
			alert('The code you require is 3 letters and 2 numbers (no spaces).\nPlease try again');
			document.all.heard_hpa.value="";
			return false;
		}
	}

	if(document.all.radiopan.checked==true){
		var match=document.all.heard_pan.value.toUpperCase();
		if(match=="HPA10"){
			return true;
		}else{
			alert('The PAN reference should be 3 letters and 1 number (no spaces).\nPlease try again');
			document.all.heard_pan.value="";
			return false;
		}
	}
	return true;
}


function validEmail(email) {
	invalidChars = " /:,;"

	if (email == "") {					
		return false
	}
	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1) {
			return false
		}
	}
	atPos = email.indexOf("@",1)			
	if (atPos == -1) {
		return false
	}
	if (email.indexOf("@",atPos+1) != -1) {	
		return false
	}
	periodPos = email.indexOf(".",atPos)
	if (periodPos == -1) {					
		return false
	}
	if (periodPos+3 > email.length)	{
		return false
	}
	return true
}
