﻿var CurrentlySubmitting = false;

function DisableButtonAndSubmit(Button, Text)
{
    var x = document.getElementById("FormButtonControl_Choice");
    if (x != null) x.value = Button.id;
    
    CurrentlySubmitting = true;
    
    if (Button.className == "nicebutton")
    {
        var FirstSpan = Button.firstChild;
        if (FirstSpan != null)
        {
            var SecondSpan = FirstSpan.firstChild;
            if (SecondSpan != null)
            {
               var ThirdSpan = SecondSpan.firstChild;
               if (ThirdSpan != null)
               {
                 if (ThirdSpan.className != null) ThirdSpan.className = "wait after";
                 if (ThirdSpan.innerHTML != "") ThirdSpan.innerHTML = Text;
               } else {
                 if (SecondSpan.innerHTML != "") SecondSpan.innerHTML = Text;
               }
            }
        }
        //Button.className="FormButton";
        //Button.style.backgroundColor = "#AAAAAA";
    } else {
        Button.value = Text;
        Button.style.backgroundColor = "#AAAAAA";
    }
    
    Button.disabled = true;
    Button.form.submit();
}

function ReviveButtons(container)
{
    if (CurrentlySubmitting) return;

    var len = container.children.length;
    
    for (var i = 0; i < len; i++)
    {
        var x = container.children[i];
        if (x != null)
        {
            if (x.type == "button" || x.type == "submit")
            {
                x.disabled = false;
            }
        }
    }
}

function JSTest() {
    alert("Test");
}

function TRonmouseover(row) 
{
    row.style.backgroundColor = "#E0E0E0";
}

function TRonmouseout(row) 
{
   row.style.backgroundColor = "";
}

function TDonmouseover(row) 
{
    row.style.backgroundColor = "#E0E0E0";
}

function TDonmouseout(row) 
{
   row.style.backgroundColor = "";
}

function UserNavImgSwap(Name, What)
{
  if (What == "on") {
    document.images["img" + Name].src = "/images/design/usernav/" + Name + ".gif";
  } else {
    document.images["img" + Name].src = "/images/design/usernav/" + Name + "-grey.gif";
  }
}

function initialCap(field) {
    var x = field.value.substr(0, 1).toUpperCase();
 
    if (field.value.length > 1) x += field.value.substr(1);
    
    field.value = x;
}

function CheckTextBox(txt, ProperName, AllowBlank, MinLength, MaxLength)
{
    var s = txt.value;
    var len = s.length;
    if (len == 0 && AllowBlank == false) 
    {
      alert(ProperName + " cannot be blank.");
      txt.focus();
      return;
    }
    
    if (len > 0) {
        if ((len > MaxLength) || (len < MinLength)) {
            alert(ProperName + " must be between " + MinLength + " and " + MaxLength + " characters.");
            txt.focus();
            return;
         }
     }
}


function ConfirmDeleteAdmin(GUID, Name)
{
    var callBackFn = function(arg) {
        if (arg) location.href = "Delete.aspx?GUID=" + GUID;
        return;
    };
  
  var NameToShow = Name;
  if (NameToShow.length > 40) NameToShow = NameToShow.substring(0, 40) + "...";
  radconfirm("<span class=PopupText>Are you sure you want to delete:<br>\n<b>" + NameToShow + "</b></span>?", callBackFn, 400, 150);
  return false;
}




function TDCSS(row, CssStyle) 
{
    if (row.className != "NavCurrent") {
        row.className = CssStyle;
    }
}

function TDNAV(Name, CssStyle) {
    var x = document.getElementById(Name);
    if (x == null) return;
    TDCSS(x, CssStyle);
}


var browserType;

if (document.layers) {browserType = "nn4"}
if (document.all) {browserType = "ie"}
if (window.navigator.userAgent.toLowerCase().match("gecko")) {
 browserType= "gecko"
}

