	
	
//var selectedTalkplan = "";
//var selectedContractLength = "";

var contractLengthComplete = false;
var contractTalkplanComplete = false;
var informationChanged = false;

function buildQueryUrl(action) {
	var url = "";
	try {
		var url = "/wcm/site/pages/queryDropdownRequest.aspx";
		url = url + "?action=" + action + "&continuitysessionid=" + document.getElementById("csessionid").value;
		
		//if talkplan chosen add to querystring
		if (selectedTalkplan != '') 
			url += "&talkplan=" + selectedTalkplan;
			
		//if contract length chosen add to querystring
		if (selectedContractLength != '') 
			url += "&contract_length=" + selectedContractLength;
			
		document.getElementById ("debug").value += "\r\n" + url;
		
	} catch (e) {
		alert (e.message);
	}
	
	return url
	
}


function updateDropdowns () {
	try {
		//show wait message 
		document.getElementById ("wait").style.display='block';
		
		contractLengthComplete = false;
		contractTalkplanComplete = false;


		//update contract length dropdown

		new Ajax.Updater ('contract_length', buildQueryUrl ("contractlengths"), 
				{
					method: 'get',
					onComplete: function() {
						contractLengthComplete = true; // mark complete
						if (contractLengthComplete && contractTalkplanComplete) 
							document.getElementById ("wait").style.display='none';
							
						repopulate("contract_length");
						document.getElementById ("originalcontract_length").value=document.getElementById ("contract_length").value;
					}
				}
			);
		new Ajax.Updater ('talkplan', buildQueryUrl ("talkplan"), 
				{
					method: 'get',
					onComplete: function() {
						contractTalkplanComplete = true; // mark complete
						if (contractLengthComplete && contractTalkplanComplete) 
							document.getElementById ("wait").style.display='none';
							
						repopulate("talkplan");
						document.getElementById ("originaltalkplan").value=document.getElementById ("contract_length").value;
					}
				}
			);
	} catch (e) {
		alert (e.message);
	}
}




function repopulate(controlName) {
	try {
		var origValue = document.getElementById ("original" + controlName).value;
		var element = document.getElementById (controlName);
		for (var optCount=0;optCount < element.options.length;optCount++) {
			if (element.options[optCount].value == origValue)
				element.selectedIndex = optCount;
		}
	} catch (e) {
		alert (e.message);
	}
}






//choose contract length item
//update talkplans passing in chosen values
function queryContractLengthSelect() {
	selectedContractLength = document.getElementById ("contract_length").value;
	updateDropdowns();
}

function setInformationChanged(element){
	if( element ){
	
		if( (element.id == 'addon1' ) || (element.id == 'addon2' ) || (element.id == 'addon3' ) || (element.id == 'incorrectAddon') || (element.id == 'talkplan') ){
			var optionValue = element.options[element.selectedIndex].value;
			var toDealerValue = optionValue.substring( optionValue.indexOf( '[' ) + 1, optionValue.indexOf( ']' ) );	
			var elementId = "d" + element.id;
			var finalvalue = document.getElementById ("poundchar").value + toDealerValue;
			document.getElementById( elementId ).innerHTML = "";
			document.getElementById( elementId ).innerHTML = "(" + finalvalue + ")";		
		}

	}
	informationChanged = true;
}

function alertIfInformationChanged(){
	if( informationChanged == true ){
		return confirm( "Any information entered will be lost if you proceed.\nSelect \"Ok\" to proceed to unsubmitted queries or Select \"Cancel\" to go back and Submit this query" )
	}
}


//opens wildcard search window 
function openWildcardSearch (controlname, actionLookup, fields) {
	try {
		url = "/your_finance/commision_queries/search_item?wildcard=true&conn_date=" + document.getElementById ("conn_date").value + "&network_id=" + document.getElementById ("network_id").value + "&querytype=1&control=" + controlname + "&actionlookup=" + actionLookup;
		for (var n=0;n<fields.split(",").length;n++) {
			url += "&" + fields.split(",")[n] + "=" + document.getElementById (fields.split(",")[n]).value;
		}
		
		window.open (url, "search", "width=600,height=500,resizable=yes,scrollbars=yes");
	} catch (e) {
		alert (e.message);
	}	
}



//Wildcard functions
function getSelectedValue () {
			
	try {

		var i=1;
		var checkedValue = "";
		element = document.getElementById ("option" + i);
		while (element) {
			element = document.getElementById ("option" + i);
			if (element.checked) {
				return element.value;
			}
			i++;
		}
		
	} catch (e) {
		alert ("getSelectedValue:" + e.message);
	}
	
	return "";
}


function populateElement (elementName) {	
	try {
	
		var selectedValue = getSelectedValue();
		
		if (selectedValue) 
			if (selectedValue != "") {
			
				var openEl = window.opener.document.getElementById (elementName);
				
				//loop for all options in calling element and select value
				for (var i=0;i < openEl.options.length;i++) {
					var option = openEl.options[i];
					if (option.value == selectedValue)
						openEl.selectedIndex = i;
						
				}
				//Get the talkplan value from the selectedValue
				var toDealerValue = selectedValue.substring( selectedValue.indexOf( '[' ) + 1, selectedValue.indexOf( ']' ) );	
				var elementId = "d" + elementName;
				var finalvalue = window.opener.document.getElementById ("poundchar").value + toDealerValue;
				window.opener.document.getElementById( elementId ).innerHTML = "(" + finalvalue + ")";		
	
			}
			
		window.close();
	} catch (e) {
		alert ("populateElement:" + e.message);
	}
}

function hyperlinkClick( element ){
	var extraParam = "&javascriptEnabled=1";
	var currentURL = element.href;
	currentURL += extraParam;
	window.location = currentURL;
	return false;
}