/* ================================================================
 trigger options enables when form loads
================================================================ */
function adFormLoaded(){
    adCatChanged();
    //caRovingChanged();
}

/* ================================================================
// Called when the advert cat is changed
================================================================ */
function adCatChanged(){
	selCat = document.forms['tpForm'].tempCatData;
	if (selCat){
		var selectedCat = selCat.options[selCat.selectedIndex].value;
		var elShootSelect  = document.getElementById('adLocationID') // shoot selector
        var elRoving  = document.getElementById('row_rovingSyndicate');
        var isSyndicate = selectedCat.indexOf("Syndicate")> -1;
        var isAvailable = selectedCat.indexOf("Available")> -1;
        // If its a syndicate / available ad
        //alert(isSyndicate + isAvailable);
		if (isSyndicate && isAvailable ){
            // Enable shoot & county, disable region
            caEnableLocations(true, true, false);
			elShootSelect.disabled = false;
            //enable roving syndicate TB
            elRoving.style.display='inline';


		}
		else{
            // enable shoot, enable county & region
			caEnableLocations(true, true, true);
            //disable roving syndicate TB
            elRoving.style.display='none';
		}
        caRovingChanged()
	}
}
/* ================================================================
Called when roving syndicate tick box is changed
================================================================ */
function caRovingChanged(){
    var elRoving  = document.getElementById('rovingSyndicate');
    var isSyndicateAvailable = caIsSyndicateAvailable();
    if (! isSyndicateAvailable || elRoving.checked==true){
        // enable all options for ROVING syndicates
        caEnableLocations(true,true,true);
        // and enable the non-spefic county options
        caEnableCountyOptions(true,true );
    }
    else{
        // enable location and county options  for NON ROVING syndicates
        caEnableLocations(true,true,false);
        // and disable the non-spefic county options
        caEnableCountyOptions(false,false );

    }
}
/* ================================================================
// helper function to enable/disable the non-specific county options
================================================================ */
function caEnableCountyOptions(opt1, opt2){
     var elCounty  = document.getElementById('adCounty');
     elCounty.options[1].disabled = ! opt1;
     elCounty.options[2].disabled = ! opt2;
}
/* ================================================================
// helper function to return true if an advert is for syndicate places available
================================================================ */
function caIsSyndicateAvailable(){
    var selectedCat = selCat.options[selCat.selectedIndex].value;
	var isSyndicate = selectedCat.indexOf("Syndicate")> -1;
    var isAvailable = selectedCat.indexOf("Available")> -1;
    return (isSyndicate && isAvailable);
}
/* ================================================================
  Called when any of the various location selectors are changed
================================================================ */
function caLocationChanged(shootChanged, countyChanged, regionChanged){

    var elShoot  = document.getElementById('adLocationID');
    var elCounty  = document.getElementById('adCounty');
    var elRegion  = document.getElementById('adRegion');
    var shootText =  elShoot.options[elShoot.selectedIndex].text.substr(0,6);
    var countyText =  elCounty.options[elCounty.selectedIndex].text.substr(0,6);
    var regionText =  elRegion.options[elRegion.selectedIndex].text.substr(0,6);

    if(shootChanged && shootText != 'Please'){
       elCounty.selectedIndex=0;
       elRegion.selectedIndex=0;
    }
    if(countyChanged && countyText != 'Please'){
       elShoot.selectedIndex=0;
       elRegion.selectedIndex=0;
    }
    if(regionChanged && regionText != 'Please'){
       elShoot.selectedIndex=0;
       elCounty.selectedIndex=0;
    }
}
/* ================================================================
// helper function to enable/disable the various location SELECTS
================================================================ */
function caEnableLocations(enableShoot, enableCounty,enableRegion){
    var elShoot  = document.getElementById('adLocationID');
    var elCounty  = document.getElementById('adCounty');
    var elRegion  = document.getElementById('adRegion');

    elShoot.disabled = ! enableShoot;
    elCounty.disabled = ! enableCounty;
    elRegion.disabled = ! enableRegion;
}

/* ================================================================
 Validator to check that at least one of teh options has been selected
 ie, that they're not all set to Please Select
================================================================ */
function caLocationSelected(){

    var elShoot  = document.getElementById('adLocationID');
    var elCounty  = document.getElementById('adCounty');
    var elRegion  = document.getElementById('adRegion');
    var shootText =  elShoot.options[elShoot.selectedIndex].text.substr(0,6);
    var countyText =  elCounty.options[elCounty.selectedIndex].text.substr(0,6);
    var regionText =  elRegion.options[elRegion.selectedIndex].text.substr(0,6);
    if ( shootText ==  'Please' && countyText == 'Please' &&  regionText == 'Please') return ('Please select a location using one of the available options'); //(false);
    return('');
}
