/****************************************************************************************
Product         : PowerVolt Website Phase I.
Copyright	    : Edify Technologies Inc.
Source          : Product_Details.js
Created Date    : 12/09/2008
Modified Date   : 07/27/2009
Author          : Justin Arun
Version         : 1.0.0
Description     : JavaScript file for validating Product_Details page

CR / Issue No      Modified By       Modified On       Version         Reason
****************************************************************************************/

var TabId = 0;
var PI = false;
var Spec = false;
var How = true;

//Validate Quote Information
function validate(tabindex) 
{
    TabId = tabindex    
}

function ValidateQuote(HiddenField1, AccordionPane1HiddenField1, AccordionPane2HiddenField1, AccordionPane3HiddenField1, ChkboxInternet, ChkMagazine) 
{
    
    if (document.getElementById(clientID + "divSuccessMessage") != null) {
        document.getElementById(clientID + "divSuccessMessage").innerHTML = "";
        document.getElementById(clientID + "divSuccessMessage").className = "";
    }
    if (!ValidatePersonalInformation(AccordionPane1HiddenField1))
        return false;

    if (!ValidateSpecifications(AccordionPane2HiddenField1))
        return false;

    if (!ValidateHowCome(AccordionPane3HiddenField1, ChkboxInternet, ChkMagazine))
        return false;

    document.getElementById('divErrorMessage4').innerHTML = "";        
    if (!ValidateImageVerfication(HiddenField1))
        return false;

    if (PI && Spec && How) {
         document.getElementById('img').style.display = "block";
        __doPostBack(document.getElementById(clientID + 'btnSubmit').name, '');  
        return true;
    }
    else {
        
        return false;
    }
}

function ValidateQuoteAdmin(AccordionPane1HiddenField1, AccordionPane2HiddenField1, AccordionPane3HiddenField1, ChkboxInternet, ChkMagazine) 
{

        if (!ValidatePersonalInformation(AccordionPane1HiddenField1))
            return false;

        if (!ValidateSpecifications(AccordionPane2HiddenField1))
            return false;
       
        if (!ValidateHowCome(AccordionPane3HiddenField1, ChkboxInternet, ChkMagazine))
            return false;

        document.getElementById('divErrorMessage4').innerHTML = "";
        
        if (PI && Spec && How) {
            
            __doPostBack(document.getElementById(clientID + 'btnSubmit').name, '');  
            return true;
        }else{
            return false;
        }
}

// Validate Personal Information

