//*******************************************************************************
//																				*
//	Generic Javascript Utility Functions - By: Luc Mousseau						*
//	Mousseau Computer Consulting - Copyright (C)2006 - All Rights Reserved		*
//																				*
//*******************************************************************************

function getPopupContainer()
{
	if(!document.getElementById('popupContainer')) {
		var oContainer = document.createElement("div");
		oContainer.id = "popupContainer";
		document.body.appendChild(oContainer);
	}
	var oContainer = document.getElementById('popupContainer');
	return oContainer;
}

function PopUpSized(url, w, h, resize, scroll)
{
	// date used for a unique window name so that all new windows open up seperately
	var d = new Date()
	
	// center the window in the screen
	var L = (screen.availWidth - w) / 2;
	var T = (screen.availHeight - h) / 2;

	var newwindow = window.open(url, 'nw'+d.getTime(), config='height=' + h + ',width=' + w + ',top=' + T + ',left=' + L + ',toolbar=no,menubar=no,scrollbars=' + scroll + ',resizable=' + resize + ',location=no,directories=no,status=no');
	newwindow.focus();
}

function PopUpImage(imgUrl, wd, ht)
{
	//--
	//-- creates a popup window containing an image and sets the size of the window to the width and height of the image
	//--
	
	if(imgUrl == "") return;
	
	// date used for a unique window name so that all new windows open up seperately
	var d = new Date()
	
	var objImg = new Image();
	objImg.src = imgUrl;
		
	if(!isNaN(wd) && wd != null) {
		if(isNaN(ht) || ht == null) {
			var hscale = Math.ceil(objImg.height / (objImg.width / wd));
		}
		objImg.width = wd;
	}

	if(!isNaN(ht) && ht != null) {
		if(isNaN(wd) || wd == null) {
			var wscale = Math.ceil(objImg.width / (objImg.height / ht));
		}
		objImg.height =  ht;
	}

	var w = (wscale) ? wscale : objImg.width + 0;
	var h = (hscale) ? hscale : objImg.height + 0;
	var abort = (w == 0 || h == 0) ? 1 : 0;
	w = (w == 0) ? 400 : w;
	h = (h == 0) ? 250 : h;

	// center the window in the screen
	var L = (screen.availWidth - w) / 2;
	var T = (screen.availHeight - h) / 2;

	wd =  (isNaN(wd))?'':' width="'+wd+'"';
	ht =  (isNaN(ht))?'':' height="'+ht+'"';

	var nw = 'nw'+d.getTime();

	var pu = "/include/PopUpImage.asp?url=" + imgUrl + "&a=" + abort + "&w=" + w + "&h=" + h + "&wd=" + wd + "&ht=" + ht

	var newwindow = window.open(pu, nw, 'height=' + h + ',innerHeight=' + h + ',width=' + w + ',innerWidth=' + w + ',top=' + T + ',left=' + L + ',toolbar=no,menubar=no,scrollbars=0,resizable=1,location=no,directories=no,status=no');

	//newwindow.focus();
	/*
	newwindow.document.body.style.margin = "0px";
	newwindow.document.body.innerHTML = '<a href="javascript:void(0)" onclick="window.close()"><img src="' + objImg.src + '" ' + wd + ht + ' border="0"></a>';
	//newwindow.document.body.appendChild(objImg);
*/
}

function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

function trim(s)
{
	return (window.RegExp) ? String(s).replace(/^\s+|\s+$/g,"").replace(/\s+/g," ") : s;
}

function left(s, l)
{
	if(typeof(s) == "string" && typeof(l) == "number") {
		return s.substr(0, l);
	}
}

function right(s, l)
{
	if(typeof(s) == "string" && typeof(l) == "number") {
		return s.substr(s.length-l);
	}
}

function len(s)
{

return s.length;

}

function isValidEmail(sEmail)
{
	//-- Using Fancy Handy Dandy Regular Expressions to validate email addresses
	/*
	The name portion of the email (before the @) allows for all alphanumeric characters
	as well as some special characters with the exception of these:
		! @ # $ % ! ) ( ` , : ; > | < \ ] [ space
	
	There must be a @ character, and it cannot be immediately preceeded or followed by a dot.
	
	The address portion of the email (after the @) allows for all characters, in order to support IDNA (internationalized domain names).
	The Top Level Domain (.com, .net, etc) must exist at the end and must be alphabetical 
	characters with a minimum length of 2 characters.
	*/
	
	bRet = false;
	
	if(window.RegExp) {
		// regular expression validation of email address
		// it's complicated but takes all allowed formats into account
	
		var filter = /^([^!@#$%!)(`,:;>|<\\\]\[\s]*)+@[^\.][^@]*\.([a-z]|[A-Z]){2,}$/;
	
		if(sEmail.match(filter)) {
			bRet = true;
		}
	} else {
		// for browser backwards compatibility. basic check: 
		// 1-		@ exists and is not first character
		// 2-		@ is in the email only once
		// 3-		. (dot) exists after the @
		// 4-		no . (dot) immediately adjacent to the @ character
		// 5-		TLD is at least two characters (dot is at least three from end)
		
		if(	sEmail.indexOf("@") > 0 
			&& sEmail.indexOf("@") == sEmail.lastIndexOf("@")
			&& sEmail.charAt(sEmail.indexOf("@")+1) != "."
			&& sEmail.charAt(sEmail.indexOf("@")-1) != "."
			&& sEmail.lastIndexOf(".") > sEmail.indexOf("@") 
			&& (sEmail.lastIndexOf(".")) <= (sEmail.length-3)
		) bRet = true;
	}
	
	return bRet;
	
}

function escapeQuotes(s)
{
	
	s = s.replace(/[\']/gi, "\\\'");
	s = s.replace(/[\"]/gi, '\\\"');

	return s;
}



