/** START: General Functions **/
function removeAllDropDownOptions(dropdown_obj_name){
	for(i=dropdown_obj_name.options.length-1; i >= 0; i--)
		dropdown_obj_name.remove(i);
}
	
function addNewDropDownOption(dropdown_obj_name, option_display_text, option_value){
	//alert(dropdown_obj_name.options.length);
	dropdown_obj_name.options[dropdown_obj_name.options.length] = new Option(option_display_text, option_value, dropdown_obj_name.options.length);
}

function trim (str) {
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}
/** END: General Functions **/

/*************************************************************************/
/****************** Get Delivery Times for *******************************/
/*************************************************************************/
function getStorePickUpTimes(store_id, times_dropdown_field, app_virtual_dir, live_domain, selected_time_value) {
	//send data to CF
	//DWRUtil.useLoadingMessage();
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'ax_getStorePickUpTimes', store_id, times_dropdown_field, app_virtual_dir, live_domain, selected_time_value, getStorePickUpTimesResult);
}
// call back function
function getStorePickUpTimesResult(r) {
	if(r.err.length > 0) {
		//alert(r.err);
	} else {
		the_selected_index = 0;
		times_dropdown_obj = document.getElementById(r.times_dropdown_field);
	
		removeAllDropDownOptions(times_dropdown_obj);
		addNewDropDownOption(times_dropdown_obj, "-- Select Time Required --", 0);
		
		for(k=0; k < r.times_array.length; k++){
			addNewDropDownOption(times_dropdown_obj, r.times_array[k].display_text, r.times_array[k].value_text);
			if(r.selected_time_value == r.times_array[k].display_text){
				the_selected_index = k + 1;
			}
		}
		
		if(r.times_array.length > 0){
			//times_dropdown_obj.selectedIndex = r.selected_time_index;
			times_dropdown_obj.selectedIndex = the_selected_index;
		} else if(r.times_array.length == 0){
			removeAllDropDownOptions(times_dropdown_obj);
			addNewDropDownOption(times_dropdown_obj, "No Time Available", 0);
		}
	}
}

/*************************************************************************/
/****************** Get Store GIS *******************************/
/*************************************************************************/
function getStoreGIS(store_id, gis_field, app_virtual_dir, live_domain) {
	//send data to CF
	//DWRUtil.useLoadingMessage();
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'ax_getStoreGIS', store_id, gis_field, app_virtual_dir, live_domain, getStoreGISResult);
}
// call back function
function getStoreGISResult(r) {
	if(r.err.length > 0) {
		//alert(r.err);
	} else {
		gis_obj = document.getElementById(r.gis_field);
		gis_obj.href = r.gmap_url;
	}
}

