﻿
//Init
function ValidateControl_Init(ValidateID,ControlID,ControlType,ControlIDs,MessageID,Required,Expression,OnClientValidate,OptionalText,RequiredText,RequiredErrorText,ExpressionErrorText,OkText,IsPostBack) {
    try {
        var data = new Object()
        data.ValidateID = ValidateID
        data.ControlID = ControlID
        data.ControlType = ControlType
        data.ControlIDs = ControlIDs
        data.MessageID = MessageID
        data.Required = (Required.toLowerCase() == "true")
        data.Expression = Expression
        data.OptionalText = OptionalText
        data.RequiredText = RequiredText
        data.RequiredErrorText = RequiredErrorText
        data.ExpressionErrorText = ExpressionErrorText
        data.OkText = OkText
        data.OnClientValidate = OnClientValidate
        data.IsPostBack = (IsPostBack.toLowerCase() == "true")
        data.ParentID = null
        data.Value = ""
        eval("window.ValidateControl_Data_" + ValidateID + " = data")
        var ct = document.getElementById(ControlID)
        var f = new Function("ValidateControl_Focus('" + ValidateID + "','control')")
        var b = new Function("ValidateControl_Blur('" + ValidateID + "','control')")
        var v = new Function("ValidateControl_Validate('" + ValidateID + "','control')")
        if (data.ControlType == "TextBox") {
            data.Value = ct.value
            ValidateControl_Attach(ct, "focus", f)
            ValidateControl_Attach(ct, "blur", b)
        } else if (data.ControlType == "CheckBox") {
            ValidateControl_Attach(ct,"click", v)
        } else if (data.ControlType == "DropDownList") {
            ValidateControl_Attach(ct, "change", v)
        } else if (data.ControlType == "ListBox") {
            ValidateControl_Attach(ct, "change", v)
        } else if (data.ControlType == "RadioButton") {
            var ar = data.ControlIDs.split(",") 
            for (var i=0;i<ar.length;i++) {
                if (ar[i] != "") {
                    var ct = document.getElementById(ar[i])
                    ValidateControl_Attach(ct, "click", v)
                }
            }            
        } else if ((ControlType == "Button") || (ControlType == "LinkButton")) {
            var r = new Function("return ValidateControl_Validate('"+ValidateID+"','validator')")
            ct.onclick = r //has to return and direct attach return value (no attach posible: FF bug, Safari bug)
        } 
        if (data.IsPostBack == true) {      
            if ((data.ControlType == "Button") || (data.ControlType == "LinkButton")) {
                window.setTimeout("ValidateControl_Validate('"+ValidateID+"','validator')",400)
            } else {
                ValidateControl_Validate(ValidateID,"control")
            }    
        } else {
            document.getElementById(data.MessageID).innerHTML = (data.Required == true) ? data.RequiredText : data.OptionalText 
        }    
    } catch(e) {
        alert("Error: ValidateControl_Init." + ValidateID + ", " + (e.message || e))
    }
}

//Validate
function ValidateControl_Validate(ValidateID,Location,ParentID) {
    try {
        var data = null; eval("data = window.ValidateControl_Data_" + ValidateID)
        var ct = document.getElementById(data.ControlID)
        var valid = true
        var yes = data.OkText 
        var no = (data.Required == true) ? data.RequiredErrorText : data.OkText 
        if (data.ControlType == "TextBox") {
            valid = true
            var s = ValidateControl_Trim(ct.value)
            if (data.Required == true) valid = (s != "")
            if ((valid == true) && (s != "")) {
                if (data.Expression.toLowerCase() == "url") {
                    if (ValidateControl_CheckURL(s) == false) valid = false 
                } else if (data.Expression.toLowerCase() == "ip") {
                    if (ValidateControl_CheckIP(s) == false) valid = false 
                } else if (data.Expression.toLowerCase() == "email") {
                    if (ValidateControl_CheckEmail(s) == false) valid = false 
                } else if (data.Expression.toLowerCase() == "integer") {
                    ct.value = ValidateControl_Integer(s)
                } else if (data.Expression.toLowerCase() == "double") {
                    ct.value = ValidateControl_Double(s)
                } else if (data.Expression.toLowerCase() == "currency") {
                    ct.value = ValidateControl_Currency(s)
                } else if (data.Expression != "") {
                    if (ValidateControl_CheckPattern(data.Expression,s) == false) valid = false 
                }
                if (valid == false) no = data.ExpressionErrorText
            }
        } else if (data.ControlType == "CheckBox") {
            valid = (ct.checked == true)
        } else if (data.ControlType == "DropDownList") {
            var s = (ct.selectedIndex != -1) ? ct.options[ct.selectedIndex].value : ""
            valid = (s != "")
        } else if (data.ControlType == "ListBox") {
            valid = false
            for (var i=0;i<ct.options.length;i++) {
                if ((ct.options[i].selected == true) && (ct.options[i].value != "")) valid = true 
            }
        } else if (data.ControlType == "RadioButton") {
            valid = false
            var ar = data.ControlIDs.split(",") 
            for (var i=0;i<ar.length;i++) {    
                if (ar[i] != "") {
                    var ct = document.getElementById(ar[i])
                    if (ct.checked == true) valid = true
                }
            }            
        } else if ((data.ControlType == "Button") || (data.ControlType == "LinkButton")) {
            valid = true
            var ParentID = ValidateID
            var ar = data.ControlIDs.split(",") 
            for (var i=0;i<ar.length;i++) { 
                if (ar[i] != "") {
                    var s = "ValidateControl_Validate('" + ar[i] + "','validator','"+ParentID+"')"
                    var r = eval(s)
                    if (r == false) valid = false
                }
            }
        }
        if ((valid == true) && (data.OnClientValidate != "")) {
            var f = new Function(data.OnClientValidate)
            var r = f()
            if ((typeof (r) == "undefined") || (r == null)) {
                //
            } else if (typeof (r) == "boolean") {
                if (r == false) valid = false
            } else {
                no = r
                valid = false
            }
        }
        document.getElementById(data.MessageID).innerHTML = (valid == true) ? yes : no
        if ((data.ControlType == "TextBox") || (data.ControlType == "CheckBox") || (data.ControlType == "DropDownList") || (data.ControlType == "ListBox") || (data.ControlType == "RadioButton")) {
            if ((Location == "control") && (data.ParentID != null)) eval("ValidateControl_Validate('" + data.ParentID + "','control')")
            if ((Location == "validator") && (ParentID != null)) data.ParentID = ParentID
        }
        if (Location == "validator") return (valid == true)
    } catch(e) {
        alert("Error: ValidateControl_Validate." + ValidateID + ", " + (e.message || e))
    }
}

