/**
	Author: Nicola Antonio Filannino
	Date: 11-12-2008
	Description:
	
	This script turns a basic B2CSearchFormHandler based form
	into an Ajax working one.
	It passes a block of parameters simulating the old version
	making a 'silent' Ajax call
*/
var AjaxSearch_lastSearchedKeyword="";
var AjaxSearch_form=null;
var AjaxSearch_state="???";


function AjaxSearch_doSilentSearch(searchKeyword,locale)
{	
	locale=(!locale)?'en_US':locale;
	var isAsynchCall=true;
	

	var form = document.getElementsByTagName('form').item(0);
	var formElems = form.childNodes;
	var dynSessConf = null;
	
	for(var i=0;i<formElems.length;i++)
	{
	   if (formElems.item(i).name=='_dynSessConf')
	       dynSessConf = formElems.item(i).value;
	}
	
	var queryString="_dyncharset=UTF-8&"+
    "%2Fatg%2Fcommerce%2Fcatalog%2FCatalogSearch.searchInput="+searchKeyword+"&"+
    "_D%3A%2Fatg%2Fcommerce%2Fcatalog%2FCatalogSearch.searchInput=+&"+
    "repositoryKey="+locale+"&"+
    "%2Fatg%2Fcommerce%2Fcatalog%2FCatalogSearch.search=search&"+
    "_D%3A%2Fatg%2Fcommerce%2Fcatalog%2FCatalogSearch.search=+&"+
    "%2Fatg%2Fcommerce%2Fcatalog%2FCatalogSearch.successUrl=&"+
    "_D%3A%2Fatg%2Fcommerce%2Fcatalog%2FCatalogSearch.successUrl=+&"+
    "%2Fatg%2Fcommerce%2Fcatalog%2FCatalogSearch.search=OK&"+
    "_D%3A%2Fatg%2Fcommerce%2Fcatalog%2FCatalogSearch.search=+&"+
    "_DARGS=%2Fbulgari%2Fus%2Fjsp_common%2Fheader.jsp&"+
    "_dynSessConf="+dynSessConf;
    
	

	
	
	myRequest = getXMLHttp(AjaxSearch_handleSilentSearch);
	myRequest.open("GET","/bulgari/us/product/search/searchProd.jsp?"+queryString,isAsynchCall);
	myRequest.onreadystatechange = AjaxSearch_handleSilentSearch;
	myRequest.send(null);
	
	
}

/**
	Don't modify below function logic, please modify 
	following handlers instead (AjaxSearch_on[EVENT_NAME]).
*/
function AjaxSearch_handleSilentSearch()
{
	 if (myRequest.readyState == 4 && myRequest.status == 200)
	 {
	 	var xml=myRequest.responseXML;
	 	// Sometimes when a 'parsererror' error occurs (Firefox) or 
	 	// there is no 'empty' tag and no 'prod' tag (Msie)
	 	// the search request needs to be reloaded.
	 	var requestReloadNeeded = (
	 									((xml.getElementsByTagName('empty').length==0) && (xml.getElementsByTagName('prod').length==0))
	 									||
	 									(xml.getElementsByTagName('parsererror').length>0)
	 							  );
	 	
	 	
	 	if (requestReloadNeeded)
	 	{
	 		return AjaxSearch_onParserError();
	 	}
	 	// If xml contains an <empty> tag or NO <prod> tag
	 	// we consider the search result as empty
	 	if ((xml.getElementsByTagName('empty').length>0) || (xml.getElementsByTagName('prod').length<=0))
	 	{
	 		AjaxSearch_onEmptySearch();
	 	}
	 	// The search-result is NOT EMPTY so we  
	 	// proceed unlocking the form and submitting it.
	 	else
	 	{
		 	AjaxSearch_onSuccessSearch();
	 	}
	 }
}


function AjaxSearch_onParserError()
{
	//alert ('parserError');
	AjaxSearch_state="parserError";
	return AjaxSearch_doSilentSearch(AjaxSearch_lastSearchedKeyword);
}

function AjaxSearch_onSuccessSearch()
{
 		AjaxSearch_unlockForm();
 		AjaxSearch_form.submit();
 		AjaxSearch_state="SuccessfulSearch";
 		return true;
}

function AjaxSearch_onEmptySearch()
{
	showWebMessageBox(200,300,"Message","No items match your search criteria.<br> Please try again, or call 1-800-BVLGARI");
	$('searchfield').focus();
	AjaxSearch_state="EmptySearch";
}


function AjaxSearch_onSubmitSearch()
{
	AjaxSearch_lastSearchedKeyword=escape($('searchfield').value);
	document.getElementsByTagName('form').item(0).action='';
	isSearchResultEmpty=AjaxSearch_doSilentSearch(AjaxSearch_lastSearchedKeyword);
	//alert ("state="+AjaxSearch_state);	
	urchinTrackerSearchExec();
}

function AjaxSearch_onSubmitGotoDisplayResult()
{	
	return true;
}



function AjaxSearch_lockForm()
{	
	var field=$('searchfield');
	AjaxSearch_form=document.getElementsByTagName('form').item(0);
	AjaxSearch_form.addEvent('submit', foo.bindWithEvent(AjaxSearch_form));
}

function AjaxSearch_unlockForm()
{	
	document.getElementsByTagName('form').item(0).action='/bulgari/us/product/search/displayResult.jsp'
	AjaxSearch_form.onsubmit=function(){return AjaxSearch_onSubmitGotoDisplayResult();};
}

function goToSearch(lang){
	var search_text = document.getElementById('searchfield');
	var value="";
	if(search_text.value!=""){
		value = search_text.value.replace(/ /gi,"_").replace( /\'/gi, "" );
		value =escape(value);
		if(search_text.value=="Search") 
			{
				value = "ALL";
			}
	}else{
		value = "ALL";
	}
	
var temp = document.getElementsByTagName('form').item(0).action.replace('/bulgari/en_US/product/search/displayResultSearch.jsp','/bulgari/'+lang+'/product/search/displayResultSearch.jsp');
temp = temp +'?scProd='+value;
	document.getElementsByTagName('form').item(0).action=temp;
this.location.href=temp;
	return true;
	/*}else{
		return false;
	}*/
	
}