function ValidatePersonalInformation(controlID) 
{
    
    try {
        var errorCode;
        firstErrorControl = "";

        errMsg = "<tr><td class='errormsg'></td></tr>";
          
        if (ValidateRequired("txtFirstName", emptyFirstName))
            ValidateSpecialChars("txtFirstName", invalidFirstName, "name");
          
        if (ValidateRequired("txtLastName", emptyLastName))
            ValidateSpecialChars("txtLastName", invalidLastName, "name");            

        ValidateRequired("txtCompanyName", emptyCompanyName)
        
        if (ValidateRequired("txtEmail", emptyEmail))
            ValidateEmail("txtEmail", invalidEmail);

        ValidateRequired("txtAddress1", emptyAddress1);

        if (ValidateRequired("txtCity", emptyCity))
            ValidateSpecialChars("txtCity", invalidCity, "name");

        ValidateDropDown("ddlState", emptyState);
        if (document.getElementById(clientID + "ddlState").value == 598) {
            if (ValidateRequired("txtOtherState", emptyStateText))
                ValidateSpecialChars("txtOtherState", invalidStateText, "all");
                
            
        }
        ValidateDropDown("ddlCountry", selectCountry);
        
        if (ValidateRequired("txtPinCode", emptyZipCode))
			if(ValidateSpecialChars("txtPinCode", invalidZipCode, "all"));    
				ValidateZipCodeLength("txtPinCode", invalidZipCodeLength); 

        if (ValidateRequired("txtPhone1", emptyPhoneNo))
            if(ValidatePhone("txtPhone1", invalidPhoneNo));
                ValidatePhoneLength("txtPhone1", invalidPhoneNoLength);  

        //Commented & Added by saravanan dated on 04/22/2009
        //Bug Id:356   
        //if (ValidateRequired("txtFaxNo1", emptyFaxNo1))
        if (document.getElementById(clientID + "txtFaxNo1").value != "") 
        {
            ValidatePhone("txtFaxNo1", invalidFaxNo1);
        }
        else 
        {
            document.getElementById(clientID + "txtFaxNo1").style.backgroundColor = "";
        }       

       ValidateRequired("txtIndustries", emptyIndustries);
 
       if(ValidateRequired("txtReqQuantity", emptyReqQuantity))
           ValidateSevenTwoDecimal("txtReqQuantity", invalidReqQuantity);

        // Commented by Saravanan dated on 10/11/2009
        // for client change request
//       if (document.getElementById(clientID + "txtEAU").value != "") 
//       {
//           ValidateNumbers("txtEAU", invalidEAU);
//       }
//       else 
//       {
//           document.getElementById(clientID + "txtEAU").style.backgroundColor = "";
//       }
       

       if (document.getElementById(clientID + "txtTargetPrice").value != "")
       {
           ValidateSevenTwoDecimal("txtTargetPrice", invalidTargetPrice);
       }
       else 
       {
           document.getElementById(clientID + "txtTargetPrice").style.backgroundColor = "";
       }

        if (firstErrorControl != '') {
            SetControlFocus(firstErrorControl);
            errMsg = "<table border='0' width='670px' cellspacing='0' cellpadding='0' class='validation'>" + errMsg + "</table>";
            document.getElementById('divErrorMessage1').innerHTML = errMsg;
            document.getElementById('divErrorMessage4').innerHTML = "";
            PI = false;            
            return true;
        }
        else {
            document.getElementById('divErrorMessage1').innerHTML = "";
            PI = true;
            return true;            
        }
    }
    catch (e) {
        alert(emptyQuoteContact);
    }
}

//Validate Specifications

