﻿
var MFKResult = true;
var FirstElement = null;
var MFKCounter = 0;
function validate(container) {
    MFKResult = true;
    MFKCounter = 0;
    $(".MFKValidate").remove();
    var Elements = $(container + " [formvalidation=true]");
    $(container + " [formvalidation=true]").unbind("click");
    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("error") != undefined) {
        if ($(ElementItem).val() == $(ElementItem).attr("error")) {
            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) {
   
    MFKCounter++;
    MFKResult = false;
    $(ElementItem).val($(ElementItem).attr("error"));
    $(ElementItem).click(function () { validateClick(ElementItem) });
     
  
}


function validateRadio(ElementItem) {
   
    var GroupName = $(ElementItem).attr("name");
    $("[name=" + GroupName + "]").unbind("click");
    $("[name=" + GroupName + "]").click(function () { validateClick(this) });
    var SelectedVal = $("[name=" + GroupName + "]:checked").val();           
    if (SelectedVal==undefined)
        validateAction(ElementItem);
}


function validateClick(ElementItem) {

    if ($(ElementItem).val() == $(ElementItem).attr("error"))
        $(ElementItem).val("");

}


function validateChange(ElementItem) {

    validateClick(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;
}


