
/**
 * Validates and submits the form
 * If the min and max values selection is not correct
 * this will show an error message
 *
 */
function checkFormValidations()
{
    var payGoId = document.getElementById('dealTypesPPAY');
	var handsetOnlyId = document.getElementById('dealTypesHANDSET');
    if (payGoId.checked == true || handsetOnlyId.checked == true) {
		checkDealTypes('DealFinder', 'dealType');
    }

    document.DealFinder.submit();
}

function newShowHide(elementId, button) {
    if (document.getElementById(elementId)) {
        if (document.getElementById(elementId).style.display == 'none') {
            document.getElementById(elementId).style.display = 'block';

            if (button.src) { button.src=document.getElementById('hideButton').src; }
        } else {
            document.getElementById(elementId).style.display = 'none';
            if (button.src) { button.src=document.getElementById('showButton').src; }
        }
    }
}


/**
 * Generic function to turn on or off the memebrs of a group
 * if the *Any* checkbox has been selected (unselected)
 *
 * @param form The name of the form
 * @param controlGroup The name of the checkbox group.
 * @param isSelected The status of the any checkbox (true means selected)
 */
function checkAny(form, controlGroup, anyField)
{
    // The form
    var dealForm = document.forms[form];
	var isSelected = anyField.checked;
	anyField.disabled = false;
    if (isSelected == true) {
		// Disable all the non any members
        for (i = 0 ; i < dealForm[controlGroup].length; i++) {
            dealForm[controlGroup][i].checked = false;
            dealForm[controlGroup][i].disabled = true;
        }


       // If only one element
       if (dealForm.elements[controlGroup].length == undefined)
       {
          dealForm.elements[controlGroup].disabled = true;
       }
    }
    else {
        // Enable all the non any members
        for (i = 0 ; i < dealForm[controlGroup].length; i++) {
            dealForm[controlGroup][i].disabled = false;
        }

       // If only one element
       if (dealForm.elements[controlGroup].length == undefined)
       {
          dealForm.elements[controlGroup].disabled = false;
       }
    }
	
}


/**
 * Function to turn on or off the memebrs of a group
 * if the *Manufacturer Any* checkbox has been selected (unselected)
 *
 * @param form The name of the form
 * @param controlGroup The name of the checkbox group.
 * @param isSelected The status of the any checkbox (true means selected)
 */
function checkManufacturerAny(form, controlGroup, anyField)
{
    checkAny(form, controlGroup, anyField);
    if(anyField.checked == true)
    {
		document.getElementById('model').options.length = 1;
		document.getElementById('model').disabled = true;
     }
}




/**
 * Disables the other pay monthly related tabs when
 pay as you go is selected
 */
function disableMonthlyOptions(formName)
{

    // The form
    var dealForm = document.forms[formName];


    var allSelectElements = document.getElementsByTagName('SELECT');
   
    var payGoId = document.getElementById('dealTypesPPAY');
	var handsetId = document.getElementById('dealTypesHANDSET');

    for (var i=0;i<allSelectElements.length;i++)
    {
        var selectedElementId = allSelectElements[i].id+"";
        //alert(div_id);
        if(selectedElementId.indexOf("Max")>0 || selectedElementId.indexOf("Min")>0
            || selectedElementId.indexOf("Type")>0)
        {
            if (payGoId.checked == true || handsetId.checked == true)
            {
                allSelectElements[i].disabled = true;
            }
            else
            {
                allSelectElements[i].disabled = false;
            }
        }
    }
    //disable contract types if pas as you go is checked
    for (i = 0 ; i < dealForm['contracts'].length; i++) {
        if (payGoId.checked == true || handsetId.checked == true) { 
            dealForm['contracts'][i].disabled = true;
            dealForm['contractAny'].disabled = true;
        }
		else{
			dealForm['contracts'][i].disabled = false;
            dealForm['contractAny'].disabled = false;
		}
    }

	if(handsetId.checked == true)
	{
		dealForm['networkAny'].disabled = true;
		for (i = 0 ; i < dealForm['networks'].length; i++) {
			dealForm['networks'][i].disabled = true;
		}
	}

}


/**
 * Checks if the pay monthly related filters are
 * selected
 *
 */
function checkDealTypes(form, controlGroup)
{

    // The form
    var dealForm = document.forms[form];
    var dealType = "";

    // Disable all the non any members
    for (i = 0 ; i < dealForm[controlGroup].length; i++)
    {
        if (dealForm[controlGroup][i].checked == true)
        {
            dealType = dealForm[controlGroup][i].value;
        }
    }
	if (dealType == 'PPAY') {
		checkAny('DealFinder', 'networks',  document.getElementById('networkAny'));
		disableMonthlyOptions(form);
	}
	else if (dealType == 'HANDSET')
    {
		disableMonthlyOptions(form);
    }
    else
    {
        var allSelectElements = document.getElementsByTagName('SELECT');
        for (var i=0; i<allSelectElements.length; i++)
        {
            var selectedElementId = allSelectElements[i].id+"";
            //alert(div_id);
            if(selectedElementId.indexOf("Max")>0 || selectedElementId.indexOf("Min")>0
                || selectedElementId.indexOf("Type")>0)
            {
                allSelectElements[i].disabled = false;
            }
        }
		
        checkAny('DealFinder', 'networks',  document.getElementById('networkAny'));
		checkAny('DealFinder', 'contracts',  document.getElementById('contractAny'));

    }

}



function checkAnyDealTypes(form, controllGroup,  status)
{
     checkAny(form, controllGroup, status);
     checkDealTypes(form, controllGroup);
}
/**
 * Initiation when view is loaded.
 * This will update the model selection
 */
function init()
{
	refreshModel('DealFinder', 'manufacturers');
	checkDealTypes('DealFinder', 'dealType');
    enableFeatureDropDownIfFeatureSelected();
	disableGiftsOptions();

	checkManufacturerAny('DealFinder', 'manufacturers',  document.getElementById('manuAny'));
	
}

/**
* Disables gifts drop down when 'Show deals with gifts' is checked
*/
function disableGiftsOptions()
{
	var gifts = document.getElementById('giftId');
	gifts.disabled = false;
	var giftsCheckBox = document.getElementById('showGifts');
	if(giftsCheckBox.checked)
	{
		gifts.disabled = true;
	}
}

/**
* enables feature drop down if feature check box is unchecked.
*/

function enableFeatureDropDown(feature , featureDropDown)
{
	dropDown = document.getElementById(featureDropDown);
	if(dropDown != null)
	{
		dropDown.disabled = true;
		if(feature.checked)
		{
			dropDown.disabled = false;
		}
	}
}
	
/**
* Enables all the feature drop downs if its corresponding check boxes are checked.
*/

function enableFeatureDropDownIfFeatureSelected()
{
	features = document.getElementsByName('features');
	for(var i = 0;i<features.length;i++)
	{
		featureValue = features[i].value;
		enableFeatureDropDown(features[i],'featureEnumValue['+featureValue+']');
	}
}

window.onload = init;