//This file is used as a library of common javascript functions. 
//the namespace is "jsUtilities"

var jsUtilities = {};

//PopWindow: used to display a pop-up window with the fix of positioning problem.
//works at least in Firefox and IE
jsUtilities.PopWindow = function(url,WinName,strWidth,strHeight,popWin,extra)
{
	var IE = (document.all) ? true : false;
    var IntPosLeft;
    var IntPosTop;
    if (IE==true)
    {
    	IntPosLeft = (window.document.body.clientWidth - parseInt(strWidth))/2 + window.screenLeft;
    	IntPosTop = (window.document.body.clientHeight - parseInt(strHeight))/2 + window.screenTop;
	}
	else
	{
		IntPosLeft = (window.innerWidth - parseInt(strWidth))/2 + window.screenX;
		IntPosTop = (window.innerHeight - parseInt(strHeight))/2 + window.screenY;
	}
	if (popWin != null)
	    popWin.close();

	if (extra==null || extra=="")
		extra = "status=no,toolbar=no";
		
	popWin = window.open(url,WinName,'width=' + strWidth + ',height=' + strHeight + ',left=' + IntPosLeft + ',top=' + IntPosTop + ',' + extra);
    popWin.focus();
}

jsUtilities.CloseWithoutWarning = function ()
{
	if (window.XMLHttpRequest) {
		window.open('','_self','');
	}else{
		window.opener=self;
	}
	window.close();
}

jsUtilities.DecodeIllegal = function (strInput)
{
  return strInput.replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&amp;/g,"&").replace(/&apos;/g,"'").replace(/&quot;/g,"\"");
}


jsUtilities.getCookie = function (cookieName) {
   if(document.cookie)
   {
       //alert(document.cookie);
       index = document.cookie.indexOf(cookieName);
       if (index != -1)
       {
            namestart = (document.cookie.indexOf("=", index) + 1);
            nameend = document.cookie.indexOf(";", index);
            if (nameend == -1) {nameend = document.cookie.length;}
            data = document.cookie.substring(namestart, nameend);
            //alert(data);
            return data;
        }
        else
        	return "";
    }
    return "";
}


jsUtilities.putCookie = function(cookieName,aString) { 
	if(document.cookie != document.cookie) {
		index = document.cookie.indexOf(cookieName);}
	else { 
		index = -1;
	}

	if (index == -1)
	{
		data=aString; // document.cf.cfd.value;
		//document.cookie=cookieName+"="+data+";" +" path=/;";
		//alert("putting cookie with data="+data);
		var date = new Date();
		date.setTime(date.getTime()+(1*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
		document.cookie = cookieName+"="+data+expires+"; path=/";
		//alert("cookies is="+document.cookie);
	}
}

jsUtilities.deleteCookie = function(name, path, domain) {
  if (getCookie(name)) {
        var d = new Date();
        d.setFullYear(1970,1,1);
	    document.cookie = name + "=" + 
	    ((path) ? "; path=" + path : "") +
	    ((domain) ? "; domain=" + domain : "") +
	    "; expires=" + d.toGMTString() + ";";
  }
}


jsUtilities.isNumberKey = function(e)
{
	var charCode = (e.which) ? e.which : e.keyCode
	if (charCode ==8 || charCode == 13 || (charCode >= 48 && charCode <= 57))
	   return true;
	else
		return false;
}

jsUtilities.isNumberKeyPhone = function(e)
{
	var charCode = (e.which) ? e.which : e.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57 ))
		if( charCode!=45&&(charCode!=40 && charCode!=41))
			 return false;
	return true;
}

jsUtilities.setDate = function(date,region){
	if(region =="en_US" ||region =="en_US"){
		return date;
	}
	if(region =="en_GB"){
		date = date.substring(3, 5)+"/"+date.substring(0, 2)+"/"+date.substring(6);
		return date;
	}
	
	if(region =="en_AU"){
		date = date.substring(3, 5)+"/"+date.substring(0, 2)+"/"+date.substring(6);
		return date;
	}
	
	return date;
}