function HideElement(ElementName) {
  if (ElementName == null || ElementName == "") return;
  
  if (browserType == "gecko" )
     document.poppedLayer = 
         eval('document.getElementById("' + ElementName + '")');
  else if (browserType == "ie")
     document.poppedLayer = 
        eval('document.getElementById("' + ElementName + '")');
  else
     document.poppedLayer =   
        eval('document.layers["' + ElementName + '"]');
  document.poppedLayer.style.display = "none";
}

function ShowElement(ElementName) {
  if (ElementName == null || ElementName == "") return;
  
  if (browserType == "gecko" )
     document.poppedLayer = 
         eval('document.getElementById("' + ElementName + '")');
  else if (browserType == "ie")
     document.poppedLayer = 
        eval('document.getElementById("' + ElementName + '")');
  else
     document.poppedLayer = 
         eval('document.layers["' + ElementName + '"]');
  document.poppedLayer.style.display = "inline";
}


// global flag
var isIE = false;

// global request and XML document objects
var req;

// retrieve XML document (reusable generic function);
// parameter is URL string (relative or complete) to
// an .xml file whose Content-Type is a valid XML
// type, such as text/xml; XML source must be from
// same domain as HTML file
function loadXMLDoc(url, func) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = func;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = func;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function GetChildNodeValue(CurrentNode, NodeName)
{
    var x = CurrentNode.getElementsByTagName(NodeName);
    if (x.length == 0) return "";
      
    x = x[0];
    if (x.childNodes.length == 0) return "";
        
    return(x.childNodes[0].nodeValue)
}

function GetNodeValue(NodeName)
{
    var x = req.responseXML.getElementsByTagName(NodeName);
    if (x.length == 0) return "";
      
    x = x[0];
    if (x.childNodes.length == 0) return "";
        
    return(x.childNodes[0].nodeValue)
}

function GetNodeAttribute(CurrentNode, AttributeName)
{
    var a = CurrentNode.attributes;
    if (a == null) return "";
    
    var x = a.getNamedItem(AttributeName);
    if (x == null) return "";
    
    return x.value;
}

function DropDownClear(dd)
{
    var len = dd.options.length;
    for (var i = 0; i < len; i++) dd.remove(0);
}

function DropDownAdd(dd, Value, Text)
{
  var option = document.createElement("OPTION");
  option.text = Text;
  option.value = Value;
  dd.options.add(option);
}


function AjaxProcessDropDown(TargetControl, Node)
{
    DropDownClear(TargetControl);
 
    for (var i = 0; i < Node.childNodes.length; i++)
    {
        var CurrentNode = Node.childNodes[i];
        if (CurrentNode.nodeName == "Option")
        {
            var OptionValue = GetChildNodeValue(CurrentNode, "Value");
            var OptionText = GetChildNodeValue(CurrentNode, "Text");
            
            DropDownAdd(TargetControl, OptionValue, OptionText);
        }
    }  
}

function AjaxProcessTargetControl(Node)
{
    var TargetControlName = GetNodeAttribute(Node, "name");
    var TargetControl = document.getElementById(TargetControlName);
    if (TargetControl == null) return;
    var TagName = TargetControl.tagName;
    if (TagName == null) return;
    TagName = TagName.toUpperCase();
    
    var ReadOnly = GetNodeAttribute(Node, "readonly");
    var CSSOverRide = GetNodeAttribute(Node, "cssoverride");
    if (ReadOnly == "true") TargetControl.readOnly = true;
    if (ReadOnly == "false") TargetControl.readOnly = false;
    if (CSSOverRide != "") TargetControl.className = CSSOverRide;
    
    if (TagName== "SELECT")
    {
        AjaxProcessDropDown(TargetControl, Node);
    } else {
        if (Node.childNodes.length == 0)
        {
            TargetControl.value = "";
        } else
        {
            if (Node.childNodes[0].nodeValue != null) TargetControl.value = Node.childNodes[0].nodeValue;
        }
    }
}

