﻿/**
 * Validates form elements.
 */
function validateForm(form) {
    var result = true;
    var elements = form.elements;
    for (var i = 0; (i < elements.length) && result; ++i) {
        var element = elements[i];
        var invalidBackgroundColor = "#DF9FC1";
        var nextSibling = element.nextSibling;

        if ((nextSibling != null) && (nextSibling.nodeType == 1) && (nextSibling.tagName == "A")) {
            
            //validate the field is not null
            if ((nextSibling.className == "nonempty") && (Trim(element.value) == "")) {
                alert("" + nextSibling.firstChild.nodeValue + " 請勿輸入空白.");
                result = false;
            }

            //validate the field is not null nor too long
            if (nextSibling.className == "description") {
                if (Trim(element.value) == "") {
                    alert("" + nextSibling.firstChild.nodeValue + " 請勿輸入空白.");
                    result = false;
                }
                else if (element.value.length > 5000) {
                    alert("" + nextSibling.firstChild.nodeValue + " 請勿輸入超過5000個字元.");
                    result = false;
                }
            }
            
            if (nextSibling.className == "description2") {
                if (Trim(element.value) != "" && element.value.length > 1000) {
                    alert("" + nextSibling.firstChild.nodeValue + " 請勿輸入超過1000個字元.");
                    result = false;
                }
            }

            //no trainling blank character
            if ((nextSibling.className == "generalname") && !isGeneralName(element.value)) {
                alert("" + nextSibling.firstChild.nodeValue + " cannot begin/end with a blank character.");
                result = false;
            }

            //validate the field is not null or http://
            if ((nextSibling.className == "http") && ((Trim(element.value) == "") || (Trim(element.value) == "http://"))) {
                alert("" + nextSibling.firstChild.nodeValue + " 請勿輸入空白.");
                result = false;
            }
            
            //the img format must JPG、GIF、BMP
            if ((nextSibling.className == "imgformat") && (Trim(element.value) != "")) {
                var str,i;
                i=element.value.length;
                str=element.value.substring(i-4,i);
                str=str.toUpperCase();
                if (str!=".JPG" && str!="JPEG" && str!=".GIF" && str!=".BMP"){
                   alert("" + nextSibling.firstChild.nodeValue + " 限jpg、jpeg、gif、bmp四種格式.");
                   result = false;
                }   
            }
            
            //the img format must JPG、GIF、BMP
            if ((nextSibling.className == "imgformat_must")) {
                var str,i;
                i=element.value.length;
                str=element.value.substring(i-4,i);
                str=str.toUpperCase();
                if (str!=".JPG" && str!="JPEG" && str!=".GIF" && str!=".BMP"){
                   alert("" + nextSibling.firstChild.nodeValue + " 限jpg、jpeg、gif、bmp四種格式.");
                   result = false;
                }   
            }

            //validate the field is mail format
            if ((nextSibling.className == "mailformat" && Trim(element.value) != "")) {
                var regstr = /^[\w-]+(\.[\w-]*)*@([\w-]+\.)+[\w-]{2,4}$/;
                if (!regstr.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " 請輸入正確的 E-Mail 格式.");
                    result = false;
                }
            }
            //validate the field is mail format
            if ((nextSibling.className == "mailformat_must" )) {
                var regstr = /^[\w-]+(\.[\w-]*)*@([\w-]+\.)+[\w-]{2,4}$/;
                if (!regstr.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " 請輸入正確的 E-Mail 格式.");
                    result = false;
                }
            }
            //validate the field is URL format
            if ((nextSibling.className == "urlformat") && (Trim(element.value) != "")) {
                var regstr = /^(http|https):\/\/([\w-]+\.)+[\w-]{2,4}(:\d+)?(\/[\w- .\/\?%&=]*)?$/;
                if (!regstr.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " 請輸入正確的 Url 格式.");
                    result = false;
                }
            }
            
            if ((nextSibling.className == "urlformat_must")) {
                var regstr = /^(http|https):\/\/([\w-]+\.)+[\w-]{2,4}(:\d+)?(\/[\w- .\/\?%&=]*)?$/;
                if (!regstr.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " 請輸入正確的 Url 格式.範例：http://www.infinity.com.tw");
                    result = false;
                }
            }

            //validate the field is date format
            if ((nextSibling.className == "dateformat") && (Trim(element.value) != "")) {
                var reg  =  /^[0-9]{4}\/[0-9]{1,2}\/[0-9]{1,2}(\s[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})?$/;
                result = (reg.test(element.value));
                if (!reg.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " 值不正確. 範例： 2005/7/3.");
                    result = false;
                }
            }
            
            if ((nextSibling.className == "dateformat_must" )) {
                var reg  =  /^[0-9]{4}\/[0-9]{1,2}\/[0-9]{1,2}(\s[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})?$/;
                result = (reg.test(element.value));
                if (!reg.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " 不得為空值並且格式如： 2005/7/3.");
                    result = false;
                }
            }
            
            //validate the field is date format
            if ((nextSibling.className == "letter")) {
                var reg  =  /^[a-zA-Z0-9_]+?$/;
                result = (reg.test(element.value));
                if (!reg.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " format is not a correct letter format, bounding a-z,A-Z,0-9,_. For example abc123_. ");
                    result = false;
                }
                else if(element.value.length < 3) {
                    alert("" + nextSibling.firstChild.nodeValue + " is too short. It should at least contain 3 characters. ");
                    result = false;
                }                
                else if(!isNaN(element.value.charAt(0)) || element.value.charAt(0) == "_") {
                    alert("" + nextSibling.firstChild.nodeValue + " format is incorrect, the first character must be letter.");
                    result = false;
                }
            }

            //validate the field is zip format
            if ((nextSibling.className == "zipcode")) {                
                var reg  = /^[+-]?[*0-9]+(\-[0-9]+)?$/;                
                if (!reg.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " format is not a correct zip code format. For example 12345-6789, or 12345.");
                    result = false;
                }
            }

            //validate the field is Code format
            if ((nextSibling.className == "code")) {                
                var reg  =  /^[0-9]+(\-[0-9]+)?$/;
                result = (reg.test(element.value));
                if (!reg.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " format is not a correct Code format.");
                    result = false;
                }
            }
            
            //validate the field which is "SSN/Tax ID"
            if ((nextSibling.className == "ssn")) {                
                var reg = /[0-9]|[a-z]|[A-Z]|-/;
                var regNegative = /[\.]|[_]/;
                //var strLength = element.value.Length;
                result = (reg.test(element.value) && !regNegative.test(element.value));
                if (!reg.test(element.value) || regNegative.test(element.value) || element.value == "") {
                    alert("The format of " + nextSibling.firstChild.nodeValue + " is not correct.");
                    result = false;
                }
            }            
            
            //validate the field is float format
            if ((nextSibling.className == "floatformat")) {
                var regfloat  = /^[0-9]+(\.[0-9]+)?$/;
                if (!regfloat.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " field requires a positive float!");
                    return false;
                }
                else if (element.value <= 0) {
                    alert("" + nextSibling.firstChild.nodeValue + " field should be more than 0!");
                    return false;
                }
            }
            
            //validate the field is money format
            if ((nextSibling.className == "moneyformat")) {
                var regfloat  = /^[0-9]+(\.[0-9]+)?$/;
                var letter = element.value;
                if (letter.substr(0,1) == "$") {
                    letter = letter.substr(1,letter.length-1);
                }
                else{
                    alert("" + nextSibling.firstChild.nodeValue + " field must be a money format. For example $1.00!");
                    return false;
                }
                
                if (!regfloat.test(letter)) {
                    alert("" + nextSibling.firstChild.nodeValue + " field must be a money format. For example $1.00!");
                    return false;
                }
                else if (letter <= 0) {
                    alert("" + nextSibling.firstChild.nodeValue + " field should be more than 0!");
                    return false;
                }
            }
            
            //validate the field is int format
            if ((nextSibling.className == "phone")) {
                var reg  =  /^[0-9*() \-]+(\-[0-9]+)?$/;
                result = (reg.test(element.value));
                if (!reg.test(element.value)) {
                    alert('Please input a phone!');
                    return false;
                }
            }

            //validate the field is int format
            if ((nextSibling.className == "intformat")) {
                var regint  = /^[0-9]*$/;
                if (!regint.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " 請輸入大於 0 的正整數!");
                    result = false;
                }
                else {                
                	if (element.value<=0) {
                	    alert("" + nextSibling.firstChild.nodeValue + " 請輸入大於 0 的正整數!");
                	    result = false;
                	}
                    //if (element.intlength) {
                    //    var intlength = parseInt(element.intlength);
                    //    var max  = Math.pow(2,intlength);
                    //    if (parseFloat(element.value) > max-1) {
                    //        alert("" + nextSibling.firstChild.nodeValue + " is too big, please input it again!");                                                        
                    //        result = false;
                    //    }
                    //}
                }
            }

            //validate the field is int format
            if ((nextSibling.className == "n_intformat")) {
                var regint  = /^([0-9]*)?$/;
                if (!regint.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " field requires a positive integer!");
                    result = false;
                }
                else {                
                    if (element.intlength) {
                        var intlength = parseInt(element.intlength);
                        var max  = Math.pow(2,intlength);
                        if (parseFloat(element.value) > max-1) {
                            alert("" + nextSibling.firstChild.nodeValue + " is too big, please input it again!");                                                        
                            result = false;
                        }
                    }
                }
            }
	    //validate the field is int format
            if ((nextSibling.className == "full_intformat")) {
                var regint  = /^-?[0-9]+$/;
                if (!regint.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " field requires a positive integer!");
                    result = false;
                }
                else {                
                    if (element.intlength) {
                        var intlength = parseInt(element.intlength);
                        var max  = Math.pow(2,intlength);
                        if (parseFloat(element.value) > max-1) {
                            alert("" + nextSibling.firstChild.nodeValue + " is too big, please input it again!");                                                        
                            result = false;
                        }
                    }
                }
            }
        }
        if (!result){
          element.style.backgroundColor = invalidBackgroundColor;
          element.focus();
          element.onkeyup = function(){
            this.style.backgroundColor  = "";
          }
        }
    }
    return result;
}


