﻿// JScript File

var gbDHTML = (document.getElementById || document.all || document.layers);
// Always call gGetObject 
function getObj(objectID)
{
  if (document.getElementById)
  {
  	this.obj = document.getElementById(objectID);
	this.style = document.getElementById(objectID).style;
  }
  else if (document.all)
  {
	this.obj = document.all[objectID];
	this.style = document.all[objectID].style;
  }
  else if (document.layers)
  {
   	this.obj = document.layers[objectID];
   	this.style = document.layers[objectID];
  }
}

function gGetObject(objectID)
{
   var obj = new getObj(objectID);
   return obj;
}

function gIsNull(obj)
{
    if(obj != null && typeof(obj) != 'undefined')
        return false;
    else
        return true;
}

function gObjExist(objectID)
{
    var obj;
    if (document.getElementById)
    {
  	    obj = document.getElementById(objectID);
    }
    else if (document.all)
    {
	    obj = document.all[objectID];
    }
    else if (document.layers)
    {
   	    obj = document.layers[objectID];
    }
    if(gIsNull(obj))
        return false;
    else
        return true;
}

function gSetCSS(obj, styleClassName)
{
    obj.className = styleClassName;
}

function gSetColor(objectID, color)
{
    if(!gbDHTML)
        return;
    var obj = gGetObject(objectID);
    if(gIsNull(obj))
        return;
     obj.style.color = color;
}

function gSetVisible(objectID, bVisible)
{
    if(!gbDHTML)
        return;
    var obj = gGetObject(objectID);
    if(gIsNull(obj))
        return;
     obj.style.visibility  = bVisible? 'visible':'hidden';
}

function gSetDisplay(objectID, bDisplay)
{
    if(!gbDHTML)
        return;
    var obj = gGetObject(objectID);
    if(gIsNull(obj))
        return;
     obj.style.display  = bDisplay ? 'block':'none';
}

function gSetFontStyle(objectID, strFontStyle)
{
    if(!gbDHTML)
        return;
    var obj = gGetObject(objectID);
    if(gIsNull(obj))
        return;
     obj.style.fontStyle  = strFontStyle; 
}

function gSetFontFamily(objectID, strFontFamily)
{
    if(!gbDHTML)
        return;
    var obj = gGetObject(objectID);
    if(gIsNull(obj))
        return;
     obj.style.fontFamily  = strFontFamily;
}

function gGetInnerHTML(objectID)
{
    if(!gbDHTML)
        return;
    var obj = gGetObject(objectID);
    if(gIsNull(obj))
        return;
     return obj.obj.innerHTML;
}

function gSetInnerHTML(objectID, strInnerHTML)
{
    if(!gbDHTML)
        return;
    var obj = gGetObject(objectID);
    if(gIsNull(obj))
        return;
     obj.obj.innerHTML  = strInnerHTML;
}

function gGetTextBoxValue(objectID)
{
    var obj = gGetObject(objectID);
    if(gIsNull(obj))
        return;
    return obj.obj.value;
}

function gSetTextBoxValue(objectID, value)
{
    var obj = gGetObject(objectID);
    if(gIsNull(obj))
        return;
    obj.obj.value = value;
}

function gGetDropdownlistSelectedValue(objectID)
{
     var obj = gGetObject(objectID);
    if(gIsNull(obj))
        return;
    return obj.obj.value;
}

function gGetIntValue(val, defVal)
{
    if(gIsNull(val) || gIsUndefined(val))
        return defVal;
        
     var ret = parseInt(val);
     if(isNaN(ret))
        return defVal;
     else
        return ret;
}

function gGetString(val, len)
{
    if(gIsNull(val) || gIsNull(len) || len < 0)
        return val;
        
    if(val.length <= len)
        return val;
     else
        return val.substr(0, len);
    
}

//============== string =================
// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

//============== Cookie =================

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();
		document.cookie = name+"="+value+expires+"; path=/";
	}
	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);
}

//==================== Validation   ==============
function gEmailValidate(string) 
{
    if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
        return true;
    else
        return false;
}
function gIsTextBoxEmpty(id)
{
    var ctrl = gGetObject(id);
    if(gIsNull(ctrl))
        return true;
    var ret = trim(ctrl.obj.value);
    if(ret.length <= 0)
        return true;
    else
        return false;
}
function gIsTextBoxValidEmailAddress(id)
{
    var ctrl = gGetObject(id);
    if(gIsNull(ctrl))
        return true;
    return gEmailValidate(ctrl.obj.value);
}
function gIsCheckBoxChecked(id)
{
   var ctrl = gGetObject(id);
    if(gIsNull(ctrl))
        return true;
    return ctrl.obj.checked;
}

