// Set local domain (dynamically)
// Browser detection
var b=navigator.userAgent.toLowerCase();
var n_ie_win=((b.indexOf('msie')>-1)&&(b.indexOf('windows')>-1))?true:false;
var n_ie_mac=((b.indexOf('msie')>-1)&&(b.indexOf('mac')>-1))?true:false;
var n_ff_win=((b.indexOf('firefox')>-1)&&(b.indexOf('windows')>-1))?true:false;
var n_ff_mac=((b.indexOf('firefox')>-1)&&(b.indexOf('mac')>-1))?true:false;
var n_o_win=((b.indexOf('opera')>-1)&&(b.indexOf('windows')>-1))?true:false;
var n_o_mac=((b.indexOf('opera')>-1)&&(b.indexOf('mac')>-1))?true:false;
var n_s_mac=((b.indexOf('safari')>-1)&&(b.indexOf('mac')>-1))?true:false;

// Set local domain (dynamically)
var theURL=document.URL.toString();
var domain=theURL.split('/');
//var myDomain="http://"+domain[2];
var myDomain="http://www.warwickdesign.com";
var sPage="";
var menObj="";

//Set Flash to display
var showFlash=true;

// 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];
		}
	}
}

//Automatically  scroll content, depending on size of screen
function setScroll(){
	var scrHeight=(self.innerHeight)?self.innerHeight:document.body.clientHeight;
	document.getElementById('body_box').style.height=(scrHeight-parseFloat(document.getElementById('body_box').style.top)-50)+'px';
	document.getElementById('body').style.height=scrHeight-50;
	document.getElementById('pos_table').style.height=scrHeight;
	document.getElementById('body_box').style.overflow='auto';
}

//window.onresize=setScroll;

//Send search data to search script within Iframe.
window.onload=function(){
		if(sPage!="")showMenu(sPage,menObj);
		else showMenu(null,null);
}

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();
}

// 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=image;
}

//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;
	}
	else if(url==(document.location+"index.html")){ 
		theIm.src=myDomain+"/images/"+img;
	}
}

// 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){
	document.getElementById(id).style.display='inline-table';
}
function hide(id){
	document.getElementById(id).style.display='none';
}

var plink="PropertyGallery.html";
var glink="GoldForm.html";
var loginCheck=0;
var track="";


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;
		}
		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
	}
	else {
		document.location=plink;
	}
}

function go(num){
	history.go(num);
}

function checkform(){
	var items=new Array("name","role","company_name","address","post_code","telephone","email","turnover","no_of_employees","products","volumes");
	var msg="";
	msg+=(document.siteform.name.value=="")?"Please enter your name.\n":"";
	msg+=(document.siteform.role.value=="")?"Please enter your title/role.\n":"";
	msg+=(document.siteform.company_name.value=="")?"Please enter your company name.\n":"";
	msg+=(document.siteform.address.value=="")?"Please enter your postal address.\n":"";
	msg+=(document.siteform.post_code.value=="")?"Please enter your post code.\n":"";
	msg+=(document.siteform.telephone.value=="")?"Please enter your contact number.\n":"";
	msg+=(!validEmail(document.siteform.email.value))?"Please enter a valid email address.\n":"";
	msg+=(document.siteform.turnover.value=="")?"Please enter your turnover.\n":"";
	msg+=(document.siteform.no_of_employees.value=="")?"Please enter your number of employees.\n":"";
	msg+=(document.siteform.products.value=="")?"Please enter the products requiring plasma enhancement.\n":"";
	msg+=(document.siteform.volumes.value=="")?"Please enter your potential annual volumes.\n":"";
	if(msg!=""){
		alert(msg);
		return false;
	}
	else 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
}



function showMenu(id, menObj, issub){
	sPage=id;
	//Highlight Main Menu items with no Sub Menus
	if(sPage==null&&sPage!='null'){
		var as=document.getElementsByTagName("A");
		for(i=0;i<as.length;i++){
			if(as[i].href==document.location&&as[i].parentNode.attributes['mid']){
				var menuid=as[i].parentNode.id.split('submenu');
				menObj=menuid[0].substring(3,menuid[0].length);
				if(window[menObj].mainstyles.bgimage_over!=""){
					as[i].parentNode.style.backgroundImage="url("+myDomain+"/images/"+window[menObj].mainstyles.bgimage_over+")";
					as[i].parentNode.onmouseover="";
					as[i].parentNode.onmouseout="";
				}
			}
		}
		return;
	}

	//Close unactive menus
	var tabs=document.getElementsByTagName('TABLE');
	for(i=0;i<tabs.length;i++){
		if(tabs[i].id.substring(0,8)=="m_tplate"&&tabs[i].id!=id){
			tabs[i].style.display="none";
		}
	}
	//Open/Close the active menu
	document.getElementById(sPage).style.display=(document.getElementById(sPage).style.display=='none')?((n_o_win)||(n_o_mac))?'inline-table':'inline':'none';

	if(window[menObj].mainstyles.bgimage_over!=""){
		var tds=document.getElementsByTagName("TD");
		for(i=0;i<tds.length;i++){
			if(tds[i].id){
				if(tds[i].id.indexOf('td_')>-1){
					tds[i].style.backgroundImage="url("+myDomain+"/images/"+window[menObj].mainstyles.bgimage_off+")";
					tds[i].onmouseover=function(){
						this.style.backgroundImage='url('+myDomain+'/images/'+window[menObj].mainstyles.bgimage_over+')';
					}
					tds[i].onmouseout=function(){
						this.style.backgroundImage='url('+myDomain+'/images/'+window[menObj].mainstyles.bgimage_off+')';
					}
				}
			}
		}
		document.getElementById('td_'+sPage).style.backgroundImage="url("+myDomain+"/images/"+window[menObj].mainstyles.bgimage_over+")";
		document.getElementById('td_'+sPage).onmouseover="return false;";
		document.getElementById('td_'+sPage).onmouseout="return false;";
	}

	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].parentNode.attributes['sid']){
			if(as[i].href.indexOf(myDomain)!=-1){
				if(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;
				}
			}
		}
		if(as[i].href==document.URL.toString()){
			if(as[i].parentNode.attributes['mid']){
				if(window[menObj].mainstyles.bgimage_over!=""){
					as[i].parentNode.style.backgroundImage="url('"+myDomain+"/images/"+window[menObj].mainstyles.bgimage_over+"')";
					as[i].parentNode.onmouseover="";
					as[i].parentNode.onmouseout="";
				}
				if(window[menObj].mainstyles.t_acol!="")as[i].style.color=window[menObj].mainstyles.t_acol;
			}
			else if(as[i].parentNode.attributes['sid']){
				if(window[menObj].substyles.bgimage_over!=""){
					as[i].parentNode.style.backgroundImage="url('"+myDomain+"/images/"+window[menObj].substyles.bgimage_over+"')";
					as[i].parentNode.onmouseover="";
					as[i].parentNode.onmouseout="";
				}
				if(window[menObj].substyles.t_acol!="")as[i].style.color=window[menObj].substyles.t_acol;
			}
		}
	}
}