var msg_wait = 5000;
var msg_fade = 2000;
var selected_values = "";

function openWin(url, name, w, h, scroll) {	
	xpos = (screen.width - w) / 2;
    ypos = (screen.height - h) / 2;
    params = "height=" + h + ",width=" + w + ",left=" + xpos + ",top=" + ypos + ",scrollbars=" + scroll + ",status=no,toolbar=no,menubar=no,location=no,modal=yes";
    window.open(url, name, params);
}

function openImgWin(url, w, h, scroll) {	
	xpos = (screen.width - w) / 2;
    ypos = (screen.height - h) / 2;
    params = "height=" + h + ",width=" + w + ",left=" + xpos + ",top=" + ypos + ",scrollbars=" + scroll + ",status=no,toolbar=no,menubar=no,location=no,modal=yes,resizable=1";
    window.open(url, "" , params);
}

function OpenPrintPage(url) {	
   var sOption="toolbar=yes,location=no,directories=yes,menubar=yes,"; 
       sOption+="scrollbars=yes,width=760,height=600,left=100,top=25"; 
       
    return window.open(url, "", sOption);
}

function blockEnterKey() 
{    
    var key;
    
    if (window.event)
    {
        if (window.event.keyCode == 13)
        window.event.keyCode =0;
    }
    else
    {
        key = e.which;
        if (key == 13) e.which=0;
    }
}

function getKeyCode()
{
    var key;
    
    if (window.event)
    {
        return window.event.keyCode;
    }
    else
    {
        return e.which;
    }
    
    return -1;
}

function enterKeySubmit(btnToSubmit) 
{    
    var key;
    
    if (window.event)
    {
        if (window.event.keyCode == 13) 
        {
        alert(btnToSubmit.id);
            btnToSubmit.click(); 
            return false;
        }
        
    }
    else
    {
        key = e.which;
        if (key == 13) 
        {alert(btnToSubmit.id);
            btnToSubmit.click(); 
            return false;
        }
    }
    
    return true;
}

function formatCurrency(ctrl) {
    var num = ctrl.value;
    
    num = num.toString().replace(/\$|\,/g,'');

    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    
    ctrl.value = (((sign)?'':'-') + num + '.' + cents);
}

function formatPosCurrency(ctrl) {
    var num = ctrl.value;
    
    num = num.toString().replace(/\$|\,/g,'');

    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    
    ctrl.value = ( num + '.' + cents);
}


function formatNumber(num){
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "1"
    
    num = Math.floor(num)
    
    return num;
}

function formatDecimalNumber(num){
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "1"
    
    return num;
}

function formatPosNumber(ctrl){
    var num = ctrl.value;
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "1"
    
    num = Math.floor(num)
    
    if (num < 0) num = num * -1;
    
    ctrl.value = num;
}

