var defaultSize		= 12;
var resizeCounter	= 0;
var increment		= 2;
var maxsize			= 18;
var minsize			= 8;
var currentID		= "";
var oldText			= "";
var currentImg		= 1;
var preloadImages	= new Array();
var currentImages	= new Array();
var rotateDiv		= 'rightColumn';
var fadeDiv			= 'fadeImage';
var currentDiv		= '';
var startOpacity	= .0;
var startFade		= 0;
var outOpacity		= 1;
var inOpacity		= 0;
var player			= null; 
var mailSubmit		= false;
var savedPhrase		= '';

///////////////////// Beyond here be functions ////////////////

function toggle(ID)
{
	if(ID == currentID)
	{
		min(ID);
	}
	else
	{
		if(currentID != "")
		{
			min(currentID);
		}
		max(ID);
	}
	return false;
}

function toggleSound()
{
	var soundImg	= $('soundToggle');
	var startSound	= readCookie('startSound');
	if(startSound == 'false')
	{
		newValue	= 'true';
		soundImg.src	= 'http://'+location.host+'/graphics/phuket_beach_club_sound.gif';
	}else{
		newValue	= 'false';
		soundImg.src	= 'http://'+location.host+'/graphics/phuket_beach_club_sound_off.gif';
	}
	createCookie('startSound', newValue, 365);
	player.sendEvent('PLAY');
	return false;
}
function createPlayer()
{
	var startValue = readCookie('startSound');
	if(startValue != 'false')
	{
		startValue	= 'true';
	}
	
	var flashvars = 
	{
		duration: 33,
		file: "/media/phuket_beach_club_sound.mp3",
		image:	"/graphics/phuket_beach_club_sound.gif",
		frontcolor: "FFFFFF",
		lightcolor: "FFFFFF",
		screencolor: "FFFFFF",
		controlbar:	"none",
		autostart: startValue,
		displayclick: "play",
		repeat: "none"
	}
	var params = 
	{
		allowfullscreen:"false", 
		allowscriptaccess:"always"
	}
	var attributes =
	{
		id:"mpl",
		name:"mpl"
	}
	swfobject.embedSWF("/scripts/player.swf", "sound", "0", "0", "9.0.115", false, flashvars, params, attributes);
}

function playerReady()
{
	player = window.document['mpl'];
}

function min(categoryID)
{
	var obj		= $(categoryID);
	obj.style.display = 'none';
	currentID	= "";
}

function max (categoryID)
{
	var obj		= $(categoryID);
	obj.style.display = 'block';
	currentID	= categoryID;
}

