// Function which makes PNG transparency be handled correctly in Win IE 5.5 & 6.
//
// Arguments:
// - none -
function correctPNG() {
	var arVersion = navigator.appVersion.split("MSIE");
	var version = parseFloat(arVersion[1]);

	if ((version >= 5.5) && (document.body.filters)) {
		for(var i=0; i < document.images.length; i++) {
			var img = document.images[i];
			var imgName = img.src.toUpperCase();
			if (imgName.substring(imgName.length - 3, imgName.length) == "PNG") {
				var imgID = (img.id) ? "id='" + img.id + "' " : "";
				var imgClass = (img.className) ? "class='" + img.className + "' " : "";
				var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
				var imgStyle = "display: inline-block;" + img.style.cssText;
				
				if (img.align == "left") {
					imgStyle = "float: left;" + imgStyle;
				}
				if (img.align == "right") {
					imgStyle = "float: right;" + imgStyle;
				}
				if (img.parentElement.href) {
					imgStyle = "cursor: hand;" + imgStyle;
				}
				
				var strNewHTML = "<span " + imgID + imgClass + imgTitle	+ " style=\"" + "width: " + img.width + "px; height: " + img.height + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
				img.outerHTML = strNewHTML;
				i = i - 1;
			}
		}
		window.attachEvent("onload", correctPNG);
	}
}


// Function which creates and returns an XMLHttpRequest object
//
// Arguments:
// - none -
function createXMLHttpRequest() {
	/*try {
		netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
	} catch (e) {
		alert("Permission UniversalBrowserRead denied.");
	}*/

	try {
		// Firefox, Opera 8.0+, Safari
		return new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				return new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return null;
			}
		}
	}
}


// Function which defines how a language variable should be concatenated to a URL
//
// Arguments:
// @lang - string which refers to the requested language type
function setURLlangVar(lang) {
	// The current URL
	var currURL = document.location.href;

	// The host URL
	var currHost = document.location.hostname;
	var startOfHost = currHost.substr(0, 3);
	
	// The last three characters of the URL (e.g. "php")
	var endOfURL = currURL.substring((currURL.length - 3));

	// The place where the characters "langset=" could be
	var alreadyConcat = currURL.substr((currURL.length - 10), 8);
	
	// If the URL doesn't end in "php", it means that there are variables 
	// concatenated to the URL, so the & should be used for more variables, 
	// otherwise, it's the first variable and ? should be used
	if ((endOfURL != "php") && (endOfURL != "tml") && (startOfHost == "www")) {
		var langVar = "&langset=" + lang;
	} else {
		var langVar = "?langset=" + lang;
	}

	// If the current URL has "langset=" in it, this should not be 
	// added to the URL again, instead, only the value of the variable
	// should be modified in the URL, otherwise, add "langset=" as well
	if (alreadyConcat == "langset=") {
		// The URl, minus the language preference value
		var shortenedURL = currURL.substr(0, (currURL.length - 2));
		document.location.href = shortenedURL + lang;
	} else {
		document.location.href = currURL + langVar;
	}
}


// Functions which change the border color for elements depending on if
// the element has focus or not
//
// Arguments:
// @element - object which refers to the element which should be manipulated
function gainFocus (element) {
	$(element).setStyle('borderColor', '#6C9DBF');
}

function loseFocus (element) {
	$(element).setStyle('borderColor', '#1A4A7A');
}


// A function which creates a Google Maps marker icon with default values
function fCreateBackpackerIcon() {
	var baseIcon = new GIcon();
	baseIcon.iconSize = new GSize(32, 32);
	baseIcon.shadowSize = new GSize(56, 32);
	baseIcon.iconAnchor = new GPoint(16, 32);
	baseIcon.infoWindowAnchor = new GPoint(16, 0);
	
	return baseIcon;
}


// A function which uses regular expressions to make sure an 
// e-mail address is valid
//
// Arguments:
// @sElemId - a string which refers to the ID of an input field
function validate_email(sElemId) {
	var oElem = $(sElemId);

	// Call the emailCheck function and if it returns true,
	// follow suit; otherwise, return false
	if (emailCheck(oElem.value)) {
			return true;
	} else {
			return false;
	}
}


// A function which uses regular expressions to make sure an 
// e-mail address is valid
//
// Arguments:
// @sEmailStr - a string which contains the e-mail address to be validated
function emailCheck (sEmailStr) {
	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 = sEmailStr.match(emailPat);
	
	if (matchArray == null) {
		return false;
	}

	var user = matchArray[1];
	var domain = matchArray[2];
	
	if (user.match(userPat) == null) {
		return false;
	}

	var IPArray = domain.match(ipDomainPat)
	if (IPArray != null) {
			for (var i = 1; i <= 4; i++) {
				if (IPArray[i] > 255) {
					return false;
				}
			}
			return true;
	}
	
	var domainArray = domain.match(domainPat);
	if (domainArray == null) {
		return false;
	}

	var atomPat = new RegExp(atom, "g");
	var domArr = domain.match(atomPat);
	var len = domArr.length;
	
	if (domArr[domArr.length - 1].length < 2 || domArr[domArr.length - 1].length > 3) {
		return false;
	}

	if (len < 2) {
		return false;
	}
	return true;
}
