﻿//----------------------------------------------------------------
//
// Common function
//
//----------------------------------------------------------------
function Common_Select_Find(objSelect, strParam, bByValue)
{
    for (var i=0; i<objSelect.options.length; i++)
    {
        if (bByValue == false)
        {
            if (objSelect.options[i].innerText == strParam)
                break;
        }
        else
        {
            if (objSelect.options[i].value == strParam)
                break;
        }           
    }
    if (i >= objSelect.options.length)    i = -1;
    return (i);
}
  
function Common_Select_SetSelected(objSelect, strParam, bByValue)
{
   var nIndex = Common_Select_Find(objSelect, strParam, bByValue);
   if (nIndex >= 0) 
       objSelect.options[nIndex].selected = true;   
}
//---------------------------------------------------------------------
function Common_Array_RemoveItem(oSrcArray, strValue)
{
    if (typeof(oSrcArray) == "undefined" || oSrcArray == null || oSrcArray.length <= 0)
        return (oSrcArray);

    var oArray = new Array();
    if (oSrcArray != null && oSrcArray.length > 0)
    {
        var strString;
        for (var i=0; i<oSrcArray.length; i++)
        {
            strString = oSrcArray[i];
            if (strString == strValue)
                continue;
                        
            oArray[oArray.length] = strString;
        }
    }             
    oSrcArray = oArray;

    return (oSrcArray);
}
//---------------------------------------------------------------------
function Common_String_Trim(strString) 
{
    if (strString != null && strString.length > 0)
    {    
        strString = strString.replace(/^\s*/,""); //strip leading
        strString = strString.replace(/\s+$/,""); //strip trailing
    }
    return (strString);
}
//---------------------------------------------------------------------
function FindObject(strName, strTagName, oParentObj)
{
    var oCurrObj = "";
    oParentObj = (oParentObj != null)? oParentObj.document : document;
    var oFoundObj = oParentObj.getElementsByTagName(strTagName);

    for (var i=0; i<oFoundObj.length; i++)
    {
        var oTempObj = oFoundObj.item(i);
        if (oTempObj.id == strName)
        {
            oCurrObj = oTempObj;
            break;
        }
    }
    return (oCurrObj);
}

