function formatHTML(str) {
	if (str == null)
		return "";
		
	str = new String(str);
		
	str = str.replace(/"/g, "&quot;");
	str = str.replace(/</g, "&lt;");
	str = str.replace(/>/g, "&gt;");
	str = str.replace(/\r\n/g, "<br>");
	
	return str;
}
function emailCheck(emailStr)
{
	var checkTLD = 1;
	var knownDomsPat = /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat = /^(.+)@(.+)$/;
	var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars = "\[^\\s" + specialChars + "\]";
	var quotedUser = "(\"[^\"]*\")";
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom = validChars + '+';
	var word = "(" + atom + "|" + quotedUser + ")";
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray = emailStr.match(emailPat);

	if (matchArray == null)
	{
		alert("Email address " + emailStr + " seems incorrect (check @ and .'s)");
		return false;
	}

	var user = matchArray[1];
	var domain = matchArray[2];

	// Start by checking that only basic ASCII characters are in the strings (0-127).

	for (i = 0; i  <user.length; i++)
	{
		if (user.charCodeAt(i) > 127)
		{
			alert("Email address " + emailStr + " contains invalid characters.");
			return false;
		}
	}

	for (i = 0; i < domain.length; i++)
	{
		if (domain.charCodeAt(i) > 127)
		{
			alert("Email address " + emailStr + " contains invalid characters.");
			return false;
		}
	}

	// See if "user" is valid 

	if (user.match(userPat) == null)
	{
		// user is not valid

		alert("The username in " + emailStr + " doesn't seem to be valid.");
		return false;
	}

	var IPArray = domain.match(ipDomainPat);

	if (IPArray != null)
	{
		// this is an IP address

		for (var i = 1; i <= 4; i++)
		{
			if (IPArray[i] > 255)
			{
				alert("Destination IP address is invalid in " + emailStr + ".");
				return false;
			}
		}
		
		return true;
	}

	// Domain is symbolic name. Check if it's valid.
	var atomPat = new RegExp("^" + atom + "$");
	var domArr = domain.split(".");
	var len = domArr.length;

	for (i = 0; i < len; i++)
	{
		if (domArr[i].search(atomPat) == -1)
		{
			alert("The domain name does not seem to be valid in " + emailStr + ".");
			return false;
		}
	}
	if (checkTLD && domArr[domArr.length-1].length != 2 && domArr[domArr.length-1].search(knownDomsPat) == -1)
	{
		alert("The address " + emailStr + " must end in a well-known domain or two letter " + "country.");
		return false;
	}

	// Make sure there's a host name preceding the domain.

	if (len < 2)
	{
		alert("Email address " + emailStr + " is missing a hostname!");
		return false;
	}

	// If we've gotten this far, everything's valid!
	return true;
}

function trim(varStr) {
	return varStr.replace(/^\s+|\s+$/g,"");
}

function keyPressed(e, action){
	var keyPressed;
	if(window.event)
		keyPressed = window.event.keyCode; // IE
	else
		keyPressed = e.which; // Firefox

	if (keyPressed == 13) {
		if(action == "Search"){
			search();
		}
	}
}

function search()
{
	if(trim(document.getElementById("txtSearch").value).length == 0)
	{
		alert("You must enter your search keywords.");
		document.getElementById("txtSearch").focus();
		return false;
	}
	if(trim(document.getElementById("txtSearch").value).length < 3)
	{
		alert("Your search keywords must be at least 3 characters long");
		document.getElementById("txtSearch").focus();
		return false;
	}
	document.getElementById("frmSearch").submit();
}

function document_onload()
{
	if(document.getElementById("txtLoadMessage").value.length > 0)
	{
		alert(document.getElementById("txtLoadMessage").value);
	}
}

function sizeStdContent(){
	var x;
	var bottomOfPage = 0;
	var elements = document.getElementsByTagName('*');
	var bottomMostDiv, lastBottomOfPage;
	for (var i = 0; i < elements.length; i++) {
		x = elements[i];
		
		if (x.tagName == "DIV"){
			if (findCoordinate(x, "BOTTOM") > bottomOfPage){
				lastBottomOfPage = bottomOfPage;
				bottomOfPage = findCoordinate(x, "BOTTOM");
				bottomMostDiv = x;
			}
		}
	}
	
	var cellHeight;
	
	if(bottomMostDiv != null && bottomMostDiv.id == "divFooter")
	{
		if(bottomOfPage - lastBottomOfPage <= 56)
		{
			cellHeight = bottomOfPage - 264 - (bottomOfPage - lastBottomOfPage);
		}
		else
		{
			cellHeight = bottomOfPage - 302;
		}
	}
	else
	{
		cellHeight = bottomOfPage - 264;
	}
	
	try
	{
	   document.getElementById("tdContent").style.height = cellHeight + "px";
	}
	catch(ex)
	{
		return;
	}
	
	document_onload();
}

function sizeHomeContent(){
	var x;
	var bottomOfPage = 0;
	var elements = document.getElementsByTagName('*');
	var bottomMostDiv, lastBottomOfPage;
	for (var i = 0; i < elements.length; i++) {
		x = elements[i];
		
		if (x.tagName == "DIV"){
			if (findCoordinate(x, "BOTTOM") > bottomOfPage){
				lastBottomOfPage = bottomOfPage;
				bottomOfPage = findCoordinate(x, "BOTTOM");
				bottomMostDiv = x;
			}
		}
	}
	
	var cellHeight;
	
	if(bottomMostDiv != null && bottomMostDiv.id == "divFooter")
	{
		if(bottomOfPage - lastBottomOfPage <= 56)
		{
			cellHeight = bottomOfPage - 187 - (bottomOfPage - lastBottomOfPage);
		}
		else
		{
			cellHeight = bottomOfPage - 232;
		}
	}
	else
	{
		cellHeight = bottomOfPage - 187;
	}
	
	try
	{
	   document.getElementById("tdContent").style.height = cellHeight + "px";
	}
	catch(ex)
	{
		return;
	}
	
	document_onload();
}

function sizeContent(){
	if(navigator.userAgent.indexOf("MSIE 7.0") > 0){
		var x;
		var bottomOfPage = 0;
		var elements = document.getElementsByTagName('*');
		var bottomMostDiv, lastBottomOfPage;
		for (var i = 0; i < elements.length; i++) {
			x = elements[i];
			
			if (x.tagName == "DIV"){
				if (findCoordinate(x, "BOTTOM") > bottomOfPage){
					lastBottomOfPage = bottomOfPage;
					bottomOfPage = findCoordinate(x, "BOTTOM");
					bottomMostDiv = x;
				}
			}
		}
		
		var cellHeight;
		cellHeight = bottomOfPage - 232;
		
		try
		{
		   document.getElementById("tdContent").style.height = cellHeight + "px";
		}
		catch(ex)
		{
			return;
		}   
	}
}

function findCoordinate(obj, coordinate) {
	objLeft = obj.offsetLeft;
	objTop = obj.offsetTop;
	objParent = obj.offsetParent;

	while (objParent != null && objParent.tagName.toUpperCase() != "BODY")
	{
		objLeft += (objParent.offsetLeft - objParent.scrollLeft);
		objTop += (objParent.offsetTop - objParent.scrollTop);
		objParent = objParent.offsetParent;
	} 
	objRight = objLeft + obj.offsetWidth;
	objBottom = objTop + obj.offsetHeight;
	
	if (coordinate == "LEFT")
		return objLeft;
	else if (coordinate == "RIGHT")
		return objRight;
	else if (coordinate == "TOP")
		return objTop;
	else if (coordinate == "BOTTOM")
		return objBottom;	
}

function format(n, d) {
  with (Math) {

	var sign  = (n < 0) ? "-" : "";
	var num  = abs(n);

	 num = floor((num * pow(10, d + 1) + 5) / 10) / pow(10, d);
	 var whole = floor(num).toString();
	 var frac  = round((num - whole) * pow(10, d)).toString();
  }

  for ( ; frac.length < d ; ) frac = "0" + frac;
  
  return sign + whole + "." + frac;
}

function setFocus(element) {
	if (!element.disabled) {
		try {
			element.focus();
			if (element.type == "text") {
				element.select();
			}
		} catch (e) {}
	}
}

function getSelectTextValue(list) {
	return list.options[list.selectedIndex].text;
}

function getSelectValue(list) {
	return list.options[list.selectedIndex].value;
}


function openWindow(page, pagename, width, height, windowfeatures) {
	if (windowfeatures != "") {
		//windowfeatures = "," + windowfeatures + ",status";
		windowfeatures = "," + windowfeatures;
	}
	
	var left = screen.availWidth/2 - width/2;	
	var top = screen.availHeight/2 - height/2;	
		
	pagename = pagename.toString().replace(/[\s-&.]/g, "");

	var w  = window.open(page, pagename,"LEFT=" + left + ",TOP=" + top + ",HEIGHT=" + height + ",WIDTH=" + width + windowfeatures);	

	try {
		if (windowfeatures.indexOf('maximized') != -1) {
			w.moveTo(0,0);
			if (document.all) {
				w.resizeTo(screen.availWidth,screen.availHeight);
			} else if (document.layers||document.getElementById) {
				if (w.outerHeight < screen.availHeight||w.outerWidth < screen.availWidth) {
					w.outerHeight = screen.availHeight;
					w.outerWidth = screen.availWidth;
				}					
			}		
		}
	} catch(e) {}	
	return w;
}
		
function showPopUp(divID, e)
{ 
	hideAllPopUps();
	$('#'+divID).fadeIn();
}

function hidePopUp(divID, e) {
	$('#' + divID).fadeOut();
}

function hideAllPopUps()
{
	document.getElementById("divTravelInfo").style.display = "none";
	document.getElementById("divSignup").style.display = "none";
	document.getElementById("divStoreFinder").style.display = "none";
}
	
function refreshStoreInfo(){
	if(document.getElementById("cboStore").value != "-1")
	{
		jsrsExecute(Application["ApplicationPath"] + "/ServerScript/Store.aspx", refreshStoreInfo_Return, "GetStoreInfo", new Array("" + document.getElementById("cboStore").value));
	}
}
function refreshStoreInfo_Return(result){
	var storeInfo = "";
	storeInfo += "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
	storeInfo += "<tr><td class=\"OurStoresPopup\">Address:</td><td>" + formatHTML(result[0][3]) + "</td></tr>";
	storeInfo += "<tr><td class=\"OurStoresPopup\"></td><td>" + formatHTML(result[0][4]) + "</td></tr>";
	storeInfo += "<tr><td class=\"OurStoresPopup\"></td><td>" + formatHTML(result[0][5]) + "</td></tr>";
	storeInfo += "<tr><td class=\"OurStoresPopup\">Telephone:</td><td>" + formatHTML(result[0][1]) + "</td></tr>";
	storeInfo += "<tr><td class=\"OurStoresPopup\">Fax:</td><td>" + formatHTML(result[0][2]) + "</td></tr>";
	storeInfo += "<tr><td class=\"OurStoresPopup\">E-mail:</td><td><a href=\"mailto:" + formatHTML(result[0][0]) + "\" class=\"OurStores\">" + formatHTML(result[0][0]) + "</a></td></tr>";
	if(formatHTML(result[0][8]) != null && formatHTML(result[0][8]).length > 0)
	{
		storeInfo += "<tr><td class=\"OurStoresPopup\">Skype:</td><td>" + formatHTML(result[0][8]) + "</td></tr>";
	}
	storeInfo += "<tr><td class=\"OurStoresPopup\">Web:</td><td><a href=\"" + formatHTML(result[0][7]) + "\" target=\"_blank\" class=\"OurStores\">" + formatHTML(result[0][7]) + "</a></td></tr>";
	storeInfo += "</table>";
	document.getElementById("divStoreInfo").innerHTML = storeInfo;
}

function storeEnquiry(){
	if(document.getElementById("cboEnquireStore").value == "-1")
	{
		alert("Please pick a region.");
		document.getElementById("cboEnquireStore").focus();
		return false;
	}
	jsrsExecute(Application["ApplicationPath"] + "/ServerScript/Store.aspx", storeEnquiry_Return, "GetStoreInfo", new Array("" + document.getElementById("cboEnquireStore").value));
}

function storeEnquiry_Return(result){
	location.href='mailto:' + formatJS(result[0][0]) + '?Subject=Website Enquiry';
}

function formatJS(str) {
	if (str == null)
		return str;
		
	str = new String(str);
		
	str = str.replace(/\\/g, "\\\\");
	str = str.replace(/"/g, "\\\"");
	str = str.replace(/'/g, "\\\'");
	str = str.replace(/\r/g, "\\r");
	str = str.replace(/\n/g, "\\n");
	
	return str;
}

function newsletterSignup()
{
	if(trim(document.getElementById("txtName").value).length == 0)
	{
		alert("Please enter your name.");
		document.getElementById("txtName").focus();
		return;
	}
	if(trim(document.getElementById("txtEmail").value).length == 0)
	{
		alert("Please enter your email.");
		document.getElementById("txtEmail").focus();
		return;
	}
	if(!emailCheck(document.getElementById("txtEmail").value))
	{
		document.getElementById("txtEmail").focus();
		return;
	}
	
	document.getElementById("txtAction").value = "SIGNUP";
	document.getElementById("frmHead").submit();
}

function sendEmail()
{
	if(trim(document.getElementById("txtFromName").value).length == 0)
	{
		alert("Name is a required field.");
		document.getElementById("txtFromName").focus();
		return false;
	}
	if(trim(document.getElementById("txtFromEmail").value).length == 0)
	{
		alert("Email address is a required field.");
		document.getElementById("txtFromEmail").focus();
		return false;
	}
	if(!emailCheck(document.getElementById("txtFromEmail").value))
	{
		document.getElementById("txtFromEmail").focus();
		return false;
	}
	if(trim(document.getElementById("txtSendToEmail").value).length == 0)
	{
		alert("Send to email is a required field.");
		document.getElementById("txtSendToEmail").focus();
		return false;
	}
	if(!emailCheck(document.getElementById("txtSendToEmail").value))
	{
		document.getElementById("txtSendToEmail").focus();
		return false;
	}
	document.getElementById("txtAction").value = "SEND_EMAIL";
	document.getElementById("frmHead").submit();
}

