﻿// Global Messages //
var deleteRecordMsg = 'Do you really want to delete?';
var tickCheckBoxMsg = 'Please tick one of the check boxes to delete.';

// Two hidden fields are required before using this function
//    <input type="hidden" id="__EVENTTARGET" name="__EVENTTARGET" />
//    <input type="hidden" id="__EVENTARGUMENT" name="__EVENTARGUMENT" />
function __doPostBack(eventTarget, eventArgument) 
{
    var theForm = document.frmMain;
    if (!theForm.onsubmit || (theForm.onsubmit() != false))
    {
        theForm.__EVENTTARGET.value = eventTarget;  
        theForm.__EVENTARGUMENT.value = eventArgument;  
        theForm.submit();  
    }
}  

function fnBlinkOn(objectID, count)
{
    if (document.getElementById(objectID) != null)
    {
        document.getElementById(objectID).className = 'BlankOn';
        count = count + 1;
        setTimeout("fnBlinkOff(\"" + objectID + "\", " + count + ")", 450);
    }
}

function fnBlinkOff(objectID, count)
{
    if (document.getElementById(objectID) != null)
    {
        document.getElementById(objectID).className = 'BlankOff';
        if (count <= 3)
        {
	        setTimeout("fnBlinkOn('" + objectID + "', " + count + ")", 450);
        }
    }
}

function isEmpty(ID, fieldName)
{
    if(document.getElementById(ID).value == "")
    {
         alert(fieldName + " is required field.");
         document.getElementById(ID).focus();
         return true;
    }
}



function isDateFormat(ID, fieldName)
{
    var reg = /^(?:(?:(?:0?[1-9]|1\d|2[0-8])\/(?:0?[1-9]|1[0-2]))\/(?:(?:1[6-9]|[2-9]\d)\d{2}))$|^(?:(?:(?:31\/0?[13578]|1[02])|(?:(?:29|30)\/(?:0?[1,3-9]|1[0-2])))\/(?:(?:1[6-9]|[2-9]\d)\d{2}))$|^(?:29\/0?2\/(?:(?:(?:1[6-9]|[2-9]\d)(?:0[48]|[2468][048]|[13579][26]))))$/

    if (document.getElementById(ID).value.match(reg)) {
        return true;
    } 
    else 
    {
        alert(fieldName + " is not correct datetime format.");
        document.getElementById(ID).focus();
        return false
    }
}

function manageCheckBox(formName, headerChkID, rowChkID)
{
    var isChecked = document.getElementById(headerChkID).checked;
    if (isChecked)
    {
        
        tickCheckBox(formName, rowChkID, true);
    }
    else
    {
        tickCheckBox(formName, rowChkID, false);
    }
}

function tickCheckBox(formName, rowChkID, isChecked)
{
    if(!document.forms[formName])
        return;
    var objCheckBoxes = document.forms[formName].elements[rowChkID];
    if(!objCheckBoxes)
        return;
    var countCheckBoxes = objCheckBoxes.length;
    if(!countCheckBoxes)
    {
        objCheckBoxes.checked = isChecked;
    }
    else
    {
        // set the check value for all check boxes
        for(var i = 0; i < countCheckBoxes; i++)
        {
	        objCheckBoxes[i].checked = isChecked;
	    }
	}
}

function tickHeader(formName, headerChkID, rowChkID)
{
    if(isAllChecked(formName, rowChkID))
    {
        document.getElementById(headerChkID).checked = true;
    }
    else
    {
        document.getElementById(headerChkID).checked = false;
    }
}

function isAllChecked(formName, rowChkID)
{
    var isChecked = true;

    var objCheckBoxes = document.forms[formName].elements[rowChkID];
    var countCheckBoxes = objCheckBoxes.length;

    if(countCheckBoxes != null)
    {
        for(var i = 0; i < countCheckBoxes; i++)
        {
            if(!objCheckBoxes[i].checked)
            {
                isChecked = false;
            }
        }
    }
    else    //if there is only one checkbox (row) objCheckBoxes.length returns null rather than 0
    {
        isChecked = document.getElementById(rowChkID).checked;
    }
    return isChecked
}

function isAnyChecked(formName, rowChkID)
{
    var isChecked = false;

    var objCheckBoxes = document.forms[formName].elements[rowChkID];
    var countCheckBoxes = objCheckBoxes.length;

    if(countCheckBoxes != null)
    {
        for(var i = 0; i < countCheckBoxes; i++)
        {
            if(objCheckBoxes[i].checked)
            {
                isChecked = true;
            }
        }
    }
    else    //if there is only one checkbox (row) objCheckBoxes.length returns null rather than 0
    {
        isChecked = document.getElementById(rowChkID).checked;
    }
    return isChecked
}

