﻿// JScript File

function checkform()
{
    var message = "";
    var fields;
    var retval = true;
    var req = getElement('requiredfields');
    if (req != null)
    {
        fields = req.value.split(',');
        for(i=0;i<fields.length;i++) {
            if (testField(fields[i],""))
            {
                message = "One or more required fields are empty.\n";
                retval=false;
                break;
            }
        }
    }
    else
    {
        message = "Missing configuration field: requiredfields.\n";
        retval = false;
    }
    var email = getElement('emailfields');
    if (email != null)
    {
        fields = email.value.split(',');
        for(i=0;i<fields.length;i++) {
            if (testEmail(fields[i]))
            {
                message += "One or more email fields are invalid.\n";
                retval=false;
                break;
            }
        }
    }
    else
    {
        message += "Missing configuration field: emailfields.\n";
        retval = false;
    }
    var matching = getElement('matchingpairs');
    if (matching != null)
    {
        var pairs = matching.value.split(',');
        for(i=0;i<pairs.length;i++) {
            fields = pairs[i].split('=');
            if (!matchFields(fields[0], fields[1]))
            {
                message += "Fields must match.\n";
                retval=false;
                break;
            }
        }
    }
    else
    {
        message += "Missing configuration field: matchingpairs.\n";
        retval = false;
    }
    if (!retval)
    {
        alert(message);
    }

    return retval;
}
    
function disablebutton(itemid) {
	
	var theItem = getElement(itemid);
	theItem.value='Please wait...';
	theItem.disabled=true;
}

function matchFields(itemid1, itemid2)
{
	return (getValue(itemid1)==getValue(itemid2));
}

function testField(itemid, value) {
	return (getValue(itemid)==value);
}

function testNumericField(itemid) {
	return (isNaN(getValue(itemid)));
}

function testEmail(element) {
     var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
     var regex = new RegExp(emailReg);
     var src = getValue(element);
     return !regex.test(src);
}

function setFocus(itemid) {
	getElement(itemid).focus();
}

function getValue(itemid) {
	return getElement(itemid).value;
}

function setValue(itemid, value) {
	getElement(itemid).value = value;
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked==true)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked==true) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function setInnerHTML(itemid, value) {
	getElement(itemid).innerHTML = value;
}

function setParentInnerHTML(itemid, value) {
	getParentElement(itemid).innerHTML = value;
}

function getElement(itemid) {
	var theElement;
	if (document.all) {
		eval("theElement = document.all."+itemid+";");
	}
	else {
		if (navigator.userAgent.indexOf("Gecko")!=-1) {// is NS6 ?
			theElement = document.getElementById(itemid);
		}
		else {
			eval("theElement = document.layers['"+itemid+"'];");
		}
	}
	return theElement;
}

function getParentElement(itemid) {
	var theElement;
	if (document.all) {
		eval("theElement = window.opener.document.all."+itemid+";");
	}
	else {
		if (navigator.userAgent.indexOf("Gecko")!=-1) {// is NS6 ?
			theElement = window.opener.document.getElementById(itemid);
		}
		else {
			eval("theElement = window.opener.document.layers['"+itemid+"'];");
		}
	}
	return theElement;
}

function getWindowElement(thewin, itemid) {
	var theElement;
	if (document.all) {
		eval("theElement = thewin.document.all."+itemid+";");
	}
	else {
		if (navigator.userAgent.indexOf("Gecko")!=-1) {// is NS6 ?
			theElement = thewin.document.getElementById(itemid);
		}
		else {
			eval("theElement = thewin.document.layers['"+itemid+"'];");
		}
	}
	return theElement;
}

function getCookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

function setCookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}