function ValidateSpecifications(controlID) 
{

try
{
    var errorCode;
    firstErrorControl = "";
    errMsg = "<tr><td class='errormsg'></td></tr>"; 
    
    if(ValidateRequired("txtPrimary1", emptyPrimary1))
    {
       ValidateSevenTwoDecimal("txtPrimary1", invalidPrimary1);
    }
    
    if ((document.getElementById(clientID + "txtPrimary2").value != "") || (document.getElementById(clientID + "txtSecondaryVolts2").value != "") || (document.getElementById(clientID + "txtSecondaryAmps2").value != "")) {
        if (ValidateRequired("txtPrimary2", emptyPrimary2))
            ValidateSevenTwoDecimal("txtPrimary2", invalidPrimary2);
    }
    else {
        document.getElementById(clientID + "txtPrimary2").style.backgroundColor = "";
    }

    if ((document.getElementById(clientID + "txtPrimary3").value != "") || (document.getElementById(clientID + "txtSecondaryVolts3").value != "") || (document.getElementById(clientID + "txtSecondaryAmps3").value != "")) {
        if (ValidateRequired("txtPrimary3", emptyPrimary3))
            ValidateSevenTwoDecimal("txtPrimary3", invalidPrimary3);
    }
    else {
        document.getElementById(clientID + "txtPrimary3").style.backgroundColor = "";
    }
    if(ValidateRequired("txtSecondaryVolts1", emptySecondaryVolts1))
        ValidateSevenTwoDecimal("txtSecondaryVolts1", InvalidSecondaryVolts1);
    
    if(ValidateRequired("txtSecondaryAmps1", emptySecondaryAmps1))
        ValidateSevenThreeDecimal("txtSecondaryAmps1", InvalidSecondaryAmps1);
        
    // removed Primary2 & 3, Secondary 2 & 3 mandatory field for bug id:1234 by maharajan 
    if ((document.getElementById(clientID + "txtSecondaryVolts2").value != ""))
    {
        ValidateSevenTwoDecimal("txtSecondaryVolts2", InvalidSecondaryVolts2);
    }
    else 
    {
        document.getElementById(clientID + "txtSecondaryVolts2").style.backgroundColor = "";
    }
    
    if ((document.getElementById(clientID + "txtSecondaryAmps2").value != ""))
    {
        ValidateSevenThreeDecimal("txtSecondaryAmps2", InvalidSecondaryAmps2);
    }
    else 
    {
        document.getElementById(clientID + "txtSecondaryAmps2").style.backgroundColor = "";
    }
    
    if ((document.getElementById(clientID + "txtSecondaryVolts3").value != ""))
    {
        ValidateSevenTwoDecimal("txtSecondaryVolts3", InvalidSecondaryVolts3);
    }
    else 
    {
        document.getElementById(clientID + "txtSecondaryVolts3").style.backgroundColor = "";
        
    }
    if ((document.getElementById(clientID + "txtSecondaryAmps3").value != ""))
    {
        ValidateSevenThreeDecimal("txtSecondaryAmps3", InvalidSecondaryAmps3);
    }
    else 
    {
        document.getElementById(clientID + "txtSecondaryAmps3").style.backgroundColor = "";
    }

    if (document.getElementById(clientID + "ddlFrequency").value == 11003) 
    {
        if (ValidateRequired("txtFrequencyOthers", emptyFrequencyOthers)) {
            ValidateNumbers("txtFrequencyOthers", InvalidFrequencyOthers);
        }       
    }

    if (document.getElementById(clientID + "ddlHipot").value == 90004)
    {
        if (ValidateRequired("txtHipotOthers", emptyHipotOthers)) 
        {
            ValidateNumbers("txtHipotOthers", InvalidHipotOthers);
        }
    }
    
    if(document.getElementById(clientID + "txtAmbientTemp").value!="")
    {
        ValidateSpecialChars("txtAmbientTemp", InvalidAmbientTemp, "title");
    }
    else
    {
        document.getElementById(clientID + "txtAmbientTemp").style.backgroundColor = "";
    }

    if (document.getElementById(clientID + "ddlInsulationClass").value == 13005) 
    {
        ValidateRequired("txtInsulationOthers", emptyInsulationOthers);
    }

    if (document.getElementById(clientID + "ddlMounting").value == 80003) 
    {

        ValidateRequired("txtMountingOthers", emptyMountingOthers);
        
    }

    if (document.getElementById(clientID + "ddlSafetyApprovalRequired").value == 14005) 
    {
        ValidateRequired("txtSafetyApprovalOthers", emptySafetyOthers);
    }
    
    // Commented by Saravanan dated on 11/11/2009
    // for client change request

//    if(document.getElementById(clientID + "txtHeight").value!="")
//    {
//        ValidateSevenTwoDecimal("txtHeight", invalidHeight);
//    }
//    else
//    {
//        document.getElementById(clientID + "txtHeight").style.backgroundColor = "";
//    }
//     
//    if(document.getElementById(clientID + "txtLength").value!="")
//    {
//        ValidateSevenTwoDecimal("txtLength", invalidLength);
//    }
//    else
//    {
//        document.getElementById(clientID + "txtLength").style.backgroundColor = "";
//    }   
//    
//    if(document.getElementById(clientID + "txtWidth").value!="")
//    {
//        ValidateSevenTwoDecimal("txtWidth", invalidWidth);
//    }
//    else
//    {
//        document.getElementById(clientID + "txtWidth").style.backgroundColor = "";
//    }

    if (firstErrorControl != '') 
    {
        SetControlFocus(firstErrorControl);
        errMsg = "<table border='0' width='670px' cellspacing='0' cellpadding='0' class='validation'>" + errMsg + "</table>";
        document.getElementById("divErrorMessage2").innerHTML = errMsg;
        document.getElementById('divErrorMessage4').innerHTML = "";
        Spec = false;
        return true;
    }
    else 
    {
        document.getElementById("divErrorMessage2").innerHTML = "";
        Spec = true;
        return true;
        //btnDisabled(clientID + "btnSubmit", "enable");
    }
    }
    catch (e) {
        alert(emptyQuoteSpec);
    }
}

