﻿
var MFKResult = true;
function validate(container) {
    MFKResult = true;
    $(".MFKValidate").remove();
    var Elements = $(container + " [formvalidation=true]");
    jQuery.each(Elements, function (i) {
   
        if ($(this).attr("type") == "text" || $(this).attr("type") == "password" || $(this).attr("type") == "textarea" || $(this).attr("type") == "select-one") {

            if ($(this).val() != "") {
                validateAttibuteControll(this);
            }
            else
                validateAction(this);

        }

        if ($(this).attr("type") == "radio") {

            validateRadio(this);
        }

        if ($(this).attr("type") == "checkbox") {

            if ($(this).is(":checked") == false)
                validateAction(this);

        }


    });
  
    return MFKResult;
}

 function validateAttibuteControll(ElementItem) 
 {
  
     if ($(ElementItem).attr("validationtype") != undefined) 
        {

          switch($(ElementItem).attr("validationtype"))
          {
            case "phone":
                if (!validatePhone($(ElementItem).val()))
                    validateAction(ElementItem);          
                    break;
            case "email":               
                if (!validateEmail($(ElementItem).val())) 
                    validateAction(ElementItem);
                    break;
            case "numeric":                
                if (!IsNumeric($(ElementItem).val()))
                    validateAction(ElementItem);
                    break;
                    
         }         
       }

     
     if ($(ElementItem).attr("minvalue") != undefined) {
            if (parseFloat($(ElementItem).attr("minvalue")) > parseFloat($(ElementItem).val())) {
                validateAction(ElementItem);
            }
      }

    if ($(ElementItem).attr("maxvalue") != undefined) {
      if (parseFloat($(ElementItem).attr("maxvalue")) < parseFloat($(ElementItem).val())) {
            validateAction(ElementItem);
            }
       }


    if ($(ElementItem).attr("minlength") != undefined) {
        if ($(ElementItem).val().length < parseFloat($(ElementItem).attr("minlength"))) {
            validateAction(ElementItem);
        }
    }


    if ($(ElementItem).attr("compareto") != undefined) {
        if ($(ElementItem).val() != $($(ElementItem).attr("compareto")).val()) {
             validateAction(ElementItem,"compareAlert");
        }
    }

     if ($(ElementItem).attr("notequals") != undefined) {
      
         if ($(ElementItem).val() == $(ElementItem).attr("notequals")) {
             validateAction(ElementItem);
         }
     }

  
}


function validateAction(ElementItem, ErrorType) {
    MFKResult = false;
    var Positions = $(ElementItem).offset();
    var TopPos = Positions.top;
    var LeftPos = Positions.left;
    var ElementHeight = ElementItem.offsetHeight;
    var ElementWidth = ElementItem.offsetWidth;
    var ErrorValue;
   
    if (ErrorType!=undefined && $(ElementItem).attr(ErrorType) != undefined) {
        ErrorValue = $(ElementItem).attr(ErrorType);
       }
    else {
       
        ErrorValue = $(ElementItem).attr("error");
    }

    if (ErrorValue != undefined) {
        var ValidateID = "";
        if ($(ElementItem).attr("type") == "radio") {
            ValidateID = $(ElementItem).attr("name");
            $("[name=" + ValidateID + "]").focus(function () { validateFocus(this) });
        }
        else
            ValidateID = $(ElementItem).attr("ID");

        $(ElementItem).after("<div id='MFKValidate" +ValidateID+ "' class='MFKValidate' >" + ErrorValue + "</div>");
        $(ElementItem).unbind("focus").unbind("change").unbind("keyup");
        $(ElementItem).focus(function () { validateFocus(ElementItem) });        
        $(ElementItem).change(function () { validateChange(ElementItem) }).keyup(function () { validateChange(ElementItem) });
    } 
}


function validateRadio(ElementItem) {
   
    var GroupName = $(ElementItem).attr("name");
    $("[name=" + GroupName + "]").unbind("click");
    $("[name=" + GroupName + "]").click(function () { validateFocus(this) });
    var SelectedVal = $("[name=" + GroupName + "]:checked").val();           
    if (SelectedVal==undefined)
        validateAction(ElementItem);
}


function validateFocus(ElementItem) {

    if ($(ElementItem).attr("type") == "radio")
        $("#MFKValidate" + $(ElementItem).attr("name")).remove();
    else
        $("#MFKValidate" + $(ElementItem).attr("ID")).remove();

}


function validateChange(ElementItem) {

    validateFocus(ElementItem);
    validateAttibuteControll(ElementItem);

}


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);
}


function IsNumeric(strString)
{
    var strValidChars = "0123456789.-";
    var strChar;
    var blnResult = true;
    if (strString.length == 0) return false;
    for (i = 0; i < strString.length && blnResult == true; i++) {
        strChar = strString.charAt(i);
        if (strValidChars.indexOf(strChar) == -1) {
            blnResult = false;
        }
    }
    return blnResult;
}


