/* ============================================================================= *
 * Constants																 	 *
 * ============================================================================= */

var FAILURE_MESSAGE = "<div class='debug'>The request failed. The Pro Web service may be unavailable. Please try later.</div>";
var TIMEOUT_MESSAGE = "<div class='debug'>The request timed out. Please try again.</div>";

/* ============================================================================= *
 * Ajax functions																 *
 * ============================================================================= */


function submitVerifForm(formId) {	
	show('#stopwatch');
	$(formId).ajaxSubmit({
		success: requestSuccess,
		timeout: requestTimeout
	});
	return;
}


var timerId = 0;
function requestSuccess(content) {
	hide('#stopwatch');
	$('#address').html(content);		
		
	// Delay required for IE
	timerId = setTimeout("setFocus();", 1);		
	return;
}


function requestFailure(content) {
	hide('#stopwatch');
	$('#address').html(FAILURE_MESSAGE);			
	return;
}


function requestTimeout() {
	hide('#stopwatch');
	$('#address').html(TIMEOUT_MESSAGE);		
	return;
}

/* ============================================================================= *
 * Page functions																 *
 * ============================================================================= */

/* 1. -----------------------------------------------------------  verifyInit.jsp */

/* Next Button */						
function doSearch() {				
	if (isVerifSearchNonEmpty()) {						
		setCountryValue(false);
		submitVerifForm('#formAddress');	
	}
}
		
function isVerifSearchNonEmpty() {
	var aUserInput = document.formAddress.UserInput;
	var isEmpty = true;
	for (var i=0; i < aUserInput.length; i++) {		
		if (aUserInput[i].value != "") {
			isEmpty = false;
		} 
	}
	if (isEmpty) {
		alert("Please enter some address details.");
	}
	return !isEmpty;
}	
			
function setCountryValue(clearAddress) {
  	var iSelected = document.formAddress.partnerData.selectedIndex;
  	var sCountry = document.formAddress.partnerData.options[iSelected].value;
  
  	if (isPartnerData(sCountry)) {
		sCountry = sCountry.substr(1,sCountry.length);
  	} else {
  		//trigger change back to flat.prompt.demo.jsp
  		switchToFlat(sCountry, document.formAddress.partnerData.options[iSelected].text);
  	}
  
	document.formAddress.DataId.value = sCountry;

  	if ( document.formAddress.partnerData.value != '') {
  		document.formAddress.CountryName.value = document.formAddress.partnerData.options[iSelected].text;
	} 
	
	if (clearAddress) {
		var aUserInput = document.formAddress.UserInput;
		for (var i=0; i < aUserInput.length; i++) {		
			if (aUserInput[i].value != "") {
			 aUserInput[i].value = ""; 
			 }
		}
	}
}						
				
		
/* 2. -----------------------------------------------------------  verify.interaction.jsp */

/* Picklist */
function doRefine() {	
	if (document.getElementById("RefineInput").value != "") {
		submitVerifForm('#formRefine');		
	} else {
		alert("Please enter your exact details.");				
	}
}

/* Suggested Address */
function acceptSuggested(){
	submitVerifForm('#formAddress');	
}

/* User input */
function acceptUserInput() {
	submitVerifForm('#formUserInput');
}

/* Back Button */
function backToPrompt() {
	document.formUserInput.Command.value = "VerifyInit";
	submitVerifForm('#formUserInput');
}

/** PICKLIST **/

/* picklist item hyperlink has been clicked: pick item, command and submit */
function doPicklist() {	
	var iIndex = document.formPicklist.matches.options.selectedIndex;								
	if (iIndex >= 0) {
		document.formPicklist.Command.value = g_asCommands[iIndex];
		document.formPicklist.Moniker.value = g_asMonikers[iIndex];
		submitVerifForm('#formPicklist');
		document.formPicklist.matches.disabled = true;
	} else {
		alert("Please select an option");		
	}
}

/* 3. -----------------------------------------------------------  verify.address.jsp */

/* Back Button */
function backToPicklist() {					
	// Remove moniker so search is resubmitted with user entered params 								
	submitVerifForm('#formFinalAddress');				
}

/* Back Button - to start (single match)*/
function backToStart() {
	document.formFinalAddress.Command.value = "VerifyInit";
	submitVerifForm('#formFinalAddress');				
}

/* New Search Button */
function newSearch(qs, dataId) {			
	show('#loading');
	
	qs = qs+"&DataId="+dataId;
	
	$.ajax({
		type: "POST", 
		url: "/proweb/QasController6", 
		data: qs, 
		dataType: "html", 
		success: requestSuccess, 
		error: requestFailure, 
		timeout: requestTimeout
	});
}


/* ============================================================================= *
 * Helper functions																 *
 * ============================================================================= */

function switchToFlat(sCountryCode, sCountryName) {	
	show('#stopwatch');
	// Required for FR contact form. Switches to FRX
	if ((sCountryCode == "FRP") && (mode == "") && (document.formAddress.siteId.value == "111")) sCountryCode = "FRX";
	
	var command = "FlatPromptEntry";
	if (sCountryCode == "FRX") {
		command="VerifyInit";
	} 
		
	var qs = "Command=" + command +
			"&DataId=" + sCountryCode + 
			"&siteId=" + document.formAddress.siteId.value + // site id required for i18n in proweb			
			"&CountryName=" + sCountryName;
			
	if (sCountryCode != "FRX") {
		qs += "&newCountry=true";
	} 				
		
	sendRequest(qs);		
}

/* Check the event object for an "Enter" press match */
function checkEnter(e, action) {     
	e = e || window.event;
	if (e.keyCode == 13) {
	   return true;
	} else {
		return false;
	}
}

function hide(id) {
	$(id).css('visibility', 'hidden');
}

function show(id) {
	$(id).css('visibility', 'visible');
}

function setFocus() {
	
	if (document.formPicklist != null) {
		document.formPicklist.matches.focus();		
	} 
	
	if (document.formAddress != null) {	
		document.formAddress.UserInput[0].focus();	
	}
	
	if (document.formFinalAddress != null) {	
		document.formFinalAddress.Address[0].focus();	
	}
}