//Validate How Come

function ValidateHowCome(controlID,chkboxInternet,chkboxMagazine) 
{

    try
    {

    var errorCode;
    firstErrorControl = "";
    var checked = false;
    
    errMsg = "<tr><td class='errormsg'></td></tr>";
    chkboxInterNetCTL = document.getElementById(chkboxInternet);
    
    var chkList1= document.getElementById(chkboxInternet);
    var arrayOfCheckBoxes= chkList1.getElementsByTagName("input");
    
    for(var i=0;i<arrayOfCheckBoxes.length;i++)
    {
        if (arrayOfCheckBoxes[i].checked) 
        {
            checked=true;
            if(i==2)
                ValidateRequired("txtOthersSource", emptyInternetOthers);
        }
     }

     var chkList1 = document.getElementById(chkboxMagazine);
     var arrayOfCheckBoxes = chkList1.getElementsByTagName("input");
     var checked1 = false;
     
     for (var i = 0; i < arrayOfCheckBoxes.length; i++) 
     {
         if (arrayOfCheckBoxes[i].checked) 
         {
            
             checked1 = true;
             if (i == 1)
                 ValidateRequired("txtOthersSource", emptyMagazineName);
             
             if (i == 2)
                 ValidateRequired("txtOthersSource", emptyMagazineOthers);
         }
     }

     if((checked==false) && (checked1==false))
     {
         //document.getElementById(clientID +  "txtMagazineName").value='';
         if (firstErrorControl == '')
                firstErrorControl = chkboxInternet;
            errMsg += GetErrorRow(chkboxInternet, emptyHowyouheared);
            
     }
    
      
    if (firstErrorControl != '') 
    {

        SetControlFocus(firstErrorControl);
        errMsg = "<table border='0' width='670px' cellspacing='0' cellpadding='0' class='validation'>" + errMsg + "</table>";
        document.getElementById("divErrorMessage3").innerHTML = errMsg;
        document.getElementById('divErrorMessage4').innerHTML = "";
        How = false;
        return false;
    }
    else 
    {
        document.getElementById("divErrorMessage3").innerHTML = "";
        How = true;
        return true;
    }
}
catch (e)
    {
      alert(emptyHowHeard);
    }
}

//Validate Image Verification
function ValidateImageVerfication(controlID) 
{    
    try
    {
        var errorCode;
        firstErrorControl = "";

        clientID = SetClientID(controlID);

        ValidateRequired("txtImageVerfication", emptyImageVerification);

        if (firstErrorControl != '') 
          {
            
            SetControlFocus(firstErrorControl);
            errMsg = "<table border='0' width='670px' cellspacing='0' cellpadding='0' class='validation'>" + errMsg + "</table>";
            document.getElementById("divErrorMessage").innerHTML = errMsg; 
            return false;
        }
        else    
        {
            document.getElementById("divErrorMessage").innerHTML = "";
            return true; 
        }
    }
    catch (e) {
        alert(imgVerifyErr);
    }
}

function openedAccordion(hdnControlName, accPanelId) {    
    if (accPanelId == 'AccordionPane1')
        document.getElementById(hdnControlName).value = 1;

    if (accPanelId == 'AccordionPane2')
        document.getElementById(hdnControlName).value = 2;

    if (accPanelId == 'AccordionPane3')
        document.getElementById(hdnControlName).value = 3;
    
}

function clearMagazineOthers(controlID) {

    if (document.getElementById(controlID).value == "") {
        document.getElementById(controlID).value = "Enter the Magazine Name";
    }
    
    if (document.getElementById(controlID).value == "Enter the Magazine Name") {
        document.getElementById(controlID).value = "";
    }  
}

