/**

 * global.js - global javascript function library

 *

 * @copyright  (c) 2007 The Best of Samui Part. Ltd.

 * @author     Thomas Knierim <samuihotels@thomasknierim.com>

 * @version    0.1 2007-08-22

 * @since      0.1

 */



/**

 * Displays a confirmation dialogue box and loads the page at the given url

 * If no url is specified, the result of the confirmation is returned, i.e.

 * -1 for "no" and 1 for "yes".

 */

function acknowledge(message, url) {

	var result = false;

	if (confirm(message)) {

		result = true;

		if (typeof url != 'undefined') {

			document.location = url;

		}

	}

	return result;

}



/**

 * Switches the visibility of an element on or off.

 */

function toggleVisible(element_id) {

	var element = document.getElementById(element_id);

	var visible = element.style.display;

	element.style.display = (visible == 'none')? '' : 'none';

}



/*

 * Toggle display of default text in a text box when it receives/loses focus.

 */

function showDefaultText(textBox, defaultText, hasFocus) {

	if (hasFocus) {

		if (textBox.value == defaultText)

			textBox.value = '';

	}

	else {

		if (textBox.value == '')

			textBox.value = defaultText;

	}

}



/*

 * Displays the given url in a popup window.

 * All parameters except url are optional.

 */

function popup(url, windowName, width, height, left, top, status, resizable, scrollbars) {

	if (typeof windowName == 'undefined')

		windowName = 'New';

	if (typeof width == 'undefined')

		width = 600;

	if (typeof height == 'undefined')

		height = 400;

	if (typeof left == 'undefined')

		left = 100;

	if (typeof top == 'undefined')

		top = 100;

	if (typeof status == 'undefined')

		status = 'no';

	if (typeof resizable == 'undefined')

		resizable = 'yes';

	if (typeof scrollbars == 'undefined')

		scrollbars = 'yes';

	window.open(url, windowName, 'width=' + width + ',height=' + height + ',top=' + top + ',left=' + left + ',status=' + status + ',resizable=' + resizable + ',scrollbars=' + scrollbars);

}


// Everything bellow is for the live chat
// If server.php is not in the same folder with this page,
// make sure you fix this URL! For instance: /supportmelive/server.php

var server = "/livecustomersupport/server.php";

var req;

function chatEntryNewRequest()
{
	req = false;
	// For Safari, Firefox, and other non-MS browsers
	if (window.XMLHttpRequest) {
		try {
			req = new XMLHttpRequest();
		} catch (e) {
			req = false;
		} 
	} else if (window.ActiveXObject) {
		// For Internet Explorer on Windows
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				req = false;
			}
		}
	}
}

function chatEntryOpenChat()
{
    window.open('https://www.samuiislandhotels.com/livecustomersupport/index.html','LiveCustomerSupport','toolbar=no,status=no,scrollbars=no,location=yes,menubar=no,directories=no,width=800,height=800');
    return;
}

function chatEntryCheckAndShow()
{
	setTimeout('chatEntryCheckAndShow()', 60000);
	chatEntryNewRequest();
	req.onreadystatechange = chatEntryPollStateChange;
	req.open("POST", server, true);
	req.setRequestHeader('Content-Type', 
		'application/x-www-form-urlencoded');
	req.send("");
}

function chatEntryPollStateChange()
{
	if (req.readyState == 4) {
		try {
			if (req.status == 200) {
				var xml = req.responseXML;
				if (xml) {
					var is_ad = xml.getElementsByTagName("ad")[0].attributes.getNamedItem("isad").value;
					var el = document.getElementById('chatinvite');
					if (is_ad) {
							document.getElementById("chatinvite").innerHTML = "<strong>Live Support is Available</strong><br />Customer Support is ready to assist you.  Click <strong><a href=\"javascript:chatEntryOpenChat();\">here</a></strong> to open a chat window.";
					}
					else {
							document.getElementById("chatinvite").innerHTML = "<strong>Live Support is Unavailable</strong><br />Customer Support is currently offline. Please use our <strong><a href=\"https://www.samuiislandhotels.com/index.php?module=ContactUs&action=index\" title=\"Contact Us\">contact form</a></strong>.";
					}
					return;
				} else {
					alert(req.responseText);
				}
			}
		} catch (e) { }
		req = null;
	}
}