//---------------------------------------------------------------------
function Common_XmlHttp_Post(strTargetASP, strData)
{
    var strReturn = "";
    var oXmlHttp = getXmlObject();
    oXmlHttp.open("POST", strTargetASP, false);
    oXmlHttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
    oXmlHttp.send(strData);  
    if (oXmlHttp.status != "200")
    {
        alert("Error : " + oXmlHttp.status + " = " + oXmlHttp.statusText);
    }
    else
    {   
       try
       {
           strReturn = oXmlHttp.responseText;
       }
       catch(e)
       {
       }
    }
    return (strReturn);
}
//---------------------------------------------------------------------
function getXmlObject() 
{ 
    var objXML = null; 
    try 
    { 
        objXML = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e) 
    { 
        try 
        { 
            objXML = new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch(oc) 
        { 
            objXML = null;
        } 
    } 
    
    if (!objXML && typeof XMLHttpRequest != "undefined" ) 
    { 
        objXML = new XMLHttpRequest();
    } 
    
    return objXML;
} 

//----------------------------------------------------------
function check_Empty(objCheck, strMsg, bFocus)
{		
    var result = true;
    if (objCheck.type == "checkbox" || objCheck.type == "radio")
    {
        result = check_Empty_Radio(objCheck);
    }
    else
    {
        if (Common_String_Trim(objCheck.value) == "")
            result = false;
    }

    if (result == false)
    {
        if (strMsg != "")    alert(strMsg);
        if (typeof(bFocus) != "undefined" && bFocus == false)
            return (result);
            
        objCheck.focus();
    }
    
    return (result);
}

//----------------------------------------------------------
function check_Empty_Radio(objCheck)
{
    var objForm = objCheck.form;     
    for (i=0; i < objForm.elements.length; i++)
    {
        var obj = objForm.elements[i];
        if (obj.name == objCheck.name)
        {
            if (obj.checked == true)
                break;
        }
    }
    return (i < objForm.elements.length);
}

//----------------------------------------------------------
function check_Email(objCheck, strMsg)
{    
    // cut email by '<>'
    var result = true;    
    var value = Common_String_Trim(objCheck.value); 
    if (result == true)
    {
        var strEmailPattern = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
        var oRegex = new RegExp(strEmailPattern);
        result = oRegex.test(value);
    }
    
    if (result == false)
    {
        if (strMsg != "")    alert(strMsg);
        objCheck.focus();
    }
    return (result);
} 

//----------------------------------------------------------
function check_Letter(objCheck, strMsg)
{
    var oRegex = /^[a-zA-Z]+$/;
    var result = oRegex.test(objCheck.value);
    if (result == false)
    {
        if (strMsg != "")    alert(strMsg);
        objCheck.focus();   
    } 
    return (result);
}
//----------------------------------------------------------
function check_Date(objCheck, strMsg)
{ 	
    var result = check_Empty(objCheck, strMsg, false);
    if (result == true)
    {         
        var strDate = Common_String_Trim(objCheck.value);
        var objDate = Common_Convert_ToDate(strDate);
        if (objDate != null)    return (true);

        result = false;
        if (result == false)
        {
            if (strMsg != "")    alert(strMsg);
            //objCheck.focus();   
        }
    }
    return (result);
}

//----------------------------------------------------------
function check_CodeID(objCheck, strMsg)
{		
    var result = check_Empty(objCheck, strMsg, true);
    if (result == true)
    {   
        result = isValidCodeID(objCheck.value);       
    
        if (result == false)
        {
            if (strMsg != "")    alert(strMsg);
            objCheck.focus();   
        }        
    }
    
    return (result);
}

//----------------------------------------------------------
function isValidCodeID(strValue)
{		
    var result = false;
    
    strValue = Common_String_Trim(strValue).toLowerCase();
                    
    if (strValue.length == 12 || strValue.length == 14)
    {
        if (strValue.length == 12)
        {
            if (/^\d+$/.test(strValue) == true)
                result = true;
        }
        else
        {
            if (strValue.substring(0,2) == "sn")
            {
                var strString = strValue.substring(2);  
                if (/^\d+$/.test(strString) == true)
                    result = true;
            }
        }
    }
    
    return (result);
}

//----------------------------------------------------------
function check_CheckNumber(objCheck, strMsg)
{		
    var result = check_Empty(objCheck, strMsg, true);
    if (result == true)
    {   
        result = isValidCheckNumber(objCheck.value);       
    
        if (result == false)
        {
            if (strMsg != "")    alert(strMsg);
            objCheck.focus();   
        }        
    }
    
    return (result);
}

//----------------------------------------------------------
function isValidCheckNumber(strValue)
{		
    var result = false;
    
    strValue = Common_String_Trim(strValue).toLowerCase();
                    
    if (strValue.length == 4)
    {
        if (/^\d+$/.test(strValue) == true)
            result = true;
    }
    
    return (result);
}
//----------------------------------------------------------
function Common_Convert_ToDate(strDate)
{   
    var objDate = null;
    if (typeof(strDate) == 'undefined' || strDate == "")
        return (null);
    
    strDate = strDate.replace(/-/g, "/");
    var oArray = strDate.split(' ');
    if (oArray == null || oArray.length <= 0)
        return (null);
        
    var array = oArray[0].split('/');
    if (array.length < 3)    return (null);
    if (isNaN(array[0]) == true || isNaN(array[1]) == true || 
        isNaN(array[2]) == true)
        return (null);
    
    var nYear = parseInt(array[0], 10);
    var nMonth = parseInt(array[1], 10);
    var nDay = parseInt(array[2], 10); 
    var nHour = 0;
    var nMinute = 0;
    var nSecond = 0;
    if (nYear < 1900)    return (null);  
    if (nMonth < 1 || nMonth > 12)    return (null);    
    
    if (oArray.length > 1)
    {
        array = oArray[1].split(':');
        if (array != null && array.length > 0)
        {
            if (isNaN(array[0]) == false)
                nHour = parseInt(array[0], 10);
            if (array.length > 1 && isNaN(array[1]) == false)
                nMinute = parseInt(array[1], 10);
            if (array.length > 2 && isNaN(array[2]) == false)
                nSecond = parseInt(array[2], 10);    
        }
    }
    
    // is date valid ??        
    var arrayDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31); 
    if ((nYear % 4 == 0) && (nYear % 100 != 0) || (nYear % 400 == 0))
        arrayDays[1] = 29;
    if (nDay < 1 || nDay > arrayDays[nMonth-1])
        return (null);
        
    var objDate = new Date(nYear, nMonth-1, nDay, nHour, nMinute, nSecond);        
    return (objDate);
}
//----------------------------------------------------------
function Common_Date_ToString(nYear, nMonth, nDate, nHour, nMinutes, nSecond)
{     
    var strDate = "";
    if (typeof(nYear) == 'undefined' || nYear == "" ||
        typeof(nMonth) == 'undefined' || nMonth == "" ||
        typeof(nDate) == 'undefined' || nDate == "")
        return (strDate); 

    var arrayTemp = new Array(nYear, nMonth, nDate);                         
    var arraySep = new Array('/', '/', '');
    if (typeof(nHour) != "undefined" && Math.floor(nHour) >= 0 && 
        typeof(nMinutes) != "undefined" && Math.floor(nMinutes) >= 0 && 
        typeof(nSecond) != "undefined" && Math.floor(nSecond) >= 0)
    {
        arrayTemp = new Array(nYear, nMonth, nDate, nHour, nMinutes, nSecond);                         
        arraySep = new Array('/', '/', ' ', ':', ':', '');    
    }
    for (var i=0; i<arrayTemp.length; i++)
    {
        if (arrayTemp[i] < 10 && i > 0)   // not year
            strDate += "0";
        strDate += arrayTemp[i] + arraySep[i];       
    }     
    return (strDate);     
}
//----------------------------------------------------------
function Common_Date_ToString2(oDate, bAddTime)
{     
    var strDate = "";
    if (typeof(oDate) == 'undefined' || oDate == null)
        return (strDate); 
            
    var arrayTemp = new Array(oDate.getFullYear(), oDate.getMonth()+1, 
                              oDate.getDate(), oDate.getHours(),
                              oDate.getMinutes(), oDate.getSeconds());
    var arraySep = new Array('/', '/', ' ', ':', ':', '');   
    var nCount = arrayTemp.length;
    if (bAddTime != true)
    {
         nCount = 3;
         arraySep[2] = '';
    }
    for (var i=0; i<nCount; i++)
    {
        if (arrayTemp[i] < 10 && i > 0)   // not year
            strDate += "0";
        strDate += arrayTemp[i] + arraySep[i];       
    }     
    return (strDate);     
}
//----------------------------------------------------------
function isValidDate(strYear, strMonth, strDate, bBirth)
{
    //Year
    var result = false;
    if (bBirth == true || typeof(bBirth) == 'undefined' || bBirth == '')
    {
        result = isValidYear(strYear);
        if (result == false)    return (result);      
    }

    //Month
    result = isValidMonth(strMonth);
    if (result == false)    return (result);
  
    //Date
    result = isValidDay(strYear, strMonth, strDate);    
    return result;
}

