﻿// JScript File
function popupWindowNoScroll(p1,sWindowName,iWidth,iHeight) {
	var popup;
	var objOptions;	
	//make it locate at center of the page.
	var WidthX = (screen.availWidth/2) - (iWidth/2)
	var HeightY = (screen.availHeight/2) - (iHeight/2)
	objOptions = 'status=1,menubar=no,scrollbars=no,resizable=no,toolbar=no';
	objOptions = objOptions + ',width=' + iWidth;
	objOptions = objOptions + ',height=' + iHeight;
	objOptions = objOptions + ',left=' + WidthX + ',top=' + HeightY;
	popup = window.open( '', sWindowName, objOptions );
	//set the url.
	popup.location = p1;
	//set focus to the popup
	popup.focus();
	return popup;
}

//-------------------------------------------------------------------------------------------------------
//<summary>
//	Remove all spaces at the beginning of a string
//	Author: Kitty
//</summary>
//-------------------------------------------------------------------------------------------------------
function trimLeft(s)
{
 var i;
 i=0;
 var n;
 n = s.length;
 while((i<n)&&(s.charAt(i)==' ')) i++;
	s = s.substring(i);
 return(s);
} 

//-------------------------------------------------------------------------------------------------------
//<summary>
//	Remove all spaces at the end of a string
//	Author: Kitty
//</summary>
//-------------------------------------------------------------------------------------------------------
function trimRight(s)
{
 var n;
 n = s.length;
 var i;
 i = s.length-1;
 while((i>=0)&&(s.charAt(i)==' ')) i--;
	s = s.substring(0,i+1);
 return(s);
}

//-------------------------------------------------------------------------------------------------------
//<summary>
//	 Remove all leading and trailing spaces in a string
//	Author: Kitty
//</summary>
//-------------------------------------------------------------------------------------------------------
function trim(s)
{
	s = trimLeft(s);
	s = trimRight(s); 
	var ex = /\s\s+/g
	s = s.replace(ex," ")

 return(s);
} 


//Catch all keys on keyboard.
//Example:Catch key:"Ctrl+D"
//shortcut("ctrl+d",function(){alert('Clicked Ctrl+D');});
//Author:Golden
//Date:28-Feb-2008
function shortcut(shortcut,callback,opt) 
{
    //Provide a set of default options
    var default_options = {
        'type':'keydown',
        'propagate':false,
        'target':document
    }
    if(!opt) 
    {
        opt = default_options;
    }
    else 
    {
        for(var dfo in default_options) 
        {
	        if(typeof opt[dfo] == 'undefined') opt[dfo] = default_options[dfo];
        }
    }

    var ele = opt.target
    if(typeof opt.target == 'string') ele = document.getElementById(opt.target);
    var ths = this;

    //The function to be called at keypress
    var func = function(e) 
    {
        e = e || window.event;

        //Find Which key is pressed
        if (e.keyCode) code = e.keyCode;
        else if (e.which) code = e.which;
        var character = String.fromCharCode(code).toLowerCase();

        var keys = shortcut.toLowerCase().split("+");
        //Key Pressed - counts the number of valid keypresses - if it is same as the number of keys, the shortcut function is invoked
        var kp = 0;

        //Work around for stupid Shift key bug created by using lowercase - as a result the shift+num combination was broken
        var shift_nums = 
        {
	        "`":"~",
	        "1":"!",
	        "2":"@",
	        "3":"#",
	        "4":"$",
	        "5":"%",
	        "6":"^",
	        "7":"&",
	        "8":"*",
	        "9":"(",
	        "0":")",
	        "-":"_",
	        "=":"+",
	        ";":":",
	        "'":"\"",
	        ",":"<",
	        ".":">",
	        "/":"?",
	        "\\":"|"
        }
        //Special Keys - and their codes
        var special_keys = 
        {
	        'esc':27,
	        'escape':27,
	        'tab':9,
	        'space':32,
	        'return':13,
	        'enter':13,
	        'backspace':8,

	        'scrolllock':145,
	        'scroll_lock':145,
	        'scroll':145,
	        'capslock':20,
	        'caps_lock':20,
	        'caps':20,
	        'numlock':144,
	        'num_lock':144,
	        'num':144,
			
	        'pause':19,
	        'break':19,
			
	        'insert':45,
	        'home':36,
	        'delete':46,
	        'end':35,
			
	        'pageup':33,
	        'page_up':33,
	        'pu':33,

	        'pagedown':34,
	        'page_down':34,
	        'pd':34,

	        'left':37,
	        'up':38,
	        'right':39,
	        'down':40,

	        'f1':112,
	        'f2':113,
	        'f3':114,
	        'f4':115,
	        'f5':116,
	        'f6':117,
	        'f7':118,
	        'f8':119,
	        'f9':120,
	        'f10':121,
	        'f11':122,
	        'f12':123
        }
        for(var i=0; k=keys[i],i<keys.length; i++) 
        {
	        //Modifiers
	        if(k == 'ctrl' || k == 'control') 
	        {
		        if(e.ctrlKey) kp++;

	        } 
	        else if(k ==  'shift') 
	        {
		        if(e.shiftKey) kp++;

	        } 
	        else if(k == 'alt') 
	        {
			    if(e.altKey) kp++;

	        } 
	        else if(k.length > 1) 
	        { //If it is a special key
		        if(special_keys[k] == code) kp++;

	        } 
	        else 
	        { //The special keys did not match
		        if(character == k) kp++;
		        else 
		        {
			        if(shift_nums[character] && e.shiftKey) 
			        { //Stupid Shift key bug created by using lowercase
				        character = shift_nums[character]; 
				        if(character == k) kp++;
			        }
		        }
	        }
        }

        if(kp == keys.length) 
        {
	        callback(e);

	        if(!opt['propagate']) 
	        { //Stop the event
		        //e.cancelBubble is supported by IE - this will kill the bubbling process.
		        e.cancelBubble = true;
		        e.returnValue = false;

		        //e.stopPropagation works only in Firefox.
		        if (e.stopPropagation) 
		        {
			        e.stopPropagation();
			        e.preventDefault();
		        }
		        return false;
	        }
        }
    }

    //Attach the function with the event	
    if(ele.addEventListener) ele.addEventListener(opt['type'], func, false);
    else if(ele.attachEvent) ele.attachEvent('on'+opt['type'], func);
    else ele['on'+opt['type']] = func;
}

