/**
 * @author Gproxy Design Inc.
 * @copyright (c) 2009, Gproxy Design Inc. All rights reserved.
 * @version 1.0
 *
 * Gproxy dosen't allow to copy or change this code without Gproxy authorization.
 * Go to http://www.gproxy.com/licenses/license01.pdf for the full license governing this code.
 */

//_ca_ss_prl_get_states
var strGetStatesScriptUrl = "https://forms.netsuite.com/app/site/hosting/scriptlet.nl?script=55&deploy=1&compid=TSTDRV670388&h=aad3dfc4fbe643a76847";
//_ca_ss_prl_search_partners
var strGetStoresScriptUrl = "https://forms.netsuite.com/app/site/hosting/scriptlet.nl?script=54&deploy=1&compid=TSTDRV670388&h=bd8d35ac7789aa5dba2e";

/**
 * Initialize the store locator 
 */
function slInitialize() {	
	slGetStates();	
	$j("#sl_cnt_results").html("");
}


/**
 * Reset the search filters
 */
function slResetFilters() {	
	$j("#sl_cnt_results").fadeOut(1000, function () {
        $j("#sl_cnt_results_background").fadeIn(1000);
		slInitialize();
  	});	
}

/**
 * Get  the states for the store locator
 */
function slGetStates(){	
	var strCountryId = "US";	
	$j("#sl_cnt_loading").show();		
	var strParams = {      
        countryid: strCountryId       
    };
	$j.ajax({
		url: strGetStatesScriptUrl + "&callback=?",	      
		type: "GET",		
		dataType: "jsonp",
		data: strParams,
		success: function(data){
			if (data.Errors.length > 0) {  
				$j.each(data.Errors, function(i, val){	
					slErrorManager('States', val.code, val.details);	  		 			            
				});
			}else{
				$j("#sl_cnt_states").empty();
				$j("<select>").attr("id", "sl_states").appendTo("#sl_cnt_states");				
				if (data.States.length > 1){
					$j("#sl_states").attr("disabled","");				
				}else{
					$j("#sl_states").attr("disabled","disabled");
				}
				$j.each(data.States, function(i, val){
					var option = $j("<option>");
					option.text(unescape(val.text)).attr("value", unescape(val.value));
					option.appendTo("#sl_states");
				});		
				$j("#sl_states").change(slGetStores);
			}
		},		
		complete: function (XMLHttpRequest, textStatus) {
  			$j("#sl_cnt_loading").hide();
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			slErrorManager('States', textStatus, errorThrown);			
		}		  
	});				
}

/**
 * Search the partners depends on the selected filters 
 */
function slGetStores() {	
	var strStateId = escape($j("#sl_states").val());
	var strCountryId = "US"
	if (strCountryId) {
		if (strStateId) {
			$j("#sl_cnt_loading").show();		
			var strParams = {      
		        countryid: strCountryId,
				stateid:  strStateId     
		    };
			$j.ajax({
				url: strGetStoresScriptUrl + "&callback=?",	      
				type: "GET",		
				dataType: "jsonp",
				data: strParams,
				success: function(data){
					if (data.Errors.length > 0) {  
						$j.each(data.Errors, function(i, val){	
							slErrorManager('Stores', val.code, val.details);				  		 			            
						});
					}else{							
						$j("#sl_cnt_results_background").fadeOut(1000, function () {
							$j.each(data.Results, function(i, val){
							 	$j("#sl_cnt_results").html(unescape(val.html));
							});	
					        $j("#sl_cnt_results").fadeIn(1000);
				      	});
					}
				},		
				complete: function (XMLHttpRequest, textStatus) {
		  			$j("#sl_cnt_loading").hide();
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					slErrorManager('Stores', textStatus, errorThrown);						
				}		  
			});	
		} else {
			alert("Please select the state...");
		}

	} else {
		alert("Please select the country...");
	}
		
}

function slErrorManager(strSource, strCode, strDetails) {
	$j("#sl_cnt_msgerror").append("<li>Error requesting " + strSource + ", code: " + unescape(strCode) + ", details: " + unescape(strDetails) + "</li>");	
}

/**
 * Opend the url in a new window
 * @param {Object} url
 */
function slRedirect(url) {
	window.open(url);
}

function hideEmptyFields(strStoreId) {
	var strImgSrc = trim($j("#image_"+strStoreId).attr("src"));	
	if (strImgSrc == null || strImgSrc == ''){
		$j("#image_"+strStoreId).attr("src","/site/ry-template/ns-storelocatorresult_img.gif");		
	}
	var strEmaila = trim($j("#emaila_"+strStoreId).text());	
	if (strEmaila == null || strEmaila == '' || strEmaila == 'null'){
		$j("#email_"+strStoreId).hide();
		$j("#emaila_"+strStoreId).text("");
	}
	var strPhone = trim($j("#phone_"+strStoreId).text());	
	if (strPhone == null || strPhone == '' || strPhone == 'null' || strPhone == trim($j("#phone_"+strStoreId+">span").text())){
		$j("#phone_"+strStoreId).hide();
	}		
}

function trim(strText){
    if (strText === null) {
        return null;
    }    
    return strText.replace(/^\s*/, '').replace(/\s+$/, '');
}