//----------------------------------------------------------
function isValidYear(strYear)
{
    var today = new Date();
    if (typeof(strYear) == 'undefined' || strYear == '' || isNaN(strYear) == true)    
        return (false);
    if ((parseInt(strYear) > parseInt(today.getFullYear())) || 
	    (parseInt(strYear) < (parseInt(today.getFullYear())-100)))
    {
        return (false);
    }    
    return (true);    
} 

//----------------------------------------------------------
function isValidMonth(strMonth)
{
    if (typeof(strMonth) == 'undefined' || strMonth == '' || isNaN(strMonth) == true)    
        return (false);
    if ((parseInt(strMonth) > 12) || (parseInt(strMonth) < 1))
    {
        return (false);
    }    
    return (true);
}

//----------------------------------------------------------
function isValidDay(strYear, strMonth, strDate)
{  
    if (typeof(strYear) == 'undefined' || strYear == '' || isNaN(strYear) == true || 
        typeof(strMonth) == 'undefined' || strMonth == '' || isNaN(strMonth) == true || 
        typeof(strDate) == 'undefined' || strDate == '' || isNaN(strDate) == true)
        return (false);
        
    var nDayCount = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    if (((parseInt(strYear) % 4 == 0) && (parseInt(strYear) % 100 != 0)) || (parseInt(strYear) % 400 == 0))
        nDayCount[1] = 29; 
    
    if ((nDayCount[parseInt(strMonth) -1] < parseInt(strDate)) || (parseInt(strMonth)  > 12) || (parseInt(strMonth)  < 1))
    {
        return (false);
    }    
    return (true);
}
//----------------------------------------------------------
function check_Numeric(objCheck, strMsg)
{
    if (check_Empty(objCheck, strMsg) == false)
        return (false);
        
    if (Common_String_Trim(objCheck.value) != "")
    {
        if (isNaN(objCheck.value) == true)
        {
            if (strMsg != "")    
                alert(strMsg);
            objCheck.focus();
            return (false);
        }
    }    

    return (true);
} 