function getCheckedString(formName, rowChkID)
{
    var checkedString = '';
    var objCheckBoxes = document.forms[formName].elements[rowChkID];
    var countCheckBoxes = objCheckBoxes.length;

    if(countCheckBoxes != null)
    {
        for(var i = 0; i < countCheckBoxes; i++)
        {
            if(objCheckBoxes[i].checked)
            {
                if (checkedString == '')
                {
                    checkedString = objCheckBoxes[i].value;
                }
                else
                {
                    checkedString = checkedString + ', ' + objCheckBoxes[i].value;
                }
            }
        }
    }
    else    //if there is only one checkbox (row) objCheckBoxes.length returns null rather than 0
    {
        if (checkedString == '')
        {
            checkedString = document.getElementById(rowChkID).value;
        }
        else
        {
            checkedString = checkedString + ', ' + document.getElementById(rowChkID).value;
        }
    }
    return checkedString
}

function checkDelete(formName, rowChkID)
{
    if (isAnyChecked(formName, rowChkID))
    {
        return window.confirm(deleteRecordMsg+'\n\n- ID: (' + getCheckedString(formName, rowChkID) +') will be deleted permanently.')
    }
    else
    {
        alert(tickCheckBoxMsg);
        return false;
    }
}

function isInteger(value, extra)
{
    var validCars = "0123456789" + extra;
    var isValid=true;
    var Char;

    for (i = 0; i < value.length && isValid == true; i++) 
    { 
        Char = value.charAt(i); 
        if (validCars.indexOf(Char) == -1) 
        {
            isValid = false;
        }
    }
    return isValid;
}

function isNumeric(ID, fieldName)
{
    if(!isInteger(document.getElementById(ID).value, ''))
    {
         alert(fieldName + " is not numeric.");
         document.getElementById(ID).focus();
         return false;
    }
    return true;
}

function isEmail(ID, fieldName) {
	var emailstring = document.getElementById(ID).value;
	var ampIndex = emailstring.indexOf("@");
	var afterAmp = emailstring.substring((ampIndex + 1), emailstring.length);
		// find a dot in the portion of the string after the ampersand only
	var dotIndex = afterAmp.indexOf(".");
		// determine dot position in entire string (not just after amp portion)
	dotIndex = dotIndex + ampIndex + 1;
		// afterAmp will be portion of string from ampersand to dot
	afterAmp = emailstring.substring((ampIndex + 1), dotIndex);
		// afterDot will be portion of string from dot to end of string
	var afterDot = emailstring.substring((dotIndex + 1), emailstring.length);
	var beforeAmp = emailstring.substring(0,(ampIndex));
		//old regex did not allow subdomains and dots in names
		//var email_regex = /^[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+(\.[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~])*\@(((\w+[\w\d\-]*[\w\d]\.)+(\w+[\w\d\-]*[\w\d]))|((\d{1,3}\.){3}\d{1,3}))$/;
	var email_regex = /^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}/ 
		// index of -1 means "not found"
	if ((emailstring.indexOf("@") != "-1") &&
		(emailstring.length > 5) &&
		(afterAmp.length > 0) &&
		(beforeAmp.length > 1) &&
		(afterDot.length > 1) &&
		(email_regex.test(emailstring)) ) {
        return true;
    } 
    else 
    {
        alert(fieldName + " is not valid.");
        document.getElementById(ID).focus();
        return false;
    }
}

function checkBrowser()
{
    var rtn = 'ie';
    
    if (document.getElementById&&!document.all)
        rtn = 'firefox';
    else if (navigator.appName=="Netscape" && navigator.appVersion.charAt(0)=="4")
        rtn = 'ns';
    
    return rtn;
}

function popup(url, title, width, height, scrollbars, resizable)
{
    var myWindow = window.open(url, title, 'width=' + width + ', height=' + height + ', scrollbars=' + scrollbars + ', resizable=' + resizable)
}

function copy(txt)
{
    if (window.clipboardData)
    {
        // only IE
        window.clipboardData.setData("Text", txt);
    }
    else if (window.netscape) 
    { 
        netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

        var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
        if (!clip) return;

        var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
        if (!trans) return;

        trans.addDataFlavor('text/unicode');

        var str = new Object();
        var len = new Object();

        var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);

        var copytext = txt;

        str.data = copytext;

        trans.setTransferData("text/unicode", str, copytext.length*2);

        var clipid=Components.interfaces.nsIClipboard;

        if (!clip) return false;

        clip.setData(trans, null, clipid.kGlobalClipboard);

    }
    
    alert("Following info was copied to your clipboard:\n\n" + txt.toString().substring(0, 100) + " ...");
    return false;
}

function breakoutFrame()
{
    if (top.location != location) {
        top.location.href = document.location.href ;
    }
}