//Add or Remove value of the checked item into the list of selected items
//Author:Golden
//Date:28-Feb-2008
function fn_AddRemoveValueToList(oCheckBox, sHiddenFieldList) 
{       
	var iValue = oCheckBox.value;
	var hidListValue = document.getElementById(sHiddenFieldList);
	var sPrevValue = "";
	
	if (hidListValue)
	{	    
	    //Get list old items
	    sPrevValue = hidListValue.value;
	    
	    if(oCheckBox.checked) 
	    {
	        if (!fn_IsExistInList(sPrevValue, iValue))
	        {
	            //Only add to list (at the end)
		        if (sPrevValue == "") 
		        {
			        sPrevValue = iValue;
		        }
		        else 
		        {
			        sPrevValue = sPrevValue + "," + iValue;
		        }
		    }
    		
		    hidListValue.value = sPrevValue;
	    }
	    else 
	    {
		    var arrPrevID, iAllTempId;
		    
		    //Keep list of new temp items
		    iAllTempId ="";
		    
		    //split into array of value
		    arrPrevID = sPrevValue.split(",");
		    
		    for (var icount=0 ; icount<arrPrevID.length ; icount++) 
		    {
			    if (arrPrevID[icount] != iValue) 
			    {
				    if(iAllTempId == "") 
				    {
					    iAllTempId = arrPrevID[icount];
				    }
				    else 
				    {
					    iAllTempId  = iAllTempId + "," + arrPrevID[icount];
				    }
			    }
		    }
		    
		    //Set to hidden field
		    hidListValue.value = iAllTempId;
	    }		
	}
}

//Author:Golden
//Date:28-Feb-2008
//Check if a value is existed in list of values
function fn_IsExistInList(sList, sValue) 
{
	var exist = false;
	if (sList != "" )
	{
		var arr = sList.split(",");
		for (var i = 0; i < arr.length ; i++)
		{
			if(arr[i] == sValue)
			{ 
				exist = true;
				break;
			}		 
		}      
	}	    
	else      
	{
		exist = false;
	}	    
	
	//Return value
	return  exist;
}


