	/*
	 * This file houses ajax specific and related functions
	*/	
	
	//use this variable when you need some cleanup done post request or have chained requests
	//this will be reset on each use
	var cleanUpFunction = emptyCleanUp;
	
	//should be set by inc_ajax.jsp
	//some sites have html output by header files, this takes care of that
	var ajaxJSONStartString = '';
	
	function callAjaxRequest(fullPath, successFunction, failureFunction, createFunction) {	
		createFunction();
	
		dojo.xhrGet({
			url: fullPath,
			handleAs: "text",
			load: function(data, args){
				successFunction(data, args);
										
				cleanUp();
			},
			// if any error occurs, it goes here:
			error: function(error, args){
				failureFunction(error, args);
			}
		});
	}	
	
	var emptyCleanUp = function() {
	}
	
	function cleanUp() {
		var cleanUpFunc = cleanUpFunction;
					
		//we clean up first in case they have a third link in the chain
		cleanUpFunction = emptyCleanUp;
		
		if( cleanUpFunc != null && cleanUpFunc != '')
			cleanUpFunc();	
	}
	
	/*************state functions*************/
	var stateFieldId = '';
	var stateAjaxRequestURL = '';
	var stateSelectedArray = new Array();
	
	//labels - should be set on individual pages
	var usCaStateLabel = '';
	var foreignStateLabel = '';
	var zipLabel = '';
	var zipRequiredLabel = '';
	
	var usaProtectorates = new Array("AS", "FM", "GU", "MH", "MP", "PW", "PR", "VI");
	
	function populateState(path, dataElement, fieldId) {
		stateFieldId = fieldId;
		
		var params = '&dataElement=' + dataElement;
		
		callAjaxRequest(path+params, stateOnSuccess, stateOnFailure, stateOnCreate);
	}
	
	var stateOnSuccess = function(data, args) {
		var stateRow;
		var stateOption; 
		var stateText;
		
		//removes any code that might exist from header jsp includes
		var jsonStr = strAfter(ajaxJSONStartString, data);				
		var states = dojo.fromJson(jsonStr);
		
		//reset dropdown
		emptyList(stateFieldId);

		var stateLength = states.states.length;
		for( var i = 0; i < stateLength; i++ ) {
			stateRow = states.states[i];
			
			if( stateRow != undefined && stateRow != null ) {
				stateOption = document.createElement('option');
				stateOption.value = stateRow.code;
				stateText = document.createTextNode(stateRow.decode);
				stateOption.appendChild(stateText);
			 
				dojo.byId(stateFieldId).appendChild(stateOption);
			}
		}
	}
	
	var stateOnFailure = function(error, args) {
	}

	var stateOnCreate = function() {
		if( dojo.byId(stateFieldId) == undefined )
			createStateDropdown();
		
		dojo.byId(stateFieldId).options.length = 0; 
		
		var stateOption = document.createElement('option');						
		stateOption.appendChild(document.createTextNode('loading...'));
		
		dojo.byId(stateFieldId).appendChild(stateOption);
	}
	
	//type signifies whether billing or shipping fields
	//make sure initial id's have this type string in front of the std id for city and country
	function updateState(type)
	{ 
		var city = dojo.byId(type + 'citycd');
		var country = dojo.byId(type + 'countrycd');
		
		var cityValue = '';
		var countryValue = '';
		if( city )
			cityValue = city.value.toLowerCase();
		//if there is no country should default to us for sake of ligic
		if( country )
			countryValue = country.value.toLowerCase();
		else
			countryValue = 'us';
		
		updateZip(type, countryValue);
		
		//used to see if we need to populate the drop down or not
		var index = getStateSelectedArrayIndex(type);
		var selectedState = stateSelectedArray[index];
		
		//usually want to show country, so just set it to show for now
		var countryDivID = type + 'CountryDiv';
		if( dojo.byId(countryDivID) )
			dojo.byId(countryDivID).style.display = '';
		
		//determine what dataElement to use
		//update for apo
		if(cityValue == 'apo' || cityValue == 'fpo')
		{			
			//hide country
			if( dojo.byId(countryDivID) )
				dojo.byId(countryDivID).style.display = 'none';
		
			//update country to be us
			if( country )
				country.value = 'US';
		
			//need to update zip again...order issue
			updateZip(type, 'us');
		
			if( selectedState != 'APO_FPO' )
				stdStateUpdate('APO_FPO', type);
		}
		//update w/ us states
		else if(countryValue == 'us'){
			if( selectedState != 'US' )
				stdStateUpdate('US', type);
		}
		//update w/ canadian provinces
		else if(countryValue == 'ca'){
			if( selectedState != 'CA' )
				stdStateUpdate('CA', type);
		}
		//update for foreign/unknown country
		else
		{			
			foreignStateUpdate(type);
		}
	}
	
	function stdStateUpdate(domainElement, type){ 
		//update state label
		dojo.byId(type + 'StateLabelDiv').innerHTML = usCaStateLabel;
	
		//hide textbox and show drop down
		dojo.byId(type + 'StateDropDownDiv').style.display = '';
		dojo.byId(type + 'StateTextBoxDiv').style.display = 'none';
		
		updateStateSelectedArray(type, domainElement);	
		
		//url, domain, fieldId
		populateState(stateAjaxRequestURL, 'state_' + domainElement, type + 'state_DropDown');
	}	
	
	function foreignStateUpdate(type){
		//determine if we need to update
		if( stateSelectedArray[getStateSelectedArrayIndex(type)] != 'FOREIGN' )
		{
			updateStateSelectedArray(type, 'FOREIGN');
			
			//update state label
			dojo.byId(type + 'StateLabelDiv').innerHTML = foreignStateLabel;
			
			//hide dropdown and show textbox
			dojo.byId(type + 'StateDropDownDiv').style.display = 'none';
			dojo.byId(type + 'StateTextBoxDiv').style.display = '';	
			
			//we need to add an option to the drop down, if there are none, and
			//give it a selection to avoid an issue if it's a required field
			emptyList(type + 'state_DropDown');
		
			var stateOption = document.createElement('option');						
			stateOption.appendChild(document.createTextNode('TEMP'));
			
			dojo.byId(type + 'state_DropDown').appendChild(stateOption);
			dojo.byId(type + 'state_DropDown').selectedIndex = 0;
		}
		
		//mimics ajax cleanup func to make life a little easier
		cleanUp();
	}
	
	function updateStateSelectedArray(type, domainElement){
		var index = getStateSelectedArrayIndex(type);
		stateSelectedArray[index] = domainElement;
	}
	
	function getStateSelectedArrayIndex(type){
		var index = type;
		if( index == undefined || index == null || index == '' )
			index = 'none';		
		return index;
	}
	
	function isUSAProtectorate(countryValue) {
		if( dojo.indexOf(usaProtectorates, countryValue) > -1 )
			return true;
		
		return false;
	}

	//update label and whether zip is required
	function updateZip(type, countryValue) {
		countryValue = countryValue.toUpperCase();
		
		var zipLabelID = type + 'ZipLabelDiv';
		var zipReqID = type + 'ZipRequired';
		
		//zip required
		if( countryValue == 'US' || countryValue == 'CA'
				|| isUSAProtectorate(countryValue) )
		{			
			dojo.byId(zipLabelID).innerHTML = zipRequiredLabel;
		}
		//zip not required
		else{
			dojo.byId(zipLabelID).innerHTML = zipLabel;
		}
	}
	/*************************************/
	
	/********************Product Functions**********************/
	var printProductAjaxRequestURL = '';
	
	function printProduct() {				
		callAjaxRequest(printProductAjaxRequestURL, productPrintOnSuccess, productPrintOnFailure, productPrintOnCreate);
	}
	
	var productPrintOnSuccess = function(data, args) {
		var pwin=window.open('','print_content','');

		pwin.document.open();
		pwin.document.write(data);
		pwin.document.close();
	 
		setTimeout(function(){pwin.close();},1000);
	}
	
	var productPrintOnFailure = function(error, args) {
	}

	var productPrintOnCreate = function() {		
	}
	/*******************************************************/
	
	/***********So these may be more utilitarian than  ajax specific functions...but they mostly get used with ajax so...yeah****************/
	//returns every after needle in the haystack
	//returns null on error
	function strAfter(needle, haystack)
	{
		try{
			var index = haystack.indexOf(needle);
			haystack = haystack.substr(index+needle.length, haystack.length);
			return haystack;
		}catch(err){
			return null;
		}
	}
	
	function strReplace(needle, haystack)
	{
		try{
			var index = haystack.indexOf(needle);
			haystack = haystack.substr(index+needle.length, haystack.length);
			return haystack;
		}catch(err){
			return null;
		}
	}
	
	function createDropdown(parentId, elementId, elementName) {		
		var dropDown = document.createElement("SELECT");
		dropDown.name = elementName;
		dropDown.id = elementId;
		dropDown.setAttribute("secure", "false");
		
		dojo.byId(parentId).appendChild(dropDown);
	}

	function createTextBox(parentId, elementId, elementName, value) {
		var textField = document.createElement("INPUT");
		textField.name = elementName;
		textField.id = elementId;
		
		textField.type = "text";
		textField.value = value;
		textField.setAttribute("secure", "false");
		
		dojo.byId(parentId).appendChild(textField);
	}
	
	function createHiddenInput(parentId, elementId, elementName, value) {
		var hiddenField = document.createElement("INPUT");
		hiddenField.name = elementName;
		hiddenField.id = elementId;
		
		hiddenField.type = "hidden";
		hiddenField.value = value;
		hiddenField.setAttribute("secure", "false");
		
		dojo.byId(parentId).appendChild(hiddenField);
	}
	
	function cleanElement(el) {
		dojo.byId(el).innerHTML = '';
		/*while (el.firstChild) {
				// nothing too fancy or fast here - removing all child elements to clean it out
				el.removeChild(el.firstChild);
		}*/
	}
	
	//dang ie won't let me do options.length = 0...fragga bagga dagga dang dong arg
	function emptyList(id) {
		var len = dojo.byId(id).options.length;
		var optionchild = null;
		for(var i=len-1; i >= 0; i--) {
			optionchild = dojo.byId(id).options[i];
			dojo.byId(id).removeChild(optionchild);
		} 
	}
	/***********end util functions****************/