function getDocHeight() {
	var D = document;
	return Math.max(Math.max(D.body.scrollHeight,
					D.documentElement.scrollHeight), Math.max(
					D.body.offsetHeight, D.documentElement.offsetHeight), Math
					.max(D.body.clientHeight, D.documentElement.clientHeight));
}
function showBusyPopup() {
	var winWidth = $(document).width();
	var winHeight = getDocHeight();
	if (winHeight < 1000) {
		winHeight = 1000;
	}
	var docHeight = $(window).height();
	var iconLeft = (winWidth - 32) / 2;
	var iconTop = (docHeight - 32) / 2;
	$(".overlay").css({
				'width' : winWidth,
				'height' : winHeight,
				'left'  : 0,
				'opacity' : 0.5
			});
	$(".busy_icon").css({
				'top' : 250,
				'left' : iconLeft
			});
	$(".overlay").show();
	$(".busy_icon").show();
	$(".busy_icon").center();
	// alert(winHeight);
	// alert("top : "+docHeight+" ..Left : "+iconLeft);

}
function hideBusyPopup() {
	$(".overlay").css({
		'width' : 0,
		'height' : 0,
		'left'  : -10000,
		'opacity' : 0
	});
	$(".overlay").hide();
	$(".busy_icon").hide();
}

function showMailFormatPopup() {
	var winWidth = $(document).width();
	var winHeight = getDocHeight();
	if (winHeight < 1000) {
		winHeight = 1000;
	}
	var docHeight = $(window).height();
	var iconLeft = (winWidth - 32) / 2;
	var iconTop = (docHeight - 32) / 2;
	$(".overlay").css({
				'width' : winWidth,
				'height' : winHeight,
				'left'  : 0,
				'opacity' : 0.5
			});
	/*$("#mailAfriendDiv").css({
				'position': 'absolute',
				'top' : 250,
				'left' : iconLeft
			});*/
	$(".overlay").show();
	$("#mailAfriendDiv").show();
	$("#mailAfriendDiv").center();
	if($("#showwishEnteryDiv:visible")){$("#showwishEnteryDiv").hide();}
	// alert(winHeight);
	// alert("top : "+docHeight+" ..Left : "+iconLeft);
}

function closeEmailPop(){
	$(".overlay").hide(); $("#mailAfriendDiv").hide();
}

function personalize() {

	var url = "personalizeAction";
	var params = "";
	$.get(url, params, function(data) {
				var sessionExpired = hasSessionExpired(data);
				if(sessionExpired){
					document.location.href="home";
					return false;
				}
				$("#personalize").html(data);

			});
	return false;
}
var map;
var startLatLong = "0,-0";
var endLatLong = "0,-0";
var storeAddress = "";
var userAddress = "";
function getmap() {
	// alert("inside getmap");

	userAddress = document.forms["storeInfo"].userAddress.value;
	storeAddress = document.forms["storeInfo"].storeAddress.value;
	storeAddress += ","+document.forms["storeInfo"].storeCity.value;
	storeAddress += ","+document.forms["storeInfo"].storeRegion.value;
	var storeLat = parseFloat(document.forms["storeInfo"].storeLatitude.value);
	var storeLong = parseFloat(document.forms["storeInfo"].storeLongitude.value);
	var userLat = parseFloat(document.forms["storeInfo"].userLatitude.value);
	var userLong = parseFloat(document.forms["storeInfo"].userLongitude.value);
	// alert("store:"+storeLat+","+storeLong+" User:"+userLat+","+userLong);
	map = new GMap2(document.getElementById("googleMap"));
	map.setCenter(new GLatLng(userLat, userLong), 9);
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());

	map.addOverlay(new GMarker(new GLatLng(storeLat, storeLong)));
	map.addOverlay(new GMarker(new GLatLng(userLat, userLong)));
	// alert("2.....store:"+storeLat+","+storeLong+"
	// User:"+userLat+","+userLong);
	// for the direction in google map latitude and longitude details are
	// required.
	startLatLong = userLat + "," + userLong;
	endLatLong = storeLat + "," + storeLong;
}

