/**
 * @fileoverview Venda.Ebiz.chooseContract - Only allow one contract and one offer to be selected.
 *
 * The values of the selected radio options are then passed through to the order
 * using Order Item Extended Fields.
 * 
 * @requires /venda-support/js/external/yui/build/yahoo-dom-event/yahoo-dom-event.js  
 * @author Hayley Easton <heaston@venda.com>
 */

//create chooseContract namespace
Venda.namespace("Ebiz.chooseContract");

/**
 * Stub function is used to support JSDoc.
 * @class Venda.Ebiz.chooseContract
 * @constructor
 */
Venda.Ebiz.chooseContract = function(){};

/**
 * Create a new contract object
 * @param {array} 	radioHooks specify which anchors will populate the Contract Order Item Extended Field
 */
Venda.Ebiz.chooseContract.create = function(radioHooks,tags) {
	Venda.Ebiz.chooseContract.tags = tags;
	
	//register listener for form submit and radio selections
	YAHOO.util.Event.addListener("addtobasket", "submit", Venda.Ebiz.chooseContract.interceptForm);
	YAHOO.util.Event.addListener(radioHooks, "click", Venda.Ebiz.chooseContract.setRadios);
};
var noOffer = false;
/**
 * Validate and submit
 * Checks if existing price plan in basket and asks user to confirm action
 * Checks that a contract length has been chosen and that a contract offer has been chosen
 * Checks are not necessary if only one radio exists
 * @param {event} e used to suppress form submit
 */
Venda.Ebiz.chooseContract.interceptForm = function(e) {	
	var duration = Venda.Ebiz.chooseContract.getCheckedValue(document.forms['addtobasket'].elements['contractlength']);
	
	var duration1radios = document.forms['addtobasket'].elements['cl_duration1offer'];
	var duration2radios = document.forms['addtobasket'].elements['cl_duration2offer'];
	var duration3radios = document.forms['addtobasket'].elements['cl_duration3offer'];
	
	var offer1 = Venda.Ebiz.chooseContract.getCheckedValue(duration1radios);
	var offer2 = Venda.Ebiz.chooseContract.getCheckedValue(duration2radios);
	var offer3 = Venda.Ebiz.chooseContract.getCheckedValue(duration3radios);
	var offer = offer1+offer2+offer3;
	
	// if there is only 1 duration radio, there are no offer radios
	// single duration radio is not clicked therefore 'setRadios' function not invoked to declare the noOffer variable
	// therefore populate the noOffers variable as true
	if (duration1radios == undefined && duration2radios == undefined && duration3radios == undefined){
		noOffer = true;
	}
	
     if (duration == "") {
            // no contract radio button has been selected so don't allow this form to submit
            YAHOO.util.Event.stopEvent(e); // suppress form submit
            alert(Venda.Ebiz.chooseContract.tags.contractmessage);
            return false;
    } else {
            if (offer == "" && noOffer == false) {
    	        // there is an offer radio button but none have been selected so don't allow this form to submit
    	        YAHOO.util.Event.stopEvent(e); // suppress form submit
    	        alert(Venda.Ebiz.chooseContract.tags.contractoffermessage);
                return false;
            } else {
            	// push the duration into the contract order item extended field
            	document.getElementById('contract').value = duration;
            	document.getElementById('specialoffer').value = offer; // this could be blank if no offer radio present
            }
    }
	// look for confirm message telling the user a price plan is already in the basket
	if (Venda.Ebiz.chooseContract.tags.confirmmessage != ""){
		// if price plan is already in basket, ask user if they want to replace
		var answer = confirm(Venda.Ebiz.chooseContract.tags.confirmmessage);
		if (!answer){
			// user clicked cancel, do not submit form
			YAHOO.util.Event.stopEvent(e); // suppress form submit
		}
    // user clicked ok, continue to next page

	// there is no price plan in the basket so just validate the fields and allow add
	}else{
		if (duration == "") {
			// no contract radio button has been selected so don't allow this form to submit
			YAHOO.util.Event.stopEvent(e); // suppress form submit
			alert(Venda.Ebiz.chooseContract.tags.contractmessage);
		} else {
			if (offer == "" && noOffer == false) {
				// there is an offer radio button but none have been selected so don't allow this form to submit
				YAHOO.util.Event.stopEvent(e); // suppress form submit
				alert(Venda.Ebiz.chooseContract.tags.contractoffermessage);
			} else {
				// push the duration into the contract order item extended field
				document.getElementById('contract').value = duration;
				document.getElementById('specialoffer').value = offer; // this could be blank if no offer radio present
			}
		}
	}
};

/**
 * Contract offer radio behaviour
 * Checks that a contract length is selected and activates the corresponding offer radios
 */
Venda.Ebiz.chooseContract.setRadios = function() {
//	noOffer = false;
	var optionName = this.id+'offer';
	// enables the offer radios of the contract clicked i.e. disabled = false
	Venda.Ebiz.chooseContract.toggleGroup(document.forms['addtobasket'].elements[optionName], false);
	
	// find how many offer radios each duration has, if any
	if(document.forms['addtobasket'].elements[optionName]){
		var offersradioLength = document.forms['addtobasket'].elements[optionName].length;
		if(offersradioLength == undefined){
			// as there is only one offer option, it should be automatically checked
			document.forms['addtobasket'].elements[optionName].checked = true;
			}
	}else{
		// set variable for form submit
		noOffer = true;
	}
	
	if(this.id == "cl_duration1"){
		Venda.Ebiz.chooseContract.toggleGroup(document.forms['addtobasket'].elements['cl_duration2offer'], this.checked);
		Venda.Ebiz.chooseContract.toggleGroup(document.forms['addtobasket'].elements['cl_duration3offer'], this.checked);
		// display pdxtpromotion if duration = 24 months
		if (document.getElementById('promo_text')) {
			document.getElementById('promo_text').style.display = 'block';
		}
	}
	if(this.id == "cl_duration2"){
		Venda.Ebiz.chooseContract.toggleGroup(document.forms['addtobasket'].elements['cl_duration1offer'], this.checked);
		Venda.Ebiz.chooseContract.toggleGroup(document.forms['addtobasket'].elements['cl_duration3offer'], this.checked);
		// hide pdxtpromotion if duration != 24 months
		if (document.getElementById('promo_text')) {
			document.getElementById('promo_text').style.display = 'none';
		}
	}
	if(this.id == "cl_duration3"){
		Venda.Ebiz.chooseContract.toggleGroup(document.forms['addtobasket'].elements['cl_duration1offer'], this.checked);
		Venda.Ebiz.chooseContract.toggleGroup(document.forms['addtobasket'].elements['cl_duration2offer'], this.checked);
		// hide pdxtpromotion if duration != 24 months
		if (document.getElementById('promo_text')) {
			document.getElementById('promo_text').style.display = 'none';
		}
	}
	
};

/**
 * Turn off inactive child offer radios
 * Set the radio button with the given value as being checked
 * Do nothing if there are no radio buttons
 * If the given value does not exist, all the radio buttons
 * are reset to unchecked and disabled
 */
Venda.Ebiz.chooseContract.toggleGroup = function(radioObj,newValue) {
   if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].disabled = newValue;
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].disabled = false;
			radioObj[i].checked = newValue;
		}
	}
}

/**
 * Get chosen contract option
 * Return the value of the radio button that is checked
 * Return an empty string if none are checked, or
 * there are no radio buttons
 */ 
Venda.Ebiz.chooseContract.getCheckedValue = function(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}