function AjaxProcessResponse()
{
    if (req == null) return;
    if (req.responseXML == null) return;
    
    var DocumentElement = req.responseXML.documentElement;
    if (DocumentElement == null) return;
    if (DocumentElement.childNodes == null) return;
    
    for (var i = 0; i < DocumentElement.childNodes.length; i++)
    {
        var CurrentNode = DocumentElement.childNodes[i];
        if (CurrentNode.nodeName == "TargetControl")
        {
            AjaxProcessTargetControl(CurrentNode);
        }
    }
}

function EnterClick(ButtonID,e)
{
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

if (keycode == 13)
{
   var x = document.getElementById(ButtonID);
   if (x != null) x.click();
   return false;
   }
else
   return
}

function SetElementValue(ID, Value)
{
    var x = document.getElementById(ID);
    if (x == null) return;
    x.value = Value;
}

function autotab(field)
{
    if (field.getAttribute == null) return;
    if (field.value.length == field.getAttribute("maxlength"))
    {
        var FieldForm = field.form;
        var FieldCount = FieldForm.length;
        for (var i = 0; i < FieldCount; i++)
        {
            if (FieldForm[i] == field)
            {
                var NextField = FieldForm[i + 1];
                if (NextField != null)
                {
                    NextField.focus();
                    return true;
                }
            }
        }
    }
    return true;
    //destination.focus()
}

function ResetControl(TargetControlID)
{
    var TargetControl = document.getElementById(TargetControlID);
    if (TargetControl != null)
    {
        var TagName = TargetControl.tagName;
        if (TagName == null) TagName= "";
        TagName = TagName.toUpperCase();

        if (TagName == "SELECT")
        {
            if (TargetControl.length > 0) TargetControl.selectedIndex = 0;
        }
        
        if (TargetControl.type == "text")
        {
            TargetControl.value = "";
        }
        
        if (TargetControl.type == "radio")
        {
            if (TargetControlID.indexOf("ctgInsuranceQuestion_questiontoggle_on_") == 0)
            {
                TargetControl.checked = false;
                var TargetControlID_Off = TargetControlID.replace("ctgInsuranceQuestion_questiontoggle_on_", "ctgInsuranceQuestion_questiontoggle_off_");
                var TargetControl_Off = document.getElementById(TargetControlID_Off);
                if (TargetControl_Off != null) TargetControl_Off.checked = true;
            }
        }
    }
}

function ResetDivControls(SubControlList)
{
    if (SubControlList.length == 0) return;
    var list = SubControlList.split(",");
    for (var i = 0; i < list.length; i++)
    {
        var TargetControlID = "ctgInsuranceQuestion_" + list[i];
        ResetControl(TargetControlID);
    }
}


function ToggleOnClick(chk, DivName, SubControlList)
{
    animatedcollapse.toggle(DivName);
    
    if (chk.checked == true) return;
    ResetDivControls(SubControlList);
}

function ToggleOn(chk, DivName, SubControlList)
{
    animatedcollapse.show(DivName);
}

function ToggleOff(chk, DivName, SubControlList)
{
    animatedcollapse.hide(DivName);
     ResetDivControls(SubControlList);
}

function SmartDivHide(IsPageLoad, DivName)
{
    var x = document.getElementById(DivName);
    if (x == null) return;
    if (IsPageLoad)
    {    
        x.style.display = "none";
    } else
    {
        animatedcollapse.hide(DivName);
    }
}

function ProcessPolicyOwnership(IsPageLoad)
{
    var ucPolicyOwnership = document.getElementById("ctgInsuranceQuestion_PolicyOwnership");
    if (ucPolicyOwnership == null) return;
    
    var PolicyOwnership = ucPolicyOwnership.value;
    if (PolicyOwnership.toUpperCase() == "COMPANY")
    {
        if (IsPageLoad == false) animatedcollapse.show("divCompanyOwned");
    } else
    {
        SmartDivHide(IsPageLoad, "divCompanyOwned");
        ResetDivControls("CompanyName");
    }
}