//Focus
function ValidateControl_Focus(ValidateID, Location) {
    try {
        var data = null; eval("data = window.ValidateControl_Data_" + ValidateID)
        if (data.ControlType == "TextBox") {
            var ct = document.getElementById(data.ControlID)
            var s = ValidateControl_Trim(ct.value)
            data.Value = s
        }
    } catch(e) {
        alert("Error: ValidateControl_Focus." + ValidateID + ", " + (e.message || e))
    }
}

//Blur
function ValidateControl_Blur(ValidateID, Location) {
    try {
        var data = null; eval("data = window.ValidateControl_Data_" + ValidateID)
        if (data.ControlType == "TextBox") {
            var ct = document.getElementById(data.ControlID)
            var s = ValidateControl_Trim(ct.value)
            if (ct.value != s) ct.value = s
            if (data.Value != s) {
                ValidateControl_Validate(ValidateID, Location) //validate
                data.Value = s
            }    
        }
    } catch(e) {
        alert("Error: ValidateControl_Blur." + ValidateID + ", " + (e.message || e))
    }
}

//Attach
function ValidateControl_Attach(ct,name,f) {
    if (ct.attachEvent != null) { 
        ct.attachEvent("on" + name,f) 
    } else { 
        ct.addEventListener(name,f,true) 
    }
}

//Trim
function ValidateControl_Trim(s) {
    if (s == null) s = ""
    while ((s.length > 0) && (s.charAt(0) == " ")) s = s.substring(1,s.length)
    while ((s.length > 0) && (s.charAt(s.length-1) == " ")) s = s.substring(0,s.length-1) 
    return s
}    

//Pattern
function ValidateControl_CheckPattern(sPattern,sString) {
    try {
        var re = new RegExp(sPattern)
        re.global = true
        re.ignoreCase = false		
        var res = sString.match(re)
        if (res == null) return false
        for (var i=0;i<res.length;i++) {
		    if (res[i].toLowerCase() == sString.toLowerCase()) return true
        }
        return false
	} catch(e) {
		alert("ValidateControl_CheckPattern: " + (e.message || e))
	}	
} 
function ValidateControl_CheckEmail(sEmail) {
    try {
        //var sPattern = "^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"	
        var sPattern = "^([a-zA-Z0-9_\\-])+(\\.([a-zA-Z0-9_\\-])+)*@((\\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\\]))|((([a-zA-Z0-9])+(([\\-])+([a-zA-Z0-9])+)*\\.)+([a-zA-Z])+(([\\-])+([a-zA-Z0-9])+)*))$"
        return ValidateControl_CheckPattern(sPattern,sEmail)
	} catch(e) {
		alert("ValidateControl_CheckEmail: " + (e.message || e))
	}	
}
function ValidateControl_CheckURL(sURL) {
    try {
        var sPattern =  "^(http|https|ftp):\\/\\/([\\w.]+\\/?)\\S*$"
        return ValidateControl_CheckPattern(sPattern,sURL)
	} catch(e) {
		alert("ValidateControl_CheckURL: " + (e.message || e))
	}	
}
function ValidateControl_CheckIP(sIP) {
    try {
        //var sPattern = "^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
        var sPattern = "^(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$"
        return ValidateControl_CheckPattern(sPattern,sIP)
	} catch(e) {
		alert("ValidateControl_CheckIP: " + (e.message || e))
	}	
}    
function ValidateControl_Integer(sInteger) {
    var s = "0123456789"
    var r = ""
    for (var i=0;i<sInteger.length;i++) {
        if (s.indexOf(sInteger.charAt(i)) != -1) r += sInteger.charAt(i)
    }
    r = parseInt(parseFloat(r || "0")) + ""
    return r || "0"
} 
function ValidateControl_Double(sDouble) {    
    var s = "0123456789."
    var r = ""
    sDouble = sDouble.replace(",",".") 
    for (var i=0;i<sDouble.length;i++) {
        if (s.indexOf(sDouble.charAt(i)) != -1) r += sDouble.charAt(i)
    }
    r = parseFloat(r || "0") + ""
    r = r.replace(".",",") 
    return r
} 
function ValidateControl_Currency(sCurrency) {
    var s = "0123456789."
    var r = ""
    sCurrency = sCurrency.replace(",",".") 
    for (var i=0;i<sCurrency.length;i++) {
        if (s.indexOf(sCurrency.charAt(i)) != -1) r += sCurrency.charAt(i)
    }
    r = parseFloat(r || "0")
    if (r == 0) return "0,00"
    r = Math.round(r * 100) 
    r = r + ""
    r = r.substring(0,r.length-2) + "," + r.substring(r.length-2)
    return r
} 
