﻿function textCounter(fieldId, countfieldId, maxlimit) 
{
    if (typeof document.getElementById != "undefined") {
        var field = document.getElementById(fieldId);
        var countfield = document.getElementById(countfieldId);
        
        if (field.value.length > maxlimit) 
        {
            field.value = field.value.substring(0, maxlimit);
        }
        else 
        {
            countfield.value = maxlimit - field.value.length;
        }
    }    
}

function DisableButton(bid)
{
	if(typeof(Page_ClientValidate) == 'function')
	{
		if(Page_ClientValidate())
		{
			document.getElementById(bid).onclick = function() { return false; }; // Prevent future clicks
			document.getElementById(bid).innerHTML="Submitting, please wait..."; // Change the content of the button
			return true;
		}
	}
	return false;
}

function ValidTextArea(textBoxId, feedbackArea, maxChars, message)
{
    var textBox = document.getElementById(textBoxId);
    var feedback = document.getElementById(feedbackArea);
    if (textBox.value.length > maxChars) 
    {
        feedback.innerHTML = message;
    }
    else 
    {
        feedback.innerHTML = "";
        return true;	
    }
    return false;
}

function TogglePanel(optValue, checkValue, panelId)
{
   var panel = document.getElementById(panelId);
    if(optValue == checkValue)
    {
        panel.style.display = "block";
    }
    else
    {
       panel.style.display = "none";
    }
}

function TogglePurchaseType(optValue, checkValue, panelId, companyVehicleRadioListId, companyVehiclePanelId)
{
  TogglePanel(optValue, checkValue, panelId);
  if(optValue != checkValue)
    {
        var companyVehiclePanel = document.getElementById(companyVehiclePanelId);
        companyVehiclePanel.style.display = "none";
        var companyVehicleRadioList = document.getElementById(companyVehicleRadioListId);
        if(companyVehicleRadioList != 'undefined')
        {
            for (var i = 0; i < companyVehicleRadioList.childNodes.length; i++) 
            {
                 var ctrl = companyVehicleRadioList.childNodes[i];
                 if(ctrl.type == 'radio')
                 {
                    ctrl.checked=false;
                 }
            }
        }          
    }
}

function ManagePanels(purchaseTypeValue, purchaseTypePanelId, companyVehicleValue, companyVehiclePanelId)
{
    TogglePanel(purchaseTypeValue, 'Company', purchaseTypePanelId);
    TogglePanel(companyVehicleValue, 'Buyer', companyVehiclePanelId);
}

function GetSelectedValue(radioButtonList)
{
    var selectedValue;
    for (var i = 0; i < radioButtonList.childNodes.length; i++) 
        {
             var ctrl = radioButtonList.childNodes[i];
             if(ctrl.type == 'radio')
             {
                if(ctrl.checked)
                {
                    selectedValue = ctrl.value;
                    break;
                }
             }
        }
    return selectedValue;
}

function IsSelected(radioButtonList)
{
    var isSomethingSelected = false;;
    for (var i = 0; i < radioButtonList.childNodes.length; i++) 
        {
             var ctrl = radioButtonList.childNodes[i];
             if(ctrl.type == 'radio')
             {
                if(ctrl.checked)
                {
                    isSomethingSelected = true;
                    break;
                }
             }
        }
    return isSomethingSelected;
}

function ApplyAltPhoneSelection(mainPhoneListId, altPhoneListId)
{
    var mainPhoneList = document.getElementById(mainPhoneListId);
    var altPhoneList = document.getElementById(altPhoneListId);
    
    var mainPhoneValue;
     for (var i = 0; i < mainPhoneList.childNodes.length; i++) 
        {
             var ctrl = mainPhoneList.childNodes[i];
             if(ctrl.type == 'radio')
             {
                if(ctrl.checked)
                {
                    mainPhoneValue = ctrl.value;
                    break;
                }
             }
        }
        
    var disableOption;
    var disableText;
    
    if(mainPhoneValue == 'Home') 
    {
        disableOption = 'AltHome';
        disableText = 'Home';
    }
    if(mainPhoneValue == 'Work')
    {
        disableOption = 'AltWork';
        disableText = 'Work' ;
    }
    if(mainPhoneValue == 'Mobile')
    {
         disableOption = 'AltMobile';
         disableText = 'Mobile';
    }
 
     for (var i = 0; i < altPhoneList.childNodes.length; i++) 
        {
             var ctrl = altPhoneList.childNodes[i];
             ctrl.disabled = false;
             if(ctrl.type == 'radio')
             {
                if(ctrl.value == disableOption)
                {
                    ctrl.checked = false;
                    ctrl.disabled = true; 
                }
             }
             else if(ctrl.tagName == 'LABEL')
             {
                if(ctrl.innerText == disableText)
                {
                    ctrl.disabled = true;
                }
             }
        }
}

function ValidateCompanyVehicle(source, args)
{
    var PurchaseTypeRadioList = document.getElementById(source.PurchaseTypeRadioList);
    var selectedPurchaseType;
    var isValid = true;
    selectedPurchaseType = GetSelectedValue(PurchaseTypeRadioList);
    
    if(selectedPurchaseType == "Company")
    {
        isValid = false;
        var CompanyVehicleRadioList = document.getElementById(source.CompanyVehicleRadioList);
        isValid = IsSelected(CompanyVehicleRadioList);
    }
    args.IsValid = isValid;
}

function ValidateAlternateTelephone(source, args)
{
    var isValid = true;
    var telephoneText = document.getElementById(source.AlternateTelephoneText);
    if(telephoneText.value.length > 0)
    {
        isValid = false;
        var telephoneRadioList = document.getElementById(source.AlternateTelephoneRadioList);
        var isselected = IsSelected(telephoneRadioList);
        isValid = isselected;
    }
    args.IsValid = isValid;
}