function GetElementValue(ID)
{
    var x = document.getElementById(ID);
    if (x == null) return "";
    
    if (x.value == null) return "";
    
    return x.value;
}

function GetElementValueToUpper(ID)
{
    return GetElementValue(ID).toUpperCase();
}

function AnimatedDivShow(ID)
{
    var x = document.getElementById(ID);
    if (x == null) return;
    
    animatedcollapse.show(ID);
}


function SmartAnimatedDivShow(IsPageLoad, DivName)
{
    var x = document.getElementById(DivName);
    if (x == null) return;
    if (IsPageLoad)
    {    
        x.style.display = "";
    } else
    {
        animatedcollapse.show(DivName);
    }
}


function HideShowEngine(IsPageLoad, NumberOfEngines, Which)
{
    // If the user doesn't have this engine
    if (NumberOfEngines < Which)
    {
        // Hide and reset everything
        SmartDivHide(IsPageLoad, "divVehicleEngine" + Which);
        SmartDivHide(IsPageLoad, "divVehicleEngine" + Which + "Extras");
        ResetDivControls("EngineYear" + Which + ",HorsePower" + Which + ",PropulsionType" + Which + ",FuelType" + Which + ",EngineMake" + Which + ",EngineModel" + Which + ",EngineVIN" + Which);
    } else {
        // If they do have this engine
        // On page load, they are all showing by default, if this is after the page loaded, we need to show it.
        if (IsPageLoad == false) 
        {
            AnimatedDivShow("divVehicleEngine" + Which);
        }
        
        // Check out the PropulsionType and show extras if needed.
        var ucPropulsionType = document.getElementById("ctgInsuranceQuestion_PropulsionType" + Which);
        if (ucPropulsionType != null)
        {
            ProcessPropulsionType(IsPageLoad, ucPropulsionType);
        }
    }
}

function ProcessNumberOfEngines(IsPageLoad)
{
    var ucNumberOfEngines = document.getElementById("ctgInsuranceQuestion_NumberOfEngines");
    if (ucNumberOfEngines == null) return;
    
    var NumberOfEngines = parseInt(ucNumberOfEngines.value);

    HideShowEngine(IsPageLoad, NumberOfEngines, 4);
    HideShowEngine(IsPageLoad, NumberOfEngines, 3);
    HideShowEngine(IsPageLoad, NumberOfEngines, 2);
    HideShowEngine(IsPageLoad, NumberOfEngines, 1);
}

function HideShowClaim(IsPageLoad, NumberOfClaims, Which)
{
    // If the user doesn't have this Claim
    if (NumberOfClaims < Which)
    {
        // Hide and reset everything
        SmartDivHide(IsPageLoad, "divOperatorClaim" + Which);
        ResetDivControls("LossCode" + Which + ",LossDate" + Which + "_Month,LossDate" + Which + "_Day,LossDate" + Which + "_Year,LossAmount" + Which + ",LossInsuranceCompany" + Which + ",LossDescription" + Which);
    } else {
        // If they do have this Claim
        // On page load, they are all showing by default, if this is after the page loaded, we need to show it.
        if (IsPageLoad == false) 
        {
            AnimatedDivShow("divOperatorClaim" + Which);
        }        
    }
}

function ProcessNumberOfClaims(IsPageLoad)
{
    var ucNumberOfClaims = document.getElementById("ctgInsuranceQuestion_NumberOfClaims");
    if (ucNumberOfClaims == null) return;
    
    var NumberOfClaims = parseInt(ucNumberOfClaims.value);

    HideShowClaim(IsPageLoad, NumberOfClaims, 3);
    HideShowClaim(IsPageLoad, NumberOfClaims, 2);
    HideShowClaim(IsPageLoad, NumberOfClaims, 1);
}