function showDirections() {
	// alert(startLatLong+" end address= "+endLatLong);
	//window.open("http://maps.google.com/maps?saddr=" + startLatLong + "&daddr="+ endLatLong);
	window.open("http://maps.google.com/maps?saddr=" + encodeURIComponent(userAddress) + "&daddr="+ encodeURIComponent(storeAddress));
	var url = "showRouteToStoreAnalyticAction";
	var params = "storeLatLong=" + endLatLong;
	$.post(url, params, function(data) {
				var sessionExpired = hasSessionExpired(data);
				if(sessionExpired){
					document.location.href="home";
					return false;
				}
				// alert("test analytics request has be send");
			});
	return true;
}

/*
 * function savePersonalizeCategory(){ //alert("savePersonalizeCategory:
 * "+document.getElementById("watches").checked); }
 * 
 * function cancelPersonalizeCategory(){ //alert("cancelPersonalizeCategory"); }
 */

function set_cookie(name, value, exp_y, exp_m, exp_d, path, domain, secure) {
	var cookie_string = name + "=" + escape(value);

	if (exp_y) {
		var expires = new Date(exp_y, exp_m, exp_d);
		cookie_string += "; expires=" + expires.toGMTString();
	}

	if (path)
		cookie_string += "; path=" + escape(path);

	if (domain)
		cookie_string += "; domain=" + escape(domain);

	if (secure)
		cookie_string += "; secure";

	document.cookie = cookie_string;
}

function delete_cookie(cookie_name) {
	var cookie_date = new Date(); // current date & time
	cookie_date.setTime(cookie_date.getTime() - 1);
	document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function get_cookie(cookie_name) {
	var results = document.cookie.match('(^|;) ?' + cookie_name
			+ '=([^;]*)(;|$)');

	if (results)
		return (unescape(results[2]));
	else
		return "NoCookie";
}

String.prototype.ReplaceAll = function(stringToFind, stringToReplace) {
	var temp = this;
	var index = temp.indexOf(stringToFind);
	while (index != -1) {
		temp = temp.replace(stringToFind, stringToReplace);
		index = temp.indexOf(stringToFind);
	}
	return temp;
}



/*
* Searches the Response Data for the session expiry text. 
* If text is found returns true.
*/
function hasSessionExpired(responseData){
	var index = responseData.indexOf("Your session has expired due to inactivity. Please Sign-in again.");
	if(index != -1 ){
		return true;
	} else {
		return false;
	}
}


function showFeedbackMailFormatPopup() {
	var winWidth = $(document).width();
	var winHeight = getDocHeight();
	if (winHeight < 1000) {
		winHeight = 1000;
	}
	var docHeight = $(window).height();
	var iconLeft = (winWidth - 32) / 2;
	var iconTop = (docHeight - 32) / 2;
	$(".overlay").css({
				'width' : winWidth,
				'height' : winHeight,
				'left'  : 0,
				'opacity' : 0.5
			});
	/*$("#mailAfriendDiv").css({
				'position': 'absolute',
				'top' : 250,
				'left' : iconLeft
			});*/
	$(".overlay").show();
	$("#sendFeedbackMailDiv").show();
	$("#sendFeedbackMailDiv").center();
	//if($("#showwishEnteryDiv:visible")){$("#showwishEnteryDiv").hide();}  LOOK AT THIS
	// alert(winHeight);
	// alert("top : "+docHeight+" ..Left : "+iconLeft);
}

function closeFeedbackEmailPopup() {
	$(".overlay").hide(); $("#sendFeedbackMailDiv").hide();
}

function onAjaxRequestError(errorMessage){
	$("#inlineMsgDiv").html(errorMessage);
	$("#inlineMsgDiv").show();
	hideBusyPopup();
}

function footerLink(url) {
    hideErrorMsgVisible();
	$("#inlineMsgDiv").html("");
	//var url="aboutUsAction";
	$.post(url, "", function(data){
		var sessionExpired = hasSessionExpired(data);
		if(sessionExpired){
			document.location.href="home";
			return false;
		} else {
			$("#registerSigninDiv").html(data);
			document.getElementById("detailsDiv").style.display = "none";
			document.getElementById("wishlistFeaturesDiv").style.display = "none";
			document.getElementById("registerSigninDiv").style.display = "block";
		}	
		hideBusyPopup();
	});		
}