function gIsUndefined(val)
{
    if(undefined == val)
        return true;
    else
        return false;
}

function gContainScript(strLine)
{
    if(gIsNull(strLine) || gIsUndefined(strLine))
        return false;
    if(strLine.length <= 0)
        return false;
    var str = strLine.toLowerCase();
    if(str.indexOf("script") > 0)
        return true;
    if(str.indexOf("vbscript") > 0)
        return true;
    if(str.indexOf("onclick") > 0)
        return true;
    if(str.indexOf("<object") > 0)  //ActiveX control not allowed.
        return true;
    
     return false;
}

//General Ajax OnError handler
function gOnError(ret, strContext, strMethodName)
{
    if(ret.get_timedOut())
        alert("OnTimeout \r\n ret=" + ret + "\r\n strContext=" + strContext + "\r\n strMethodName=" + strMethodName);
    else
        alert("OnError \r\n ret=" + ret + "\r\n msg= "+ ret.get_message() +"\r\n stack="+ ret.get_stackTrace() +"\r\n strContext=" + strContext + "\r\n strMethodName=" + strMethodName);   
}

//==================== Date Time   ==============
//Formate datetime object to 11/10/2007 12:49:35 AM 
function gDateFormat(dt, separator)
{
    if(gIsNull(dt))
        return "";
    var nMonth = dt.getMonth() + 1;         //getMonth() returns 0 - 11
    return "" + nMonth + separator + dt.getDate() + separator + dt.getFullYear();
}

function gTimeFormat(dt, Is24HrFormat)
{
 
    if(gIsNull(dt))
        return "";
      
    if(Is24HrFormat)
    {
        return "" + dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();   
    }
    else
    {
        if(dt.getHours() > 12)
            return "" + (dt.getHours()-12) + ":" + dt.getMinutes() + ":" + dt.getSeconds() + " PM";   
        else
            return "" + dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds() + " AM";   
    }  
}

function gDayOfYear(dt)
{
    var yearStart = new Date(dt.getFullYear(), 0, 1);
    return Math.ceil((dt - yearStart) / 86400000);
}

function gWeekOfYear(dt)
{
    return Math.ceil(gDayOfYear(dt)/7);
}

function gDateAdd(dt, nYear, nMonth, nDay)
{
    if(gIsNull(dt))
        return dt;
    var val = dt.getFullYear();
    if(!gIsNull(nYear))
    {
        val = val + nYear;
        dt.setFullYear(val);
    }
    
    if(!gIsNull(nMonth))
    {
        val = dt.getMonth() + nMonth;
        if(val >= 12)
        {
            dt.setMonth(val - 12);
            dt.setFullYear(dt.getFullYear() + 1);
        }
        else if(val < 0)
        {
            dt.setMonth(val + 12);
            dt.setFullYear(dt.getFullYear() - 1);
        }
        else
        {
            dt.setMonth(val);
        }
    }
    
    if(!gIsNull(nDay))
    {
        var dayofyear = gDayOfYear(dt);
        dayofyear = dayofyear + nDay;
        dt = gGetDate(dt.getFullYear(), dayofyear);
    }
    return dt;
}

function gDaysInMonth(nMonth, nYear)
{
    //This is a very interesting function. the new Date() will move the date to next month if overflow.
	return 32 - new Date(nYear, nMonth, 32).getDate();
}

function gGetMonth(nYear, nDays)
{
    var nTotalDays = nDays%366;
    for(i = 0; i < 12; i++)
    {
        nTotalDays = nTotalDays + gDaysInMonth(i, nYear);
        if(nTotalDays > nDays)
            return i - 1;
    }
    return -1;
}

function gGetDate(nYear, nDays)
{
    var dt = new Date(nYear,0,1);
    nDays = nDays%366;
    var nTotalDays = 0;
    for(i = 0; i < 12; i++)
    {
        nTotalDays = nTotalDays + gDaysInMonth(i, nYear);
        if(nTotalDays > nDays)
        {
            dt.setMonth(i - 1);
            dt.setDate(nDays - (nTotalDays - gDaysInMonth(i, nYear)));
            break;
        }
    }
    return dt;
}
