﻿function handleEnter(field, event) {
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (keyCode == 13) {
        var i;
        for (i = 0; i < field.form.elements.length; i++)
            if (field == field.form.elements[i])
                break;
        i = (i + 1) % field.form.elements.length;
        field.form.elements[i].focus();
        return false;
    }
    else
        return true;
}
function GetQueryString(Sname) {
    try {
        var urlquery = window.location.search.substring(1);
        var querya = urlquery.split("&");
        var queryb;
        for (i = 0; i < querya.length; i++) {
            queryb = querya[i].split("=");
            if (queryb[0] == Sname) {
                return queryb[1];
            }
        }
        return "";
    } catch (e) {
        return "";
    }
}
function EncodeVariables(encodevariables) {
    try {
        encodedHtml = CheckStringForSQL(encodevariables);

        while (encodedHtml.indexOf("¿") > -1) encodedHtml = encodedHtml.replace("¿", "&#191;");
        while (encodedHtml.indexOf("ß") > -1) encodedHtml = encodedHtml.replace("ß", "&#223;");
        while (encodedHtml.indexOf("'") > -1) encodedHtml = encodedHtml.replace("'", "&#39;");
        while (encodedHtml.indexOf(",") > -1) encodedHtml = encodedHtml.replace(",", "&#44;");
        return encodedHtml;
    } catch (e) {
        return "";
    }

}