function verifyForm(pgForm)
{
	var errMsgs		= new Array();
	var reqMsgSet	= false;
	for(var i = 0; i < pgForm.elements.length; i++)
	{
		if(pgForm.elements[i].name == 'user_name')
		{
			if((pgForm.elements[i].value.length < 6)||(pgForm.elements[i].value.length > 12))
			{
				if(!alertMsg){var alertMsg	= "User names must be 6 - 12 characters long.";}
				else{alertMsg = alertMsg+"\nUser names must be 6 - 12 characters long.";}
				errMsgs.push(pgForm.elements[i].name);
			}
		}
		if((pgForm.elements[i].name == 'email')||(pgForm.elements[i].name == 'senderEmail'))
		{
			if(isValidEmail(pgForm.elements[i].value) == false)
			{
				if(!mailMsgSet)
				{
					if(!alertMsg){var alertMsg	= "A valid Email address is required!";}
					else{alertMsg = alertMsg+"\nA valid Email address is required!";}
					var mailMsgSet	= true;
				}
					errMsgs.push(pgForm.elements[i].name);
			}
			var priEmail	= pgForm.elements[i].value;
		}
		if(pgForm.elements[i].name == 'user_pass')
		{
			if(isValidPass(pgForm.elements[i].value) == false)
			{
				if(!alertMsg){var alertMsg	= "A password is required and may contain only Numbers, letters and these characters; @#$!%&";}
				else{alertMsg = alertMsg+"\nA password is required may contain only Numbers, letters and these characters; @#$!%&";}
				errMsgs.push(pgForm.elements[i].name);
				
			}else{
				var userPass	= pgForm.elements[i].value;
			}
		}
		if(pgForm.elements[i].name == 'verify_pass')
		{
			if(pgForm.elements[i].value != userPass)
			{
				if(!alertMsg){var alertMsg	= "Passwords do not match!";}
				else{alertMsg = alertMsg+"\nPasswords do not match!";}
				errMsgs.push(pgForm.elements[i].name);
			}
		}
		if(pgForm.elements[i].getAttribute('number'))
		{
			var numVal		= pgForm.elements[i].getAttribute('number');
			var	shoVal		= numVal.replace(/,/, " - ");
			var regExpObj	= new RegExp("^\\d{"+numVal+"}$");
			if(pgForm.elements[i].getAttribute('required') == '1') 
			{
				if(regExpObj.exec(pgForm.elements[i].value) == null)
				{
					if(!alertMsg){var alertMsg	= "Field "+pgForm.elements[i].name+" must be "+shoVal+" digits only";}
					else{alertMsg += "\nField "+pgForm.elements[i].name+" must be "+shoVal+" digits only";}
					errMsgs.push(pgForm.elements[i].name);
				}
			}else{
				if((pgForm.elements[i].value.search(regExpObj) == -1) && (pgForm.elements[i].value.length > 0))
				{
					if(!alertMsg){var alertMsg	= "Field "+pgForm.elements[i].name+" must be "+shoVal+" digits only";}
					else{alertMsg += "\nField "+pgForm.elements[i].name+" must be "+shoVal+" digits only";}
					errMsgs.push(pgForm.elements[i].name);
				}
			}
		}
		if(pgForm.elements[i].className == 'required')
		{
			if(pgForm.elements[i].value == '')
			{
				if(!reqMsgSet)
				{
					if(!alertMsg){var alertMsg	= "Required Field(s) blank.";}
					else{alertMsg = alertMsg+"\nRequired Field(s) blank.";};
					reqMsgSet	= 1
				}
				if(pgForm.elements[i].type != 'radio')
				{
					errMsgs.push(pgForm.elements[i].name);
				}
			}
		}
	}
	if(errMsgs.length > 0)
	{
		alert(alertMsg+'\nPlease correct highlighted fields.');
		for(var j = 0; j < errMsgs.length; j++)
		{
			var fieldName	= errMsgs[j];
			pgForm.elements[fieldName].style.background = '#fff3c8';
		}
		return false;
	}
	return true;
}
function mailListInput(serverScript, divID)
{
	targetDiv	= $(divID);
	oldText		= targetDiv.innerHTML;
	targetDiv.innerHTML	= "<input name=\"email\" id=\"emailInput\" type=\"text\" />";
	emailInput	= $('emailInput');
	document.subscribeform.emailInput.focus();
/*	emailInput.onkeypress = function (event) 
	{
		event = event || window.event;
		if (event.keyCode == 13)
		{
			mailListSubmit(serverScript, divID)
			return false;
		}
	}
*/
	return false;
}

function mailListSubmit(serverScript, divID)
{
	targetDiv	= $(divID);
	if(oldText != "")
	{
		inputDiv	= $('emailInput');
		alert("You entered:\n\""+inputDiv.value+"\"");
		targetDiv.innerHTML	= oldText;
		targetDiv.style.padding = '0px';
		oldText	= "";
	}
	return false;
}

function rotateBG()
{
	currentDiv	= rotateDiv;
	$(fadeDiv).style.opacity	= inOpacity;
	$(fadeDiv).style.filter	= 'alpha(opacity:'+(inOpacity*100)+')';

	$(rotateDiv).style.opacity	= outOpacity;
	$(rotateDiv).style.filter	= 'alpha(opacity:'+(outOpacity*100)+')';
	
	$(rotateDiv).style.background = "url('"+preloadImages[currentImg]+"')";
	$(rotateDiv).style.backgroundPosition = "top left";
	fadeInterval	= setTimeout("nextImage()", 5000);
}

function fadeIn(inDiv, outDiv)
{
	inOpacity	= inOpacity + .025;
	$(inDiv).style.opacity	= inOpacity;
	$(inDiv).style.filter	= 'alpha(opacity:'+(inOpacity*100)+')';
	outOpacity	= outOpacity - .025;
	$(outDiv).style.opacity	= outOpacity;
	$(outDiv).style.filter	= 'alpha(opacity:'+(outOpacity*100)+')';
	if(outOpacity <= 0)
	{
		if(currentDiv == rotateDiv)
		{
			$(outDiv).src = preloadImages[currentImg];
		}else{
			$(outDiv).style.backgroundImage = "url('"+preloadImages[currentImg]+"')";
			$(outDiv).style.backgroundPosition = "top left";
		}
	}
	if(inOpacity >= 1)
	{
		clearInterval(fadeInInterval);
		fadeInterval	= setTimeout("nextImage()", 2000);
	}
}