function HideShowViolation(IsPageLoad, NumberOfViolations, Which)
{
    // If the user doesn't have this Violation
    if (NumberOfViolations < Which)
    {
        // Hide and reset everything
        SmartDivHide(IsPageLoad, "divOperatorViolation" + Which);
        ResetDivControls("ViolationCode" + Which + ",ViolationDate" + Which + "_Month,ViolationDate" + Which + "_Day,ViolationDate" + Which + "_Year");
    } else {
        // If they do have this Violation
        // On page load, they are all showing by default, if this is after the page loaded, we need to show it.
        if (IsPageLoad == false) 
        {
            AnimatedDivShow("divOperatorViolation" + Which);
        }        
    }
}

function ProcessNumberOfViolations(IsPageLoad)
{
    var ucNumberOfViolations = document.getElementById("ctgInsuranceQuestion_NumberOfViolations");
    if (ucNumberOfViolations == null) return;
    
    var NumberOfViolations = parseInt(ucNumberOfViolations.value);

    HideShowViolation(IsPageLoad, NumberOfViolations, 6);
    HideShowViolation(IsPageLoad, NumberOfViolations, 5);
    HideShowViolation(IsPageLoad, NumberOfViolations, 4);
    HideShowViolation(IsPageLoad, NumberOfViolations, 3);
    HideShowViolation(IsPageLoad, NumberOfViolations, 2);
    HideShowViolation(IsPageLoad, NumberOfViolations, 1);
}

function ProcessFiling(IsPageLoad)
{
    var ucFilingStatusCode =  document.getElementById("ctgInsuranceQuestion_FilingStatusCode");
    if (ucFilingStatusCode == null) 
    {
        SmartDivHide(IsPageLoad, "divOperatorFiling");
        return;
    }
    
    var StatusCode = ucFilingStatusCode.value.toUpperCase();
    
    if (StatusCode == "" || StatusCode == "NONE" || StatusCode == "N") 
    {
        SmartDivHide(IsPageLoad, "divOperatorFiling");
        
        var ucFilingCaseNumber = document.getElementById("ctgInsuranceQuestion_FilingCaseNumber");
        if (ucFilingCaseNumber != null) ucFilingCaseNumber.value = "";
        
        var ucFilingStateCode = document.getElementById("ctgInsuranceQuestion_FilingStateCode");
        if (ucFilingStateCode != null) ucFilingStateCode.value = "";
        
        return;
    }
    
    SmartAnimatedDivShow(IsPageLoad, "divOperatorFiling");
}

function ProcessPropulsionType(IsPageLoad, CallingControl)
{
    var Which = CallingControl.id.replace("ctgInsuranceQuestion_PropulsionType", "");
    if (CallingControl.value.toUpperCase() == "OUTBOARD")
    {
        AnimatedDivShow("divVehicleEngine" + Which + "Extras");
        
        // If this is showing after a page has loaded, we need to update the make drop down (by processing year)
        if (IsPageLoad == false)
        {
            var ucEngineYear = document.getElementById("ctgInsuranceQuestion_EngineYear" + Which);
            VehicleDBAjaxUpdate(ucEngineYear);
        }
        
    } else
    {
        SmartDivHide(IsPageLoad, "divVehicleEngine" + Which + "Extras");
        ResetDivControls("EngineMake" + Which + ",EngineModel" + Which + ",EngineVIN" + Which);
    }
}

function FormButtonControlConfirmDelete(btn)
{
    var callBackFn = function(arg) {
        if (arg) DisableButtonAndSubmit(btn, 'Please Wait...');
        return;
    };
  
  var Name = "";
  var x;
  
  x = document.getElementById("ctgInsuranceQuestion_FirstName");
  if (x != null) Name += x.value;
  
  x = document.getElementById("ctgInsuranceQuestion_LastName");
  if (x != null) Name += " " + x.value;
  
  if (Name == " ") Name = "";
  
  var text = "<span class=PopupText>Are you sure you want to delete?</span>";
  if (Name != "") text = "<span class=PopupText>Are you sure you want to delete: <b>" + Name + "</b>?</span>";
  radconfirm(text, callBackFn, 400, 150);
  return false;
}

