﻿ var ClientID;
 var PrtCustInfoFields;
 
 function ValidateLicAgr(sender, args)
    {
        var isValid = true;
        if(document.getElementById(ClientID + "_ckb_LicAgr").checked)
        {
            document.getElementById(ClientID + "_lbl_LicAgrIHaveRead").style.color = "black";
        }    
        else
        {
            document.getElementById(ClientID + "_lbl_LicAgrIHaveRead").style.color = "red";
            isValid = false;
        }
        if(!isValid)
        {
            document.getElementById(ClientID + "_btn_Submit").value='Submit';
            document.getElementById(ClientID + "_btn_Submit").disabled = false;
        }
        args.IsValid = isValid;
    }
   
    function ValidatePZLicAgr(sender, args)
    {
        var isValid = true;
        if(document.getElementById(ClientID + "_ckb_PrtLicAgr").checked)
        {
            document.getElementById(ClientID + "_lbl_PrtLicAgr").style.color = "black";
        }    
        else
        {
            document.getElementById(ClientID + "_lbl_PrtLicAgr").style.color = "red";
            isValid = false;
        }
        
        if(!isValid)
        {
            document.getElementById(ClientID + "_btn_PrtSubmit").value='Submit';
            document.getElementById(ClientID + "_btn_PrtSubmit").disabled = false;
        }
        args.IsValid = isValid;
    }
    
    function ValidateForm(sender, args)
    {
        var isValid = true;
        var preQID = 0;
        var preID = 0;
        var chk = false;
        var type = "tbx";
        var preType = "";
        for (var elems=0; elems<document.forms[0].length;elems++)
        {
          
          var id = document.forms[0].elements[elems].id;
          if(IsRequired(id) == true)
          {
            if(id.indexOf("Q_tbx") > 0)
            {
        
                type = "Q_tbx";
                
                var str1 = id.substr(id.indexOf(type) + 6);
                var qID = str1.substr(0, str1.indexOf("_"));
                
                if(preType == "Q_cbx")
                {
                   isValid = ValidateCheckBox(preID, chk, isValid);
                        
                }
                
                if(document.forms[0].elements[elems].value == "")
                {
                    document.getElementById(id.replace(type, "lbl")).style.color = "red";
                    isValid = false;
                }
                else
                   document.getElementById(id.replace(type, "lbl")).style.color = "black";
                    
                
            }
            else if(id.indexOf("Q_ddl") > 0)
            {
                type = "Q_ddl";
                
                var str1 = id.substr(id.indexOf(type) + 6);
                var qID = str1.substr(0, str1.indexOf("_"));
                
                if(preType == "Q_cbx")
                {
                   isValid = ValidateCheckBox(preID, chk, isValid);
                        
                }
                   
                if(document.forms[0].elements[elems].value == "")
                {
                    var otherOption = id.replace(type, "Q_OO");
                    if(document.getElementById(otherOption) != null &&
                        document.getElementById(otherOption).value != "")
                        document.getElementById(id.replace(type, "lbl")).style.color = "black";
                    else
                    {
                        document.getElementById(id.replace(type, "lbl")).style.color = "red";
                        isValid = false;
                    }
                }
                else
                {
                    document.getElementById(id.replace("Q_ddl", "lbl")).style.color = "black";
                }
            }
            else if(id.indexOf("Q_cbx") >0)
            {  
                 type = "Q_cbx";
                 
                var str1 = id.substr(id.indexOf(type) + 6);
                var qID = str1.substr(0, str1.indexOf("_"));
                
                if(preQID != qID)
                {
                   if(preType == "Q_cbx")
                   {
                       isValid = ValidateCheckBox(preID, chk, isValid);
                        
                   }
                   
                   preID = id;
                   preQID = qID;
                   chk = false;
                }
                if(!chk)
                {
                    if(document.forms[0].elements[elems].checked)
                        chk = true;
                } 
            }
            else if(id.indexOf("Q_rdl") > 0)
            {
                type = "Q_rdl";
                
                var str1 = id.substr(id.indexOf(type) + 6);
                var qID = str1.substr(0, str1.indexOf("_"));
             
                if(preQID != qID)
                {
                    if(preType == "Q_cbx")
                    {
                        isValid = ValidateCheckBox(preID, chk, isValid);
                        
                    }
                    
                    chk = false;
                    for (i=0;i<document.getElementsByName(document.forms[0].elements[elems].name).length;i++) {
	                    if (document.getElementsByName(document.forms[0].elements[elems].name)[i].checked) {
		                     chk = true;
	                    }
                    }
                    if(!chk)
                    {
                        var otherOption =  id.substr(0, id.lastIndexOf("_")).replace(type, "Q_OO");
                       if(document.getElementById(otherOption) != null &&
                            document.getElementById(otherOption).value != "")
                            document.getElementById(id.substr(0, id.lastIndexOf("_")).replace(type, "lbl")).style.color = "black";
                       else 
                       {
                            isValid = false;
                            document.getElementById(id.substr(0, id.lastIndexOf("_")).replace(type, "lbl")).style.color = "red";
                        }
                    }
                    else
                    {
                       document.getElementById(id.substr(0, id.lastIndexOf("_")).replace(type, "lbl")).style.color = "black";
                    }
                    preID = id;
                    preQID = qID;
                }

            }
            preType = type;
          }
        }
         
         if(preQID != 0 && !chk)
        {
            var otherOption =   preID.substr(0, preID.lastIndexOf("_")).replace(type, "Q_OO");
   
            if(document.getElementById(otherOption) != null &&
               document.getElementById(otherOption).value != "")
             {
                 document.getElementById(preID.substr(0, preID.lastIndexOf("_")).replace(type, "lbl")).style.color = "black";
          
             }
             else
             {
                document.getElementById(preID.substr(0, preID.lastIndexOf("_")).replace(type, "lbl")).style.color = "red";
                isValid = false;
             }
               
        }
        else if(preQID != 0 && chk)
        {
              document.getElementById(preID.substr(0, preID.lastIndexOf("_")).replace(type, "lbl")).style.color = "black";
        }
        args.IsValid = isValid;
        if(!isValid)
        {
            document.getElementById(ClientID + "_btn_Submit").value='Submit';
            document.getElementById(ClientID + "_btn_Submit").disabled = false;
        }
    }
    
    function ValidateCheckBox(id, chk, cIsValid)
    {
        var type = "Q_cbx";
        if(chk)
        {
            document.getElementById(id.substr(0, id.lastIndexOf("_")).replace(type, "lbl")).style.color = "black";
                            
        }
        else  if(!chk)
        {
            var otherOption =   id.substr(0, id.lastIndexOf("_")).replace(type, "Q_OO");
            if(document.getElementById(otherOption) != null &&
               document.getElementById(otherOption).value != "")
                document.getElementById(id.substr(0, id.lastIndexOf("_")).replace(type, "lbl")).style.color = "black";
            else 
            {
                document.getElementById(id.substr(0, id.lastIndexOf("_")).replace(type, "lbl")).style.color = "red";
                     return false;
            }
        }
        return cIsValid;
    }
    function IsRequired(id)
    {
        var pos;
        var str1;
        var getLastIndex = false;
        if(id.indexOf("Q_tbx") > 0)      str1 = id.substr(id.indexOf("Q_tbx") + 2);
        else if(id.indexOf("Q_rdl") > 0) {
            getLastIndex = true;
            str1 = id.substr(id.indexOf("Q_rdl") + 2);
        }
        else if(id.indexOf("Q_ddl") > 0) str1 = id.substr(id.indexOf("Q_ddl") + 2);
        else if(id.indexOf("Q_cbx") > 0) {
            getLastIndex = true;       
            str1 = id.substr(id.indexOf("Q_cbx") + 2);
        }
        else return false;
      
        var qID = str1;
        
        if(getLastIndex) qID = str1.substr(0, str1.lastIndexOf("_"));
        if(document.getElementById(ClientID + "_hf_ReqControls").value.indexOf(qID) >= 0)
           return true;
        else return false;
    }

    function ValidatePZCustInfoForm(sender, args)
    {
        var PZCustInfoClientID = ClientID + "_uc_PZCustInfo";
        var fields = PZCustInfoFields;
        var isValid = true;
        var arrFields = fields.split(";");
        
        for(var i=0;i<arrFields.length;i++)
        {
            var field = arrFields[i].split(",");
            if(document.getElementById(PZCustInfoClientID + "_" + field[1]).value == "")
            {
                document.getElementById(PZCustInfoClientID + "_" + field[0]).style.color = "red";
                isValid = false;
            }
            else
                 document.getElementById(PZCustInfoClientID + "_" + field[0]).style.color = "black";
               
        }
     
        args.IsValid = isValid;   
                     
        if(!isValid)
        {
            document.getElementById(ClientID+ "_btn_Submit").value='Submit';
            document.getElementById(ClientID + "_btn_Submit").disabled = false;
        }
    }