function nextImage()
{
	outOpacity	= 1;
	inOpacity	= 0;
	if(currentDiv == rotateDiv)
	{
		outDiv	= rotateDiv;
		inDiv	= fadeDiv;
		currentImg++;
		if(currentImg == preloadImages.length)
		{
			currentImg = 0;
		}
		$(fadeDiv).src = preloadImages[currentImg];
		currentDiv	= fadeDiv;
	}
	else if(currentDiv == fadeDiv)
	{
		outDiv	= fadeDiv;
		inDiv	= rotateDiv;
		currentImg++;
		if(currentImg == preloadImages.length)
		{
			currentImg = 0;
		}
		$(rotateDiv).style.background = "url('"+preloadImages[currentImg]+"') top left no-repeat";
		currentDiv	= rotateDiv;
	}
	fadeInInterval	= setInterval("fadeIn(inDiv, outDiv)", 50);
	
}

function postform(commentForm, serverScript, targetDiv)
{
	var errMsgs		= new Array();
	var reqMsgSet	= false;
	commentReq		= new xmlRequest();
	var formSubmit	= null;
	targetObj		= $(targetDiv);
	for(var i = 0; i < commentForm.elements.length; i++)
	{
		if(!formSubmit)
		{
			if((commentForm.elements[i].type == 'checkbox')||(commentForm.elements[i].type == 'radio'))
			{
				if(commentForm.elements[i].checked){formSubmit = commentForm.elements[i].name+'='+escape(commentForm.elements[i].value);}
			}else{
				var fieldData	= commentForm.elements[i].value.replace("&", "AMP");
				formSubmit		= commentForm.elements[i].name+'='+escape(fieldData);
			}
			if((commentForm.elements[i].value == '')&&(commentForm.elements[i].type != 'select-one'))
			{
				errMsgs.push(commentForm.elements[i].name);
			}
		}else{
			if((commentForm.elements[i].type == 'checkbox')||(commentForm.elements[i].type == 'radio'))
			{
				if(commentForm.elements[i].checked){formSubmit += '&'+ commentForm.elements[i].name+'='+escape(commentForm.elements[i].value);}
			}else{
				var fieldData	= commentForm.elements[i].value.replace("&", "AMP");
				formSubmit		+= '&'+commentForm.elements[i].name+'='+escape(fieldData);
			}
			if((commentForm.elements[i].value == '')&&(commentForm.elements[i].type != 'select-one'))
			{
				errMsgs.push(commentForm.elements[i].name);
			}
		}
		if(commentForm.elements[i].name == 'email')
		{
			if(isValidEmail(commentForm.elements[i].value) == false)
			{
				errMsgs.push(commentForm.elements[i].name);
			}
		}
		if(commentForm.elements[i].getAttribute('required') == '1')
		{
			if(commentForm.elements[i].value == '')
			{
				errMsgs.push(commentForm.elements[i].name);
			}
		}
	}
	if(errMsgs.length > 0)
	{
		alert('ERROR: All fields are required');
		for(var i = 0; i < errMsgs.length; i++)
		{
			var fieldName	= errMsgs[i];
			commentForm.elements[fieldName].style.background = '#fff3c8';
		}
		return false;
	}
	
	commentReq.open("POST", serverScript, true);
	commentReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	commentReq.onreadystatechange = function()
	{
		if (commentReq.readyState == 4 && commentReq.status == 200)
		{
			if(commentReq.responseText.substr(0, 6) == "ERROR:")
			{
				alert(commentReq.responseText);
			}
			else if(commentReq.responseText.substr(0,2) == "OK")
			{
				targetObj.innerHTML = commentReq.responseText.substr(3);
			}
		}
	}
	commentReq.send(formSubmit);
	return false;
}

function commentForm(serverScript, targetID)
{
	
	formReq		= new xmlRequest();
	targetObj	= $(targetID);
	formReq.open("GET", serverScript, true);
	formReq.onreadystatechange = function()
	{
		if (formReq.readyState == 4 && formReq.status == 200)
		{
			oldText	= targetObj.innerHTML;
			targetObj.innerHTML = formReq.responseText;
		}
	}
	formReq.send(null);
	return false;
	
}