function CheckForm(ContainerID, normalbgcolor, errbgcolor, normalcolor, errcolor) {
    var returnvalidation = true;
    $(ContainerID + " input," + ContainerID + " select," + ContainerID + " textarea").each(function (i) {
        if ($("input[name='" + this.name + "'],select[name='" + this.name + "'],radio[name='" + this.name + "'],textarea[name='" + this.name + "']").attr("formvalidation") == "true") {
            $("input[name='" + this.name + "'],select[name='" + this.name + "'],radio[name='" + this.name + "'],textarea[name='" + this.name + "']").each(function (i) {
                if (returnvalidation) {
                    if ($("#" + this.id).attr("type") == "text" || $("#" + this.id).attr("type") == "password") {
                        var textvalue = $("input[name='" + this.name + "']").val();
                        switch ($("#" + this.id).attr("validationtype")) {
                            case undefined:
                                if (textvalue.length == 0) {
                                    $("input[name='" + this.name + "']").css("background-color", errbgcolor);
                                    $("input[name='" + this.name + "']").css("color", errcolor);
                                    $("input[name='" + this.name + "']").focus();
                                    returnvalidation = false;

                                }
                                else {
                                    $("input[name='" + this.name + "']").css("background-color", normalbgcolor);
                                    $("input[name='" + this.name + "']").css("color", normalcolor);
                                }
                                break;
                            case "EMail":
                                if (!validateEmail(textvalue) || !CheckEmail(this.id)) {
                                    returnvalidation = false;
                                    $("input[name='" + this.name + "']").css("background-color", errbgcolor);
                                    $("input[name='" + this.name + "']").css("color", errcolor);
                                    $("input[name='" + this.name + "']").focus();
                                }
                                else {
                                    $("input[name='" + this.name + "']").css("background-color", normalbgcolor);
                                    $("input[name='" + this.name + "']").css("color", normalcolor);
                                }
                                break;
                            case "Phone":
                                if (!validatePhone(textvalue)) {
                                    returnvalidation = false;
                                    $("input[name='" + this.name + "']").css("background-color", errbgcolor);
                                    $("input[name='" + this.name + "']").css("color", errcolor);
                                    $("input[name='" + this.name + "']").focus();
                                }
                                else {
                                    $("input[name='" + this.name + "']").css("background-color", normalbgcolor);
                                    $("input[name='" + this.name + "']").css("color", normalcolor);
                                }
                                break;
                            case "Password":
                                if (textvalue.length < 6) {
                                    returnvalidation = false;
                                    $("input[name='" + this.name + "']").css("background-color", errbgcolor);
                                    $("input[name='" + this.name + "']").css("color", errcolor);
                                    $("input[name='" + this.name + "']").focus();
                                }
                                else {
                                    $("input[name='" + this.name + "']").css("background-color", normalbgcolor);
                                    $("input[name='" + this.name + "']").css("color", normalcolor);
                                }
                                break;
                            case "SecureCode":
                                if (textvalue.length < 6) {
                                    returnvalidation = false;
                                    $("input[name='" + this.name + "']").css("background-color", errbgcolor);
                                    $("input[name='" + this.name + "']").css("color", errcolor);
                                    $("input[name='" + this.name + "']").focus();
                                }
                                else {
                                    $("input[name='" + this.name + "']").css("background-color", normalbgcolor);
                                    $("input[name='" + this.name + "']").css("color", normalcolor);
                                }
                                break;
                            case "TCNo":
                                if (textvalue.length < 11) {
                                    returnvalidation = false;
                                    $("input[name='" + this.name + "']").css("background-color", errbgcolor);
                                    $("input[name='" + this.name + "']").css("color", errcolor);
                                    $("input[name='" + this.name + "']").focus();
                                }
                                else if (textvalue.length == 11) {
                                    try {
                                        toplam = Number(textvalue.substring(0, 1)) + Number(textvalue.substring(1, 2)) + Number(textvalue.substring(2, 3)) + Number(textvalue.substring(3, 4)) + Number(textvalue.substring(4, 5)) + Number(textvalue.substring(5, 6)) + Number(textvalue.substring(6, 7)) + Number(textvalue.substring(7, 8)) + Number(textvalue.substring(8, 9)) + Number(textvalue.substring(9, 10));
                                        if (Number(textvalue.substring(10, 11)) == (toplam % 10)) {
                                            $("input[name='" + this.name + "']").css("background-color", normalbgcolor);
                                            $("input[name='" + this.name + "']").css("color", normalcolor);
                                        }
                                        else {
                                            returnvalidation = false;
                                            $("input[name='" + this.name + "']").css("background-color", errbgcolor);
                                            $("input[name='" + this.name + "']").css("color", errcolor);
                                            $("input[name='" + this.name + "']").focus();
                                        }
                                    } catch (e) {
                                        returnvalidation = false;
                                        $("input[name='" + this.name + "']").css("background-color", errbgcolor);
                                        $("input[name='" + this.name + "']").css("color", errcolor);
                                        $("input[name='" + this.name + "']").focus();
                                    }
                                    $("input[name='" + this.name + "']").css("background-color", normalbgcolor);
                                    $("input[name='" + this.name + "']").css("color", normalcolor);
                                }
                                else {
                                    $("input[name='" + this.name + "']").css("background-color", normalbgcolor);
                                    $("input[name='" + this.name + "']").css("color", normalcolor);
                                }
                                break;
                        }

                    }
                    else if ($("#" + this.id).attr("type") == "select-one") {
                        var selectedvalue = $("select[name='" + this.name + "']").val().trim();
                        if (selectedvalue.length == 0) {
                            $("select[name='" + this.name + "']").css("background-color", errbgcolor);
                            $("select[name='" + this.name + "']").css("color", errcolor);
                            $("select[name='" + this.name + "']").focus();
                            returnvalidation = false;
                            return false;
                        }
                        else {
                            $("select[name='" + this.name + "']").css("background-color", normalbgcolor);
                            $("select[name='" + this.name + "']").css("color", normalcolor);
                        }
                    }
                    else if ($("#" + this.id).attr("type") == "radio") {
                        var selectedvalue = $("input[name='" + this.name + "']:checked").val().trim();
                        if (selectedvalue == undefined || selectedvalue.length == 0) {
                            $("radio[name='" + this.name + "']").css("background-color", errbgcolor);
                            $("radio[name='" + this.name + "']").css("color", errcolor);
                            $("radio[name='" + this.name + "']").focus();
                            returnvalidation = false;
                            return false;
                        }
                        else {
                            $("radio[name='" + this.name + "']").css("background-color", normalbgcolor);
                            $("radio[name='" + this.name + "']").css("color", normalcolor);
                        }
                    }
                    else if (this.tagName.toLowerCase() == "textarea") {
                        var textvalue = $("textarea[name='" + this.name + "']").val();
                        if (textvalue.length == 0) {
                            $("textarea[name='" + this.name + "']").css("background-color", errbgcolor);
                            $("textarea[name='" + this.name + "']").css("color", errcolor);
                            $("textarea[name='" + this.name + "']").focus();
                            returnvalidation = false;

                        }
                        else {
                            $("textarea[name='" + this.name + "']").css("background-color", normalbgcolor);
                            $("textarea[name='" + this.name + "']").css("color", normalcolor);
                        }
                    }
                }
            });
        }
    });
    return returnvalidation;
}
function LoadDropdown(sourceid, targetid, url, ids, values, tag, paramtype) {
    var Drp_ListData = new Array();
    var lids = ids.split('ß');
    var lvalues = values.split('ß');
    var strparam = "";
    var paramprechar = "";

    switch (paramtype) {
        case "string": paramprechar = "'";
            break;
        case "int": paramprechar = "";
            break;
    }
    for (i = 0; i < lids.length; i++) {
        if (lvalues[i].length == 0) strparam += lids[i] + ":" + paramprechar + "0";
        else strparam += lids[i] + ":" + paramprechar + lvalues[i];
        if (i < lids.length - 1) strparam += ",";
    }
    $.ajax({
        type: "POST",
        url: url,
        data: "{" + strparam + paramprechar + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            Drp_ListData = msg.d;
            $("#" + targetid + " > " + tag).remove();
            for (i = 0; i < Drp_ListData.length; i++) {
                var objvalue = new Array();
                objvalue = Drp_ListData[i].split('ß');
                $("#" + targetid).append("<" + tag + " id='" + tag + "-" + objvalue[0] + "' value='" + objvalue[0] + "'>" + objvalue[1] + "</" + tag + ">");
            }
        },
        error: function (msg) {
        }
    });
}
function CheckEmail(sourceid) {
    var rvalue = true;
//    $.ajax({
//        type: "POST",
//        url: "/Content/WebService/AritabWebService.asmx/CheckDuplicateEmail",
//        data: "{Email:'" + $("#" + sourceid).val() + "'}",
//        contentType: "application/json; charset=utf-8",
//        dataType: "json",
//        success: function (msg) {
//            if (msg.d == true) rvalue = true;
//            else rvalue = false;
//        },
//        error: function (msg) {
//            rvalue = false;
//        }
//    });
    return rvalue;
}

function validatePhone(elementValue) {
    var phone2 = /^(0)[2-9][0-9][0-9]([0-9]){7}$/;
    return phone2.test(elementValue);
}

function validateEmail(elementValue) {
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    return emailPattern.test(elementValue);
}

