var lastMouseX;
var lastMouseY;
var curPopupWindow = null;

curDate = new Date();
in12Years = new Date(curDate.getFullYear() + 15, 1, 1);

/*========================================================================================
' Application specific functions
'========================================================================================*/

function GeneratePassword(){
	var strPassword = "";
	var c = "";

	for(var i=0;i<7;i++){
		if(i<4){
			c = String.fromCharCode(Math.round((90-65) * Math.random()) + 65)
			if(!(c=="A" || c=="E" || c=="I" || c=="O" || c=="U")){
				strPassword += c
			} else {
				i--;
			}
		} else {
			strPassword += String.fromCharCode(Math.round((57-48) * Math.random()) + 48)
		}
	}
	
	return strPassword;
}

/*========================================================================================
' Window functions
'========================================================================================*/

window.onresize = function()
{
	if(document.all['tools'])
	{
		document.all['tools'].style.visibility = 'hidden';
	}
}

function openPopup(url, name, pWidth, pHeight, features, snapToLastMousePosition) {
   closePopup();
	if (snapToLastMousePosition) {
		if (lastMouseX - pWidth < 0) {
			lastMouseX = pWidth;
		}
		if (lastMouseY + pHeight > screen.height) {
			lastMouseY -= (lastMouseY + pHeight + 50) - screen.height;
		}
                lastMouseX -= pWidth;
                lastMouseY += 10;
		features +=	"screenX=" + lastMouseX + ",left=" + lastMouseX + "screenY=" + lastMouseY + ",top=" + lastMouseY;
	}
	curPopupWindow = window.open(url, name, features, false);

}

function closePopup() {
	if (curPopupWindow != null) {
	   
		if (!curPopupWindow.closed) {
			curPopupWindow.close();
		}
		curPopupWindow = null;
	}
}

function checkEnter(event,frmName,strFunctionCall)
{
	var NS4 = (document.layers) ? true : false; 	
	var code = 0;
	
	if (NS4)
		code = event.which;
	else
		code = event.keyCode;
	if (code==13){
		if(frmName!=""){
			document.forms[frmName].submit();
		} else {
			eval(strFunctionCall);
		}
	}
}

/*========================================================================================
' Text functions
'========================================================================================*/

// removes the leading and trailing spaces from a string, 
// similar to the java.lang.String.trim() function
// added by lturetsky, taken from http://www.voy.com/1888/58.html
function trim(st) {
	var len = st.length
	var begin = 0, end = len - 1;
	while (st.charAt(begin) == " " && begin < len) {
		begin++;
	}
	while (st.charAt(end) == " " && begin < end) {
		end--;
	}
	return st.substring(begin, end+1);
}

/*========================================================================================
' Math functions
'========================================================================================*/

function roundAccuracy(num, accuracy){
    var factor=Math.pow(10,accuracy);
    return Math.round(num*factor)/factor;
}

/*========================================================================================
' Generic functions
'========================================================================================*/

function parseQueryToArray(queryString) {
	var results = new Array();
	if (queryString != '') 
	{
		var srchArray = queryString.split("&");
		var tempArray = new Array();
		for (i = 0; i < srchArray.length; i++) 
		{
			tempArray = srchArray[i].split("=");
			results[unescape(tempArray[0])] = unescape(tempArray[1]);
		}
	} 
	return results;
}

function findOption(field, value)
{
	for(i = 0; i < field.options.length; i++)
	{
		if(field.options[i].value == value)
		{ 
			return i;
		}
	}
	return -1;
}
 
function setLastMousePosition(e) {
	if (navigator.appName.indexOf("Microsoft") != -1) e = window.event;
	lastMouseX = e.screenX;
	lastMouseY = e.screenY;
}


/*========================================================================================
' Cookie functions
'========================================================================================*/
function setCookie (name, value) 
{
     var argv = setCookie.arguments;
     var argc = setCookie.arguments.length;
     var expires = (argc > 2) ? argv[2] : null;
     var path = (argc > 3) ? argv[3] : null;
     var domain = (argc > 4) ? argv[4] : null;
     var secure = (argc > 5) ? argv[5] : false;
     document.cookie = name + "=" + escape (value) +
       ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
       ((path == null) ? "" : ("; path=" + path)) +
       ((domain == null) ? "" : ("; domain=" + domain)) +
       ((secure == true) ? "; secure" : "");
}

function getCookieVal(offset)
{
   var endstr = document.cookie.indexOf( ";", offset );
   if( endstr == -1 )
       endstr = document.cookie.length;
   return unescape( document.cookie.substring(offset, endstr) );
}

function getCookie(name)
{
	var arg = name+"=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	var j = 0;
	while( i < clen )
	{
		j = i + alen;
		if (document.cookie.substring( i, j ) == arg)
			return getCookieVal( j );
		i = document.cookie.indexOf( " ", i ) + 1;
		if( i == 0 )
			break;
	} return null;
}
function moveFields(objFrom,objTo,bAll){
	var strName="";
	var strValue="";
	var vOption;

	for(var i=0;i<objFrom.options.length;i++){
		if(objFrom.options[i].selected || bAll){
			strName = objFrom.options[i].text;
			strValue = objFrom.options[i].value;
			objFrom.options[i]=null;
			
			vOption = new Option(strName,strValue);
			objTo.options[objTo.options.length]=vOption;
			
			i--;
		}
	}
}
