var tnProxy = new tryNumberProxy();
tnProxy.setErrorHandler(proxyErrorHandler);

//This function is called from the Try Number Bar whenever the user selects a new DNIS group (i.e., country)
function changeDNISGroup(groupObj) {
	if (groupObj.options[groupObj.selectedIndex].value == "")
		groupObj.selectedIndex = 0;
	tnProxy.setCallbackHandler(loadDNISTypes);
	tnProxy.getDNISTypes(groupObj.options[groupObj.selectedIndex].value,true);
	}

//This function is called from the Try Number Bar whenever the user selects a new DNIS type
function changeDNISType(dnisTypeObj) {
	tnProxy.setCallbackHandler(loadDNISes);
	tnProxy.getDNISByType(dnisTypeObj.options[dnisTypeObj.selectedIndex].value);
	}

//This function is called from the Try Number Bar whenever the user selects a new destination country
function changeDestination(destinationObj) {
	var prefix = destinationObj.options[destinationObj.selectedIndex].value;
	if (prefix == "") {
		prefix = "1-";
		destinationObj.selectedIndex = 0;
		}
	else
	if (prefix == 1)
		prefix = "1-";
	else
	if (prefix == 999)
		prefix = "device@sip.yourdomain.com";
	else {
		prefix = "+" + prefix;
		prefix = prefix + "-";
		}
	document.tryNumberForm.txtRingTo.focus();
	document.tryNumberForm.txtRingTo.value = prefix;
	saveUserSelections(true);
	}

var loadDNISTypes = function(results) {
	with(document.tryNumberForm) {
		selDNISType.options.length = 0;
	    for (i=0; i<results.DATA.length; i++){
	        var option = new Option();
			option.text = results.DATA[i][results.COLUMNS.findIdx("SHORTNAME")];
			option.value = results.DATA[i][results.COLUMNS.findIdx("DNISTYPE")];
			option.title = option.text;
	        selDNISType.options[i] = option;
	    	}
	    selDNISType.selectedIndex = 0;
	    changeDNISType(selDNISType);
		loadToolTipContent(document.tryNumberForm.selDNISType.value);
		
		}
	}

var loadDNISes = function(results) {
	with(document.tryNumberForm) {
		selDNIS.options.length = 0;
	    for (i=0; i<results.DATA.length; i++){
	        var option = new Option();
			option.text = results.DATA[i][results.COLUMNS.findIdx("APPEAR")];
			option.value = results.DATA[i][results.COLUMNS.findIdx("DNIS")];
			option.title = option.text;
	        selDNIS.options[i] = option;
	    	}
	    selDNIS.selectedIndex = Math.floor(Math.random() * (results.DATA.length-1));
		}
	saveUserSelections(true);
	}

//  This function is fired every time the user clicks in the Ring To text box; if by default we pre-populate
//	that field with "device@sip.yourdomain.com" when SIP is chosen as the destination "country," but we don't
//	want that domain going into the cart, since it's meant simply for an example and not actual real data.
function checkRingTo(ringToObj) {
	if (ringToObj.value.toLowerCase().indexOf("yourdomain.com") >= 0)
		ringToObj.value = "";
	}

//  refreshRates is a boolean value that indicates whether the Rates page should be refreshed
//	after the user's Try Number Bar selections are saved (assuming we're actually on the Rates page)
function saveUserSelections(refreshRates) {
	refreshRatesFlag = refreshRates;
	with (document.tryNumberForm) {
		var dnisGroup = selDNISGroup.options[selDNISGroup.selectedIndex].value;
		var type = selDNISType.options[selDNISType.selectedIndex].value;
		var number = selDNIS.options[selDNIS.selectedIndex].value;
		var toCountry = selDestination.options[selDestination.selectedIndex].value;
		var ringTo = trim(txtRingTo.value);
		tnProxy.setCallbackHandler();
		if (isRatesPage && refreshRatesFlag){
			$("#countryrates").load(ratesPageURL+"&tocountry="+toCountry+"&type="+type);
		}
		tnProxy.saveTryNumber(dnisGroup,type,number,toCountry,ringTo);
		}
	}

var saveUserSelectionsCallback = function() {
	//  If we're on the Rates page, auto-submit the form
	if (isRatesPage && refreshRatesFlag)
		window.location.href = ratesPageURL;
	}

function addToCart() {
	with (document.tryNumberForm) {
		var dnis = selDNIS.options[selDNIS.selectedIndex].value;
		var dnisType = selDNISType.options[selDNISType.selectedIndex].value;
		var longName = "";
		var appear = "";
		var destinationCountryCode = selDestination.options[selDestination.selectedIndex].value;
		var ringTo = trim(txtRingTo.value);
		var plan = "basic";
		}
	tnProxy.setCallbackHandler(goToCheckout);
	tnProxy.addToCart(dnis,destinationCountryCode,ringTo);
	}

var goToCheckout = function() {
	window.location.href = checkoutURL;
	}

// Error handler for the asynchronous functions
var proxyErrorHandler = function(statusCode, statusMsg) {
	/*
	if (document.getElementById("ajaxLoaderMsg1"))
		document.getElementById("ajaxLoaderMsg1").style.display = "none";
	*/
    alert('Status: ' + statusCode + ', ' + statusMsg);
	//alert("We could not process your request");
	}