function formatQty(ctrl){    
    var num = ctrl.value;
    
    if (num != "")
    {
        num = num.toString().replace(/\$|\,/g,'');
        if(isNaN(num))
        num = "1"
        
        num = Math.floor(num)
        
        if (num < 0) num = num * -1;
        if (num == 0) num = 1;
        
        ctrl.value = num;
    }
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


/*FORM INPUT FORMATTER*/

	function removeInlineSpaces(strSource) {
		var arySrc, i, strFormatted;
		
		strFormatted = "";
		arySrc = strSource.split(' ');
		
		for (i=0 ; i < arySrc.length ; i++) {
			if (arySrc[i].charAt(0) != '') {
				strFormatted = strFormatted + arySrc[i] + " ";
			}
		}
		return strFormatted;
	}
		
	function toTitleCase(strSource) {
		var arySrc, i, strFormatted, strFirstChar, strTillLast;
		
		strFormatted = "";
		arySrc = strSource.split(' ');
		
		for (i=0 ; i < arySrc.length ; i++) {
			strFirstChar = arySrc[i].charAt(0).toUpperCase();
			strTillLast = arySrc[i].substring(1, arySrc[i].length);
			strFormatted = strFormatted + strFirstChar + strTillLast + " ";
		}
		return strFormatted;
	}
	
	function trimControl (frmCtrl){
	    var strText = frmCtrl.value;
	
	    while (strText.charAt(0) == ' ') 
	        strText = strText.substring(1, strText.length);

	    // this will get rid of trailing spaces 
	    while (strText.charAt(strText.length - 1) == ' ')
    	    strText = strText.substring(0, strText.length-1);

	    frmCtrl.value = strText;   
	}

	function trim(strText) { 
	    // this will get rid of leading spaces 
    	while (strText.charAt(0) == ' ') 
	        strText = strText.substring(1, strText.length);

	    // this will get rid of trailing spaces 
	    while (strText.charAt(strText.length - 1) == ' ')
    	    strText = strText.substring(0, strText.length-1);

	    return strText;
	} 
	
	function formatName (frmCtrl) {
		// Removes inline spaces and changes the names to title cap.
		// Do a simple conversion of _PL_ to Pte Ltd
		var strFormatted = frmCtrl.value;
		strFormatted = removeInlineSpaces(strFormatted);
		strFormatted = toTitleCase(strFormatted);
		strFormatted = strFormatted.replace(/\bPL\b/gi,"Pte Ltd");
		strFormatted = trim(strFormatted);
		frmCtrl.value = strFormatted;
	}
	
	function formatAddress (frmCtrl) {
		// Removes inline spaces and changes to title cap, and changes common abbreviations to designated descriptions
		frmCtrl.value = removeInlineSpaces(frmCtrl.value);
		frmCtrl.value = toTitleCase(frmCtrl.value);
		frmCtrl.value = subAbbreviations(frmCtrl.value);
		frmCtrl.value = trim(frmCtrl.value);	
	}
	
	function formatURL (frmCtrl) {
		var strFormatted = frmCtrl.value;
		if (strFormatted.match(/http:\/\//) == null) {
			strFormatted = "http://" + strFormatted;
		}
		frmCtrl.value = strFormatted;
	}
	
	function subAbbreviations(strSrc) {
		var strFormatted = strSrc;
		if (window.RegExp) {
			strFormatted = strFormatted.replace(/\bApartment\b/gi,"Apt");
			strFormatted = strFormatted.replace(/\bArcade\b/gi, "Arc");
			strFormatted = strFormatted.replace(/\bAssociation\b/gi, "Assn");
			strFormatted = strFormatted.replace(/\bAvenue\b/gi, "Ave");
			strFormatted = strFormatted.replace(/\bBuilding\b/gi,"Bldg");
			strFormatted = strFormatted.replace(/\bBlock\b/gi,"Blk");
			strFormatted = strFormatted.replace(/\bBoulevard\b/gi,"Blvd");
			strFormatted = strFormatted.replace(/\bBukit\b/gi,"Bt");
			strFormatted = strFormatted.replace(/\bChamber\b/gi,"Chbrs");
			strFormatted = strFormatted.replace(/\bClose\b/gi,"Cl");
			strFormatted = strFormatted.replace(/\bCrescent\b/gi,"Cres");
			strFormatted = strFormatted.replace(/\bCourt\b/gi,"Ct");
			strFormatted = strFormatted.replace(/\bCommonwealth\b/gi,"C'wealth");
			strFormatted = strFormatted.replace(/\bDrive\b/gi,"Dr");
			strFormatted = strFormatted.replace(/\bEstate\b/gi,"Est");
			strFormatted = strFormatted.replace(/\bFactory\b/gi,"Fty");
			strFormatted = strFormatted.replace(/\bFarmway\b/gi,"F'way");
			strFormatted = strFormatted.replace(/\bGarden\b/gi,"Gdn");
			strFormatted = strFormatted.replace(/\bGrove\b/gi,"Gr");
			strFormatted = strFormatted.replace(/\bHeadquarter\b/gi,"Hq");
			strFormatted = strFormatted.replace(/\bHouse\b/gi,"Hse");
			strFormatted = strFormatted.replace(/\bIndustrial\b/gi,"Ind");
			strFormatted = strFormatted.replace(/\bJalan\b/gi,"Jln");
			strFormatted = strFormatted.replace(/\bKampong\b/gi,"Kg");
			strFormatted = strFormatted.replace(/\bKilometre\b/gi,"Km");
			strFormatted = strFormatted.replace(/\bLorong\b/gi,"Lor");
			strFormatted = strFormatted.replace(/\bMansion\b/gi,"Mans");
			strFormatted = strFormatted.replace(/\bMarket\b/gi,"Mkt");
			strFormatted = strFormatted.replace(/\bMount\b/gi,"Mt");
			strFormatted = strFormatted.replace(/\bNorth\b/gi,"Nth");
			strFormatted = strFormatted.replace(/\bOffice\b/gi,"Ofc");
			strFormatted = strFormatted.replace(/\bPark\b/gi,"Pk");
			strFormatted = strFormatted.replace(/\bPlace\b/gi,"Pl");
			strFormatted = strFormatted.replace(/\bPoint\b/gi,"Pt");
			strFormatted = strFormatted.replace(/\bQuarters\b/gi,"Qrs");
			strFormatted = strFormatted.replace(/\bRoad\b/gi,"Rd");
			strFormatted = strFormatted.replace(/\bSector\b/gi,"Sec");
			strFormatted = strFormatted.replace(/\bSungei\b/gi,"Sg");
			strFormatted = strFormatted.replace(/\bSite Office\b/gi,"S/Ofc");
			strFormatted = strFormatted.replace(/\bSingapore\b/gi,"S'pore");
			strFormatted = strFormatted.replace(/\bSquare\b/gi,"Sq");
			strFormatted = strFormatted.replace(/\bStreet\b/gi,"St");
			strFormatted = strFormatted.replace(/\bSaint\b/gi,"St");
			strFormatted = strFormatted.replace(/\bSouth\b/gi,"Sth");
			strFormatted = strFormatted.replace(/\bStation\b/gi,"Stn");
			strFormatted = strFormatted.replace(/\bTerrace\b/gi,"Ter");
			strFormatted = strFormatted.replace(/\bTanjong\b/gi,"Tg");
			strFormatted = strFormatted.replace(/\bUpper\b/gi,"Up");
			strFormatted = strFormatted.replace(/\billage\b/gi,"Vill");
			strFormatted = strFormatted.replace(/\bWalk\b/gi,"Wlk");
			return strFormatted;
		}
	}


	function highlightRow(row_uid) {
			document.getElementById(row_uid).className = "clickableRow";
			
	}
	
	function highlightRow2(row_uid, highlightclass) {
			document.getElementById(row_uid).className = highlightclass;
	}
	
	function unhighlightRow(row_uid, newclass) {
			document.getElementById(row_uid).className = newclass;
	}
	
	function highlightDiv(row_uid) {
			document.getElementById(row_uid).className = "event_item_highlight";
	}
	
	function unhighlightDiv(row_uid, newclass) {
			document.getElementById(row_uid).className = newclass;
	}

	function CheckAll(cb)
    {

	    for (var i=0;i<document.aspnetForm.elements.length;i++)
	    {
		    var e = document.aspnetForm.elements[i];
		    
		    if ((e != cb) && (e.type=='checkbox'))
		    {
		        
		        if (!e.disabled) e.checked = cb.checked;
		    }
	    }	    
    }
    
    function MarkCheckBox(uid)
    {
        
        var e = document.getElementById("cb_" + uid);        
        e.checked = !(e.checked);
    }
    
    function BlockUI(msg, validate)
    {
        var valid = true;        
        
        if (validate == "1")
        {            
            valid = Page_ClientValidate();
        }
        
        if (valid)
        {
            $.blockUI({ message: '<b style=""font-size:14px; font-family:Tahoma;""><img src="../Images/indicator.gif" /> &nbsp;&nbsp;' + msg + '...</b>' , css: { padding: 8, border: '2px solid #000'}});                                                
        }
    }

/* jQuery */

    function Confirmation(msg)
    {
        var con = false;
        
        con = confirm(msg);        
        if ( con ) 
            BlockUI('Processing Update...', '0');
        
        return con;
    }