//----------------------------------------------------------
function check_NumericEx(objCheck, strMsg)
{
    if (check_Empty(objCheck, strMsg) == false)
        return (false);
    
    var strValue = Common_String_Trim(objCheck.value).toUpperCase();
    for (var i=0; i<strValue.length; i++)
    {
        var ch = strValue.substring(i, i+1);
        if (i == 0 && ch == "-")
        	continue;
	    
	    if ((ch < '0' || ch > '9'))
        {
            if (strMsg != "")    
                alert(strMsg);
            objCheck.focus();
            return (false);   
        }    
    }         
    
    return (true);
}
//----------------------------------------------------------
function check_LetterNumeric(objCheck, strMsg, bShowMsg)
{
    if (Common_String_Trim(objCheck.value) != "")
    {
        var value = Common_String_Trim(objCheck.value);
        value = value.toUpperCase();        
        for (var i=0; i<value.length; i++)
        {
            var ch = value.substring(i, i+1);
            if ((ch < '0' || ch > '9') && (ch < 'A' || ch > 'Z'))
            {
                if (typeof(bShowMsg) != "undefined" && bShowMsg == true && strMsg != "")    alert(strMsg);
                objCheck.focus();  
                return (false);            
            }    
        }        
    }
    return (true);
} 

//----------------------------------------------------------
function check_LetterNumeric2(objCheck, strMsg)
{
    var result = false;
    if (Common_String_Trim(objCheck.value) != "")
        result = check_LetterNumeric(objCheck, strMsg, false);
    
    if (result == false)
    {
        if (strMsg != "")    alert(strMsg);
        if (typeof(bFocus) != "undefined" && bFocus == false)
            return (result);
            
        objCheck.focus();
    }
     
    return (result);
}

//----------------------------------------------------------
function check_LetterNumeric2Ex(objCheck, strMsg, nMinLen, nMaxLen)
{
    var result = false;    
    if (check_LetterNumeric2(objCheck, strMsg) == true)
        result = check_LimitLen(objCheck, strMsg, nMinLen, nMaxLen);
    
    return (result);
}

//----------------------------------------------------------
function check_LimitLen2(objCheck, strMsg, nMinLen, nMaxLen)
{
    var result = false;
    var nLen = Common_String_Trim(objCheck.value).length;
    if (nLen >= nMinLen)
    {
        var nCharLen = 0;
        for (var i=0; i<nLen; i++)
        {
            var nCode = Common_String_Trim(objCheck.value).charCodeAt(i);
            if (nCode > 255)
                nCharLen += 2;
            else
                nCharLen++;
        }
        if (nCharLen <= nMaxLen)
            result = true;
    }

    if (result == false)
    {
        if (strMsg != "")    alert(strMsg);
        objCheck.focus();      
    }
    return (result);
}

//----------------------------------------------------------
function check_LimitLen(objCheck, strMsg, nMinLen, nMaxLen)
{
    var nLen = Common_String_Trim(objCheck.value).length;    
    if (nLen < nMinLen || nLen > nMaxLen)
    {
        if (strMsg != "")    alert(strMsg);
        objCheck.focus();      
        return (false);	
    }        
    return (true);
}
//----------------------------------------------------------
function Common_GetFormItemValue(objCheck)
{
    var strValue = "";
    if (typeof(objCheck) == "undefined" || objCheck == null)
        return ("");

    var strType = objCheck.type;
    if (strType == "select" || strType == "select-one" || 
        strType == "select-multiple")
        strValue = objCheck.value;
    else if (typeof(objCheck.length) != "undefined")
    {
        if (objCheck.length > 0)
        { 
            var objItem;     
            for (var i=0; i<objCheck.length; i++)
            {
                objItem = objCheck[i];
                if (objItem.type == "checkbox" || objItem.type == "radio")
                {
                    if (objItem.checked == true)
                    {
                        if (strValue != "")
                            strValue += ",";
                        strValue += objItem.value;
                    }
                }
            }
        }
    }
    else if (objCheck.type == "checkbox" || objCheck.type == "radio")
    {
        if (objCheck.checked == true)
            strValue = objCheck.value;
    }
    else
        strValue = objCheck.value;
    return (strValue);
}