function showLetters(serverScript, targetID)
{
	formReq		= new xmlRequest();
	targetObj	= $(targetID);
	formReq.open("GET", serverScript, true);
	formReq.onreadystatechange = function()
	{
		if (formReq.readyState == 4 && formReq.status == 200)
		{
			oldText	= targetObj.innerHTML;
			targetObj.innerHTML = formReq.responseText+"<br /><a href=\"http://"+location.host+"\" onclick=\"return closeForm('"+targetID+"');\">Close Letters</a>";
		}
	}
	formReq.send(null);
	return false;
	
}

function closeForm(targetID)
{
	targetObj	= $(targetID);
	targetObj.innerHTML	= oldText;
	return false;
}

function isValidEmail(email)
{
	email	= email.trim();
	if(email.search(/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i) == -1)
	{
		return false;
	}else{
		return true;
	}
}

function isValidPass(pass)
{
	pass	= pass.trim();
	if(pass.search(/[-a-zA-Z0-9]/) == -1)
	{
		return false;
	}else{
		return true;
	}
}
function $(id) 
{
	return document.getElementById(id);
}

String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function()
{
	return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function()
{
	return this.replace(/\s+$/,"");
}

function xmlRequest()
{
	try
	{
		xmlHttp=new XMLHttpRequest();
	}catch(e){
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}catch (e){
				alert("Your browser is quite out-dated and does not support AJAX!");
			}
		}
	}
	return xmlHttp;
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function imgkeep(id) 
{
	$('imgFile-' + id).disabled = true;
}

function imgdelete(id) 
{
	$('imgFile-' + id).disabled = true;
}

function imgchange(id) 
{
	$('imgFile-' + id).disabled = false;
}

function getElementbyClass(rootobj, classname)
{
	var temparray=new Array()
			var inc=0
			var rootlength=rootobj.length
			for (i=0; i<rootlength; i++)
	{
		if (rootobj[i].className==classname){temparray[inc++]=rootobj[i];}
	}
	return temparray
}

function Cookie(name)
{
	this.$name = name;  // Remember the name of this cookie
	var allcookies = document.cookie;
	
	if (allcookies == ""){return;}
	
	var cookies = allcookies.split(';');
	var cookie = null;
	
	for(var i = 0; i < cookies.length; i++)
	{
		cookies[i]	= cookies[i].trim();

		 // Does this cookie string begin with the name we want?
		if(cookies[i].substring(0, name.length+1) == (name + "="))
        	{
			cookie = cookies[i];
			break;
		}
	}

	if (cookie == null){return;}
	var cookieval = cookie.substring(name.length+1);

	var a = cookieval.split('&'); // Break it into an array of name/value pairs
	for(var i=0; i < a.length; i++)
	{
		a[i] = a[i].split(':');
	}

	for(var i = 0; i < a.length; i++)
	{
		this[a[i][0]] = decodeURIComponent(a[i][1]);
	}
}

Cookie.prototype.store = function(daysToLive, path, domain, secure)
{
	var cookieval = "";
	for(var prop in this)
	{
        
		if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')){continue;}
		if (cookieval != ""){cookieval += '&';}
		cookieval += prop + ':' + encodeURIComponent(this[prop]);
	}

	var cookie = this.$name + '=' + cookieval;
	if (daysToLive || daysToLive == 0)
	{
		var date = new Date();
		date.setTime(date.getTime()+(daysToLive*24*60*60*1000));
		cookie += ";expires="+date.toGMTString();
	}

	if (path){cookie += ";path=" + path;}
	if (domain){cookie += ";domain=" + domain;}
	if (secure){cookie += ";secure";}
	
	document.cookie = cookie;
}

Cookie.prototype.remove = function(path, domain, secure)
{
    // Delete the properties of the cookie
    for(var prop in this)
	{
		if (prop.charAt(0) != '$' && typeof this[prop] != 'function')
		delete this[prop];
	}

	// Then, store the cookie with a lifetime of 0
	this.store(0, path, domain, secure);
}

