function toUpper(control_id) {
    var strValue = document.getElementById(control_id).value;
    var strNewValue = "";
    var strPref = "";
    for (var i = 0; i < strValue.length; i++) {
        if (strPref == "" || strPref == " ")
            strNewValue += strValue.charAt(i).toUpperCase();
        else
            strNewValue += strValue.charAt(i);
        strPref = strValue.charAt(i);
    }
    document.getElementById(control_id).value = strNewValue;
}
function toUpperAll(control_id) {
    var strValue = document.getElementById(control_id).value = document.getElementById(control_id).value.toUpperCase();
}

function isValidEmail2(who) {
    var email = /^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
    return (email.test(who));
}
function isValidEmail(email) {
    if (email == null) {
        return true;
    }
    if (email.length == 0) {
        return true;
    }
    if (!allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >= 0) { // two periods in a row is not valid
        return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
        return false;
    }
    if (email.indexOf("@") > 0) {
        var s_count = email.indexOf("@");
        if (email.indexOf("@", s_count + 1) >= 0)
            return false;
    }
    return true;
}

function allValidChars(email) {
    var parsed = true;
    var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
    for (var i = 0; i < email.length; i++) {
        var letter = email.charAt(i).toLowerCase();
        if (validchars.indexOf(letter) != -1)
            continue;
        parsed = false;
        break;
    }
    return parsed;
}
function mailValidator(control_id, error_control_id, error_control_id2) {
    var strValue = document.getElementById(control_id).value;
    hideError(error_control_id);
    if (strValue == "")
        return;
    if (isValidEmail2(strValue))
        return;
    showError(error_control_id, error_control_id2, 'Please enter a valid value for E-mail');
}
function hideError(control_id) {
    document.getElementById(control_id + '_input').value = "";
    document.getElementById(control_id).innerHTML = "";
}
function showError(control_id, control_id2, message) {
    document.getElementById(control_id + '_input').value = "Error";
    if (document.getElementById(control_id2) != undefined) {
        var val1 = document.getElementById(control_id2).innerHTML;
        var val2 = "Email";
        var val3 = "E-mail";
        if (val1.indexOf(message) != -1 || val1.indexOf(val2) != -1 || val1.indexOf(val3) != -1)
            return;
    }
    document.getElementById(control_id).innerHTML = message;
}

/*
Developed by Robert Nyman, http://www.robertnyman.com
Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/
var getElementsByClassName = function(className, tag, elm) {
    if (document.getElementsByClassName) {
        getElementsByClassName = function(className, tag, elm) {
            elm = elm || document;
            var elements = elm.getElementsByClassName(className),
				nodeName = (tag) ? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
            for (var i = 0, il = elements.length; i < il; i += 1) {
                current = elements[i];
                if (!nodeName || nodeName.test(current.nodeName)) {
                    returnElements.push(current);
                }
            }
            return returnElements;
        };
    }
    else if (document.evaluate) {
        getElementsByClassName = function(className, tag, elm) {
            tag = tag || "*";
            elm = elm || document;
            var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace) ? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
            for (var j = 0, jl = classes.length; j < jl; j += 1) {
                classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
            }
            try {
                elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
            }
            catch (e) {
                elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
            }
            while ((node = elements.iterateNext())) {
                returnElements.push(node);
            }
            return returnElements;
        };
    }
    else {
        getElementsByClassName = function(className, tag, elm) {
            tag = tag || "*";
            elm = elm || document;
            var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all) ? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
            for (var k = 0, kl = classes.length; k < kl; k += 1) {
                classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
            }
            for (var l = 0, ll = elements.length; l < ll; l += 1) {
                current = elements[l];
                match = false;
                for (var m = 0, ml = classesToCheck.length; m < ml; m += 1) {
                    match = classesToCheck[m].test(current.className);
                    if (!match) {
                        break;
                    }
                }
                if (match) {
                    returnElements.push(current);
                }
            }
            return returnElements;
        };
    }
    return getElementsByClassName(className, tag, elm);
};

function SetUniqueRadioButton(nameregex, current) {
    re = new RegExp(nameregex);
    for (i = 0; i < document.forms[0].elements.length; i++) {
        elm = document.forms[0].elements[i];
        if (elm.type == 'radio') {
            if (re.test(elm.name)) {
                elm.checked = false;
                elm.className = "nonSelectedRadio";
            }
        }
    }
    current.checked = true;
}

function SetUniqueCheckButtonUsingParent(attributename, groupname, current) {
    //alert(current.getAttribute("customgroup"));
    //search in the parent
    //alert('kkk');
    var defaultvalue = current.checked;
    for (i = 0; i < document.forms[0].elements.length; i++) {
        elm = document.forms[0].elements[i];

        if (elm.type == 'checkbox') {
            if (!elm.parentElement) {
                if (elm.parentNode.getAttribute(attributename) == groupname) {
                    elm.checked = false;
                }
            }
            else {
                if (elm.parentElement.getAttribute(attributename) == groupname) 
                    elm.checked = false;
            }
        }
    }
    current.checked = defaultvalue;

}
