﻿function validate(elem){           
   if(elem.val()==""){
      $(elem).siblings(".validField").addClass("invalidField").removeClass("validField");
      return false;
   }
   else{
      $(elem).siblings(".invalidField").addClass("validField").removeClass("invalidField");
       $(elem).css("border-color","")
       return true;
          
   }
}

function clearForm(elem){
   $(elem).find("input").each(function() {

      var type = this.type, tag = this.tagName.toLowerCase();
   
      if (type == 'text' || type == 'password' || tag == 'textarea')

        this.value = '';

      else if (type == 'checkbox' || type == 'radio')

        this.checked = false;

      else if (tag == 'select')

        this.selectedIndex = -1;
        });            
}

function fixClonedElements(elem,index){
       
   $(elem).find('.date-pick').removeDatepicker()
  
    
    $(elem).find("[id]").each(function() {
        $(this).attr("id", function (arr) {
              return $(this).attr("id")+ "_" + index;
            });
        $(this).attr("name", function (arr) {
      
              return $(this).attr("name")+ "_" + index;
            });
       
    });
     $(elem).find("[for]").each(function() {
        $(this).attr("for", function (arr) {
              return $(this).attr("for")+ "_" + index;
            });
      
    });
       $(elem).find("legend").each(function() {
      
        var lastSpace = $(this).text().lastIndexOf(" ");
        var newVal = $(this).text().substring(0,lastSpace) +" " + (index+1);
      
        $(this).html(newVal);
      
    });
     $(elem).find("[targetelement]").each(function(){
        $(this).attr("targetelement", function (arr) {
        
              return $(this).attr("targetelement").replace(/,/g,"_"+ index +",") + "_" + index;
            });            
           
     });
      $(elem).find("label").filter(".error").each(function(){
        $(this).remove();         
           
     });
     
      $(elem).find("[targetelement]").each(function(){
         manageEvent($(this))     
       });

    
     $(elem).find('.date-pick').each(function(){
     var d=new Date();
     $(this).filter('.today').val( LZ(d.getDate()) + "/" + LZ(d.getMonth()+1) + "/" + d.getFullYear() );
      $(this).attachDatepicker({defaultDate:$(this).val()});  
       
   });
   
    validate($(elem).find('.date-pick'))
}

function scrollToInvalid(){
                  
}

function manageEvent(elem)
{
    // var trigger = $(elem).attr("trigger");
                        
    var testoperator = $(elem).attr("testoperator");
    var failureaction = $(elem).attr("failureaction");
    var successaction = $(elem).attr("successaction");
                        
    var elementvalue=$(elem).val()  
                
   
    //if an operator has been selected, then check it is valid, otherwise accept all elementvalues
   
    var testvalue = $(elem).attr("testvalue");
    var targetelement = $(elem).attr("targetelement");
    
    //test for multiple value options
        var testvalueArray 
        if(undefined !=testvalue) 
        {
           testvalueArray = testvalue.split(",")
         }
       var targetelementArray = targetelement.split(",")
        var failureactionArray = failureaction.split(",")
        var successactionArray = successaction.split(",")
 
   for(var i=0;i<targetelementArray.length;i++)
   {
        if(testvalueArray != null)
        {
            testvalue = testvalueArray[i]
       }
         if(failureactionArray != null)
        {
        failureaction = failureactionArray[i]
        }
         if(successactionArray != null)
        {
            successaction = successactionArray[i]
        }
       targetelement = targetelementArray[i]
       
     var valid=true;
     
    switch (testoperator)
    {
        case "Equal":
        if(elementvalue != testvalue ){
        valid=false;
        }
        break;
         case "LessThanOrEqualTo":
          if(elementvalue > testvalue){
            valid=false;
        }
        break;
         case "GreaterThanOrEqualTo":
            if(elementvalue < testvalue){
            valid=false;
        }
        break;
         case "GreaterThan":
            if(elementvalue <=testvalue){
            valid=false;
        }
        break;
         case "LessThan":
            if(elementvalue >= testvalue){
            valid=false;
        }
        break;
         case "NotEqual":
            if(elementvalue == testvalue){
            valid=false;
        }
        break;    
        default:
        
        if(!$(elem).attr("checked"))
            {
            valid=false;
            }
        break;        
    }
    
    targetelement =$("#" + targetelement)
    
        if(valid && $(elem).is(":enabled"))
        {
            if(successaction=="show")
            {
                
                 $(targetelement).find(":input").andSelf().removeAttr("disabled")
            }
            else
            {
               $(targetelement).find(":input").andSelf().attr('disabled','disabled')
            }
           
            eval("$(targetelement)." + successaction +"()");
            $(targetelement).find(":input").each(function(){
           // $("#form1").validate().element( $(this));   
            });
           
        }
        else if(!valid && $(elem).is(":enabled")){
            if(failureaction=="hide")
            {
                $(targetelement).find(":input").andSelf().attr('disabled','disabled')
            }
            else
            {
                $(targetelement).find(":input").andSelf().removeAttr("disabled")
            }
         
          eval("$(targetelement)." + failureaction +"()");

        }
        else{//if this item is disabled, then its children should be disabled also
         $(targetelement).find(":input").andSelf().attr('disabled','disabled')
          eval("$(targetelement)." + failureaction +"()");
        }
        
        if($(targetelement).find("[trigger]").length>0)
        {
              manageEvent($(targetelement).find("[trigger]"))
        }
    }
}

  function LZ(n) {
    return (n > 9 ? n : '0' + n);
    }
    
    function CreateErrorTooltop(elem)
    {
         var elem = elem.siblings().andSelf().filter(".error");
                 if (elem.length>0){  
                   var errorMsg =elem.attr("title")
                
                    JT_show(errorMsg, elem.id,elem.siblings("label").text())
                    
                    $(this).mouseout(function(){
                      $('#JT').remove()
                    });
                  } 
    }
            