Cookie.enabled = function( )
{

	if (navigator.cookieEnabled != undefined) return navigator.cookieEnabled;
	if (Cookie.enabled.cache != undefined) return Cookie.enabled.cache;

	document.cookie = "testcookie=test; max-age=10000";  // Set cookie

    // Now see if that cookie was saved
	var cookies = document.cookie;
	if(cookies.indexOf("testcookie=test") == -1)
	{
        // The cookie was not saved
        	return Cookie.enabled.cache = false;
	}else{
		// Cookie was saved, so we've got to delete it before returning
		document.cookie = "testcookie=test; max-age=0";  // Delete cookie
		return Cookie.enabled.cache = true;
	}
}

function bookmarkPage(url, title)
{
	if (window.sidebar)
	{
		alert("Please press Ctrl+D to bookmark this page.");
	}
	else if(window.opera && window.print)
	{
		alert("Please press Ctrl+T to bookmark this page.");
	}
	else if(window.external)
	{
		window.external.AddFavorite(url, title);
	}
	return false;
}

function varitext(text)
{
	text=document;
	print(text);
	return false;
}

function clearEmailPrompt(fieldID, phrase)
{
	var emailField = $(fieldID);
	if (emailField.value == phrase) 
	{
		savedPhrase	= phrase
		emailField.value = "";
	}
}

function setEmailPrompt(fieldID, phrase) 
{
	var emailField = document.getElementById(fieldID);
	if (emailField.value == "") 
	{
		emailField.value = savedPhrase;
		savedPhrase	= '';
	}
}

function clearMessagePrompt(fieldID, phrase)
{
	var message = document.getElementById(fieldID);
	if (message.value == phrase)
	{
		message.value = "";
	}
}


function setMessagePrompt() 
{
	var message = document.getElementById("contactMsg");
	if(message.value == "")
	{
		message.value = "Your Message...";
	}
}

function clearSecCodePrompt(fieldID, fieldPrompt)
{
	var secCode = document.getElementById(fieldID);
	if (secCode.value == fieldPrompt)
	{
		secCode.value = "";
	}
}


function setSecCodePrompt(fieldID, fieldPrompt) 
{
	var secCode = document.getElementById(fieldID);
	if(secCode.value == "")
	{
		secCode.value = fieldPrompt;
	}
}


window.onload = function()
{
	if($('preload'))
	{
		preloadDiv = $('preload')
		for(i = 0; i < preloadDiv.childNodes.length; i++)
		{
			if(preloadDiv.childNodes[i].tagName == 'IMG')
			{
				preloadImages.push(preloadDiv.childNodes[i].src);
			}
		}
		if(document.images)
		{
			for(i=0; i>preloadImages.length; i++)
			{
				image[i]		= new Image();
				image[i].src	= preloadImages[i];
			}
		}
	}
	if($('fadeImage'))
	{
		rotateBG();
	}
	var soundImg	= $('soundToggle');
	var startVal	= readCookie('startSound');
	if(startVal == 'false')
	{
		soundImg.src	= 'http://'+location.host+'/graphics/phuket_beach_club_sound_off.gif';
	}
	createPlayer();
	playerReady();
	var nVer = navigator.appVersion;
	var nAgt = navigator.userAgent;
	var browserName  = '';
	var fullVersion  = 0; 
	var majorVersion = 0;
	if((verOffset=nAgt.indexOf("MSIE"))!=-1) 
	{
		browserName  = "Microsoft Internet Explorer";
		fullVersion  = parseFloat(nAgt.substring(verOffset+5));
		majorVersion = parseInt(''+fullVersion);
	}
	else if((verOffset=nAgt.indexOf("Opera"))!=-1)
	{
		browserName  = "Opera";
		fullVersion  = parseFloat(nAgt.substring(verOffset+6));
		majorVersion = parseInt(''+fullVersion);
	}
	else if((verOffset=nAgt.indexOf("Firefox"))!=-1)
	{
		browserName  = "Firefox";
		fullVersion  = parseFloat(nAgt.substring(verOffset+8));
		majorVersion = parseInt(''+fullVersion);
	}
	else if((nameOffset=nAgt.lastIndexOf(' ')+1) < (verOffset=nAgt.lastIndexOf('/')) )
	{
		browserName  = nAgt.substring(nameOffset,verOffset);
		fullVersion  = parseFloat(nAgt.substring(verOffset+1));
		if (!isNaN(fullVersion)) majorVersion = parseInt(''+fullVersion);
		else {fullVersion  = 0; majorVersion = 0;}
	}

	if((browserName == 'Microsoft Internet Explorer') && (fullVersion < 7))
	{
		startList();
	}
}