function ToggleAutoHide(DivName)
{
    var togglediv = document.getElementById(DivName);
    if (togglediv == null) return;
    
    var togglecontrol = document.getElementById("ctgInsuranceQuestion_questiontoggle_" + DivName);
    // If this is a checkbox.
    if (togglecontrol != null)
    {
        if (togglecontrol.checked == false) togglediv.style.display = "none";
        return;
    }
    
    // Otherwise it's radio buttons
    // See if yes/on is checked"
    togglecontrol =  document.getElementById("ctgInsuranceQuestion_questiontoggle_on_" + DivName);  
    if (togglecontrol != null)
    { 
      if (togglecontrol.checked == false) togglediv.style.display = "none";
    }
    
    return;
}

function StripURL(URL)
{
    var x = URL.toLowerCase();
    x = x.replace(/www/g, "");
    x = x.replace(/http:\/\//g, "");
    x = x.replace(".com", "");
    x = x.replace(".", "");
    return x;
}

function PickAffiliateCode()
{
    var txtFirstName = document.getElementById("ctl00_ContentPlaceHolder1_ucAffiliateSignup_txtFirstName");
    if (txtFirstName == null) txtFirstName = document.getElementById("ctl00_ContentPlaceHolder_txtFirstName");
    
    var txtLastName = document.getElementById("ctl00_ContentPlaceHolder1_ucAffiliateSignup_txtLastName");
    if (txtLastName == null) txtLastName = document.getElementById("ctl00_ContentPlaceHolder_txtLastName");
    
    var txtAffiliateCode = document.getElementById("ctl00_ContentPlaceHolder1_ucAffiliateSignup_txtAffiliateCode");
    if (txtAffiliateCode == null) txtAffiliateCode = document.getElementById("ctl00_ContentPlaceHolder_txtAffiliateCode");
    
    var txtURL =  document.getElementById("ctl00_ContentPlaceHolder1_ucAffiliateSignup_txtURL");
    if (txtURL == null) txtURL = document.getElementById("ctl00_ContentPlaceHolder_txtURL");
    
    if (txtFirstName == null || txtLastName == null || txtURL == null || txtAffiliateCode == null) return;
    
    if (txtAffiliateCode.value != "") return;
    
    var FirstName = txtFirstName.value.toLowerCase();
    var LastName = txtLastName.value.toLowerCase();
    var URL = StripURL(txtURL.value);
    
    var AffiliateCode = URL;
    if (AffiliateCode == "") AffiliateCode = FirstName + LastName;
    
    AffiliateCode = AffiliateCode.replace(/\s+/g, "");
    txtAffiliateCode.value = AffiliateCode;
}    


function popup(url, windowname, width, height) 
{
     var left   = (screen.width  - width)/2;
     var top    = (screen.height - height)/2;
     var params = 'width='+width+', height='+height;
     params += ', top='+top+', left='+left;
     params += ', directories=no';
     params += ', location=no';
     params += ', menubar=no';
     params += ', resizable=no';
     params += ', scrollbars=no';
     params += ', status=no';
     params += ', toolbar=no';
     
     newwin=window.open(url,windowname, params);
     if (window.focus) {newwin.focus()}
     return false;
}


function popup_affiliate_email(AffiliateGUID)
{
    popup("/Affiliate/Email.aspx?AffiliateGUID=" + AffiliateGUID, "SendEmail", 430, 460);
}

function PolicyViewRad(PolicyGUID)
{
      return PolicyViewRadTab(PolicyGUID, '');
}

function PolicyViewRadTab(PolicyGUID, TabName)
{
      var windowtitle = 'Policy_' + PolicyGUID.replace(/-/g, '_');
      var windowurl = '/Admin/Insurance/Policy/View.aspx?GUID=' + PolicyGUID + "&Tab=" + TabName;
   
      var wnd = window.radopen(windowurl);
      wnd.setSize(980, 620);
      wnd.Center();
      
      return false; // Stop browser from following anchor tag 
}