/*************************************************************************/
/****************** Get Nearest Stores    *******************************/
/*************************************************************************/
function getNearestStores(zipcode, store_dropdown_field, state_dropdown_field, store_distance_field, time_dropdown_field, gis_field, app_virtual_dir, live_domain) {
	//send data to CF
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'ax_getNearestStores', zipcode, store_dropdown_field, state_dropdown_field, store_distance_field, time_dropdown_field, gis_field, app_virtual_dir, live_domain, getNearestStoresResult);
}
// call back function
function getNearestStoresResult(r) {
	if(r.err.length > 0) {
		//store_distance_obj = document.getElementById(r.store_distance_field);
		//store_distance_obj.style.display = "none";
		 	
		//alert(r.err);
		store_distance_obj = document.getElementById(r.store_distance_field);
		//store_distance_obj.innerText = r.err;
		
		if(document.all){
				store_distance_obj.innerText = trim(r.err);
		}
		else {
			store_distance_obj.textContent = trim(r.err);
		}
			
		//Setting the box class for color effect on the display page
		store_distance_obj.setAttribute("class", "error");
		store_distance_obj.style.display = "block";
		
	} else {
		store_dropdown_obj = document.getElementById(r.store_dropdown_field);
		state_dropdown_obj = document.getElementById(r.state_dropdown_field);
		state_selected_index = 0;
		
		removeAllDropDownOptions(store_dropdown_obj);
		addNewDropDownOption(store_dropdown_obj, "-- Select Store for Pick-Up --", 0);
		
		removeAllDropDownOptions(state_dropdown_obj);
		addNewDropDownOption(state_dropdown_obj, "-- Select State for Pick-Up --", 0);
		
		for(k=0; k < r.store_array.length; k++){
			addNewDropDownOption(store_dropdown_obj, r.store_array[k].store_name, r.store_array[k].store_id);
		}
		
		for(s=0; s < r.state_array.length; s++){
			addNewDropDownOption(state_dropdown_obj, r.state_array[s].state_name, r.state_array[s].state_id);
			if(r.store_array[0].store_state == r.state_array[s].state_id)
			{
				state_selected_index = s + 1;
			}
		}
		addNewDropDownOption(state_dropdown_obj, r.store_array[0].store_state, r.store_array[0].store_state);
		
		if(r.store_array.length > 0){
			store_dropdown_obj.selectedIndex = 1;
			state_dropdown_obj.selectedIndex = state_selected_index;	
		} else if(r.store_array.length == 0){
			removeAllDropDownOptions(store_dropdown_obj);
			addNewDropDownOption(store_dropdown_obj, "No Nearest Stores", 0);
		}
		
		store_distance_obj = document.getElementById(r.store_distance_field);
		
		if(document.all){
			store_distance_obj.innerText = "Nearest pick-up store to your zipcode (" + r.zipcode + ") is " + r.store_array[0].store_name + " store.\nThe distance is approximately " + Math.round(r.store_array[0].store_distance*100)/100 + " miles.";
		}
		else {	
			store_distance_obj.textContent = "Nearest pick-up store to your zipcode (" + r.zipcode + ") is " + r.store_array[0].store_name + " store.\nThe distance is approximately " + Math.round(r.store_array[0].store_distance*100)/100 + " miles.";
		}
		store_distance_obj.setAttribute("class", "notice");
		store_distance_obj.style.display = "block";
			
		times_dropdown_obj = document.getElementById(r.time_dropdown_field);
	
		removeAllDropDownOptions(times_dropdown_obj);
		addNewDropDownOption(times_dropdown_obj, "-- Select Time Required --", 0);
		
		for(t=0; t < r.times_array.length; t++){
			addNewDropDownOption(times_dropdown_obj, r.times_array[t].display_text, r.times_array[t].value_text);
		}
		
		gis_obj = document.getElementById(r.gis_field);
		gis_obj.href = r.gmap_url;
		gis_obj.style.display = "inline";
		
	}
}

/*************************************************************************/
/****************** Get TIME MSG		*******************************/
/*************************************************************************/
function getTimeMessage(del_type, zipcode, store_id, date_req, selected_time_req ,msg_field, msg_button, app_virtual_dir, live_domain) {
	//send data to CF
	//DWRUtil.useLoadingMessage();
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'ax_getTimeMSG', del_type, zipcode, store_id, date_req, selected_time_req ,msg_field, msg_button, app_virtual_dir, live_domain, getTimeMSGResult);
}
// call back function
function getTimeMSGResult(r) {
	if(r.err.length > 0) {
		//alert(r.err);
	} else {
		msg_obj = document.getElementById(r.msg_field);
		msg_button_obj = document.getElementById(r.msg_button);
		
		if(document.all){
			msg_obj.innerText = trim(r.msg);
		}
		else {
			msg_obj.textContent = trim(r.msg);
		}
		if(trim(r.msg).length > 0) {
			msg_obj.style.display = "block";	
		} else {
			msg_obj.style.display = "none";
		}
		if(trim(r.msg_is_error) == "TRUE") {
			msg_button_obj.setAttribute("class", "small disabled button floatright");
			msg_button_obj.disabled = true;
		}
		else{
			msg_button_obj.setAttribute("class", "small green button floatright");
			msg_button_obj.disabled = false;
		}
	}
}

/*************************************************************************/
/****************** Check Valid for Delivery *******************************/
/*************************************************************************/
function getDeliveryCheck(zipcode, msg_field, app_virtual_dir, live_domain) {
	//send data to CF
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'ax_getDeliveryCheck', zipcode, msg_field, app_virtual_dir, live_domain, getDeliveryCheckResult);
}
// call back function
function getDeliveryCheckResult(r) {
	if(r.err.length > 0) {
		//alert(r.err);
	} else {
		msg_obj = document.getElementById(r.msg_field);
		
		if(r.del_msg != 'OK')  
		{	
		  //alert(r.del_msg);
		  	
			if(document.all){
				msg_obj.innerText = trim(r.del_msg);
			}
			else {
				msg_obj.textContent = trim(r.del_msg);
			}
			
			//Setting the box class for color effect on the display page
			
			if(r.msg_type == 'NOTICE'){
				msg_obj.setAttribute("class", "notice");
			}
			else if (r.msg_type == 'ERROR') {
				msg_obj.setAttribute("class", "error");
			} 
			msg_obj.style.display = "block";
		  
		} // msg is not OK
		else
		{
		  msg_obj.style.display = "none";
		}
	} // No r.err
} // End of function getDeliveryCheckResult