function DeleteCustoms()
{
    var gridview = document.getElementById(clientID+"gvQuoteList"); 
    var checkStatus = 'false';
    for ( var rowCount = 1; rowCount < gridview.rows.length; rowCount++ ) 
    {       
    var rowsTr = gridview.getElementsByTagName("TR");
    var rowsInput = rowsTr[rowCount].getElementsByTagName("input");
    for(i=0;i<rowsInput.length;i++)
     {
      if(rowsInput[i].type == 'checkbox')
        { 
         if(rowsInput[i].checked==true)
         {
            checkStatus = 'true';
            break;
         }
        }
     } 
    }

    if(checkStatus=='true')
    {
        return confirm('Are you sure to delete?')
    }else{
        alert("Select atleast one item");
        return false;
    }
}

//function to disable submit buttons on single click
function btnDisabled(controlID, actionVal)//Used in Code Behind
{
    if (actionVal == "enable") {
        if (document.getElementById(controlID) != null)
            document.getElementById(controlID).disabled = false;
    }
    else if (actionVal == "disable") {
        if (document.getElementById(controlID) != null)
            document.getElementById(controlID).disabled = true;
    }
}

//Function used to show/hide other columns in 
//each page for CustomSpecification Page
function ddlOthersDisp(getChkVal, getIdValue, getIdValue2)
{
    switch(getChkVal)
    {
        case "frequency":       
            if(document.getElementById(clientID + "ddlFrequency").value == getIdValue)
            {            
                document.getElementById(clientID + "lblFrequencyOthers").style.display="block";
                document.getElementById(clientID + "txtFrequencyOthers").style.display="block";
            }
            else
            {            
                document.getElementById(clientID + "lblFrequencyOthers").style.display="none";
                document.getElementById(clientID + "txtFrequencyOthers").style.display="none";
            }
            break;
        case "hipot": 
            if(document.getElementById(clientID + "ddlHipot").value == getIdValue)
            {            
                document.getElementById(clientID + "lblHipotOthers").style.display="block";
                document.getElementById(clientID + "txtHipotOthers").style.display="block";
            }
            else
            {            
                document.getElementById(clientID + "lblHipotOthers").style.display="none";
                document.getElementById(clientID + "txtHipotOthers").style.display="none";
            }
            break;
        case "insulation": 
            if(document.getElementById(clientID + "ddlInsulationClass").value == getIdValue)
            {            
                document.getElementById(clientID + "lblInsulationOthers").style.display="block";
                document.getElementById(clientID + "txtInsulationOthers").style.display="block";
            }
            else
            {            
                document.getElementById(clientID + "lblInsulationOthers").style.display="none";
                document.getElementById(clientID + "txtInsulationOthers").style.display="none";
            }
            break;
        case "mounting": 
            if(document.getElementById(clientID + "ddlMounting").value == getIdValue)
            {            
                document.getElementById(clientID + "lblMountingOthers").style.display="block";
                document.getElementById(clientID + "txtMountingOthers").style.display="block";
            }
            else
            {            
                document.getElementById(clientID + "lblMountingOthers").style.display="none";
                document.getElementById(clientID + "txtMountingOthers").style.display="none";
            }
            
            if(document.getElementById(clientID + "ddlMounting").value == getIdValue2)
                document.getElementById(clientID + "ddlMountingFrame").style.display="block";
            else         
                document.getElementById(clientID + "ddlMountingFrame").style.display="none";
                
            break;
        case "sar": 
            if(document.getElementById(clientID + "ddlSafetyApprovalRequired").value == getIdValue)
            {            
                document.getElementById(clientID + "lblSafetyApprovalOthers").style.display="block";
                document.getElementById(clientID + "txtSafetyApprovalOthers").style.display="block";
            }
            else
            {            
                document.getElementById(clientID + "lblSafetyApprovalOthers").style.display="none";
                document.getElementById(clientID + "txtSafetyApprovalOthers").style.display="none";
            }
            break;
    }  
}