/*
 isEmail check if an email address is valid (format only) 
 Author: Kitty
 Date: March 11,2008
*/
function isEmail(strEmail)
{

 var intlen;
 var ctmp;
 strEmail = trim(strEmail);
 if(strEmail=='') return false;
 intlen=strEmail.length;
 if(intlen<5) return false;
 if(strEmail.indexOf('@')==-1) return false;
 if(strEmail.indexOf('@')==0) return false;
 if(strEmail.indexOf('.')==-1) return false;
 if(intlen - strEmail.lastIndexOf('.') -1 > 3) return false; 
 if((strEmail.indexOf("_")!=-1) && (strEmail.lastIndexOf("_") > strEmail.lastIndexOf("@"))) return false;
 if(strEmail.lastIndexOf(".") <= strEmail.lastIndexOf("@")+1)  return false;
 if(strEmail.indexOf("@")!=strEmail.lastIndexOf("@")) return false;
 if(intlen -1 == strEmail.lastIndexOf('.')) return false;
 if(strEmail.charAt(strEmail.indexOf('@')+1)=='.') return false;
 if(strEmail.indexOf(" ")!=-1) return false;
 if(strEmail.indexOf("..")!=-1) return false;
 
 strEmail=strEmail.toLowerCase();
 var nPos = strEmail.indexOf('@');
 
 //check charaters before @
 for(intcnt=0;intcnt<=nPos;intcnt++)
	{
	 ctmp = strEmail.charAt(intcnt);
	 if((!isDigit(ctmp))&& ((ctmp>'z')||(ctmp<'a')) && (ctmp!='\'') && (ctmp!='-') && (ctmp!='.') && (ctmp!='@') && (ctmp!='_')) return false;
	}
	 
	
 //check charaters after @
 for(intcnt=nPos+1;intcnt<intlen;intcnt++)
	{
	 ctmp = strEmail.charAt(intcnt);
	 if((!isDigit(ctmp))&& ((ctmp>'z')||(ctmp<'a')) && (ctmp!='-') && (ctmp!='.') && (ctmp!='@') && (ctmp!='_')) return false;
	}
	
 /*for(intcnt=0;intcnt<intlen;intcnt++)
	{
	 ctmp = strEmail.charAt(intcnt);
	 if((!isDigit(ctmp))&& ((ctmp>'z')||(ctmp<'a')) && (ctmp!='\'') && (ctmp!='-') && (ctmp!='.') && (ctmp!='@') && (ctmp!='_')) return false;
	}*/

	return true	;
}
/*
isDigit
Check if a character is a digit or not
*/
function isDigit(strMaxHorizontalScale)
{
    var horizontalScalePattern = /^\d+$/;
    var horizontalRegExp = new RegExp(horizontalScalePattern);
    return horizontalRegExp.test(strMaxHorizontalScale);
}
/* set focus for default field of the form */

function setFocus(ObjName)
{
    try
    {
        setTimeout("document.getElementById('" + ObjName + "').focus();",100);
    }catch (ex){}
    
}

function ReturnMainPageForAuthorizedUser()
{
    window.location.href = "UtilityBillingAccountInquiry.aspx";
}

function ReturnMainPageForSystemAdmin()
{
    window.location.href = "ManageAuthorizeUser.aspx";
}

//Format Credit Card Number
function FormatCreditCardNumber(value)
{
    var len = value.length;
    var result = "";
    var tmp = "";
    
    while(len > 4)
    {
        tmp = value.substr(0,4);
        value = value.substr(4,len);
        len = value.length;
        result += tmp + "-";
    }
    
    result += value;
    return result;
}

//Format Number
function FormatNumber(value, numberLen)
{
    var len = value.length;
    var result = "";
    var tmp = "";
    
    while(len > 5)
    {
        tmp = value.substr(0,5);
        value = value.substr(5,len);
        len = value.length;
        result += tmp + "-";
    }
    
    result += value;
    return result;
}

//Format Phone Number
function FormatPhoneNumber(value, numberLen)
{
    var len = value.length;
    var result = "";
    var tmp = "";
    var i = 0;
    while(len > numberLen)
    {
        i++;
        if(i == 3)
        {
            tmp = value.substr(0,4);
            value = value.substr(4,len);
            result += tmp;
        }
        else
        {
            tmp = value.substr(0,numberLen);
            value = value.substr(numberLen,len);
            result += tmp + "-";
        }
        len = value.length;
        
    }
    
    result += value;
    return result;
}