function Common_InitFormItemValue(objForm, strCtrlName, strValue)
{
    var objItem = eval("objForm." + strCtrlName);
    if (typeof(objItem) == "undefined" || objItem == null)
        return;

    var strInitValue = "" + strValue;
    if (objItem.type == "select" || objItem.type == "select-one" || 
        objItem.type == "select-multiple")
    {
        //strInitValue = strInitValue.replace(/ /gi, "");     
        strInitValue = Common_String_Trim(strInitValue);
        var arrayValue = strInitValue.split(",");
        for (var j=0; j<arrayValue.length; j++)
        {
            for (var k=0; k<objItem.options.length; k++)
            {
                if (objItem.options[k].value == arrayValue[j])                        
                {
                    objItem.options[k].selected = true;
                    break;
                }
            }
        }
    }
    else if (typeof(objItem.length) != "undefined")
    {
        strInitValue = strInitValue.replace(/ /gi, "");     
        var arrayValue = strInitValue.split(",");
        for (var j=0; j<arrayValue.length; j++)
        {
            for (var k=0; k<objItem.length; k++)
            {
                if (objItem[k].value == arrayValue[j])                        
                {
                    objItem[k].checked = true;
                    break;
                }
            }
        }
    }
    if (objItem.type == "checkbox" || objItem.type == "radio")
        objItem.checked = (objItem.value == strInitValue);
    else 
        objItem.value = strInitValue;
}

function Common_ClearFormItemValue(objForm, strCtrlName, bDisabledExtCtrl)
{
    if (objForm == null || objForm.elements.length <= 0 || strCtrlName == "")
        return;

    var objItem = eval("objForm." + strCtrlName);
    if (typeof(objItem) == "undefined" || objItem == null)
        return;

    if (typeof(objItem.length) != "undefined")
    {
        for (var i=0; i<objItem.length; i++)
        {
            objItem[i].checked = false;
            objItem[i].disabled = false;
            if (typeof(bDisabledExtCtrl) != "undefined" && bDisabledExtCtrl == true)
            {
                var oCtrl = eval("objForm." + strCtrlName + "_ans" + objItem[i].value);
                if (typeof(oCtrl) != "undefined" && oCtrl != null && oCtrl.disabled == false)
                {
                    oCtrl.value = "";
                    oCtrl.disabled = true;
                }
            }    
        }
    }
    else 
    {
        if (objItem.type == "select" || objItem.type == "select-one" || 
            objItem.type == "select-multiple")
            objItem.options[0].selected = true;
        else 
            objItem.value = "";
    }
}

//----------------------------------------------------------
//
// Global Variables : initForm, 
//                    insertInitItem, insertInitCommand
//
//-----------------------------------------------------------
var _arrayInitItem = new Array;
var _arrayInitValue = new Array;
var _arrayInitCommand = new Array;


//----------------------------------------------------------
function insertInitItem(strName, strValue)
{
    var nIndex = _arrayInitItem.length;
    _arrayInitItem[nIndex] = strName;
    _arrayInitValue[nIndex] = strValue.toString();
}

//----------------------------------------------------------
function insertInitCommand(strCommand)
{
    var nIndex = _arrayInitCommand.length;
    _arrayInitCommand[nIndex] = strCommand;
}
  
//---------------------------------------------------------- 
function initForm(objForm)
{
    if (typeof(objForm) == "undefined")
    {
        if (document.forms.length > 0)
            objForm = document.forms[0];
        else
            objForm = null;
    }

    for (var i=0; (objForm != null && i<_arrayInitItem.length); i++)
    {
        var strCtrlName = _arrayInitItem[i];
        var strInitValue = _arrayInitValue[i]; 
        var objItem = Common_InitFormItemValue(objForm, strCtrlName, strInitValue);
    }
    
    for (i=0; i<_arrayInitCommand.length; i++)  
        eval(_arrayInitCommand[i]);    	 
} 
//----------------------------------------------------------
function getEvent()
{   
    if(document.all)    return window.event;              
    func=getEvent.caller;                  
    while(func!=null)
    {          
        var arg0=func.arguments[0];      
        if(arg0)
        {      
            if((arg0.constructor==Event || arg0.constructor ==MouseEvent)      
               || (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation))
            {          
                return arg0;      
            }      
        }      
        func=func.caller;      
    }      
    return null;      
}
