var context = "";

function load(url) {
	window.location.href =url;
}

function searchDeals() {
	var obj = getHtmlControl("SearchText");
	var objCat = getHtmlControl("ParentCatId");
	if (objCat && objCat.value != '0'){
		load(context + "http://dealrocker.com/search-deals?text=" + obj.value + "&pcat=" + objCat.value);
	} else {
		load(context + "http://dealrocker.com/search-deals?text=" + obj.value);
	}
}

function searchOnEnter(inputField, e) {
	var keyCode;
	if (window.event) keyCode = window.event.keyCode;
	else if (e) keyCode = e.which;
	else return true;
	
	if (keyCode == 13) {
		searchDeals();
		return false;
	}
	else {
	   return true;
	}
}

function searchDealsCatBrand(catid, brandid) {
	load(context + "http://dealrocker.com/search-deals?cid=" + catid + "&bid=" + brandid);
}

function openDeal(dealId, storeId, isDirect) {
	if (isDirect) {
		openPopupWindowNamed(context + "http://dealrocker.com/app/ViewDeal?key=" + dealId, 'Store_'+storeId);
	} else {
		showMessageTimed("Please wait while we locate the selected bargain on store site...", 7000);
		storewin = openStoreSite(storeId);
		var dealPage = context + "http://dealrocker.com/app/ViewDeal?key=" + dealId;
		setTimeout("locatePage(storewin,'" + dealPage + "')", 5000);
	}
}

function openDealWithCoupon(dealId, couponId, storeId) {
	showMessageTimed("Please wait while we enable coupon savings for this deal...", 7000);
	storewin = openPopupWindowNamed(context + "http://dealrocker.com/app/ViewCoupon?key=" + couponId, 'Store_'+storeId);
	var dealPage = context + "http://dealrocker.com/app/ViewDeal?key=" + dealId;
	setTimeout("locatePage(storewin,'" + dealPage + "')", 5000);
}

function openStoreSite(storeId) {
	return openPopupWindowNamed(context + "http://dealrocker.com/app/StoreSite?store=" + storeId, 'Store_'+storeId);
}

function openSpecialDeal(dealId, storeId, type) {
	if  (type == 1) {
		openPopupWindowNamed(context + "http://dealrocker.com/app/ViewSpecialDeal?key=" + dealId, 'Store_'+storeId);
	} else {
		showMessageTimed("Please wait while we locate the selected bargain on store site...", 7000);
		storewin = openStoreSite(storeId);
		var dealPage = context + "http://dealrocker.com/app/ViewSpecialDeal?key=" + dealId;
		setTimeout("locatePage(storewin,'" + dealPage + "')", 5000);
	}
}

function openCoupon(couponId, storeId, type) {
	if  (isUndefined(type) || type == 1) {
		openPopupWindowNamed(context + "http://dealrocker.com/app/ViewCoupon?key=" + couponId, 'Store_'+storeId);
	} else {
		showMessageTimed("Please wait while we locate the selected coupon on store site...", 7000);
		storewin = openStoreSite(storeId);
		var page = context + "http://dealrocker.com/app/ViewCoupon?key=" + couponId;
		setTimeout("locatePage(storewin,'" + page + "')", 5000);
	}
}

function locatePage(win, url) {
	win.location=url;
}

function subscribe() {
	var obj = getHtmlControl("email");
	if (obj){
		//var url = context + "/app/Newsletter?e=" + obj.value;
		var url = context + "http://dealrocker.com/app/Newsletter" ;
		window.open(url, 'dr_subscribe','top=100, left=500, height=400,width=620,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');
	}
}

function viewDealDetail(dealId) {
	load(context + "http://dealrocker.com/app/DealDetail?key=" + dealId);
}

function switchDeal(obj) {
	this.innerHTML='';
	var el = document.getElementById(obj);
	if ( el.style.display != "none" ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}

function handleOver() {
	if (document.images) document.imgName.src=img_on.src;
}

function handleOut() {
	if (document.images) document.imgName.src=img_off.src;
}

function resizeimage(img, height, width){
	return;
}

function showDiv(objectID) {
	var divCtl = getHtmlControl(objectID);
	if(divCtl.style.display == "block"){
		divCtl.style.display = "none";
		divCtl.blur();
	}else{
		divCtl.style.display = "block";
		divCtl.focus();
	}
}

function showDealPopup(obj, index){
	var msgBox = getHtmlControl("messageBox" + index);

	if (isIE()) {
		msgBox.style.top = getPageOffsetTop(obj) + obj.offsetHeight;
		msgBox.style.left = getPageOffsetLeft(obj);
	} else {
		msgBox.style.top = (obj.offsetTop + obj.offsetHeight) + 'px';
		msgBox.style.left = (obj.offsetLeft) + 'px';
	}
	msgBox.style.display="block";
}

function hideDealPopup(index){
	var msgBox = getHtmlControl("messageBox" + index);
	msgBox.style.display = "none";
}

function showCouponPopup(obj, index){
	var msgBox = getHtmlControl("couponPopup" + index);

	if (isIE()) {
		msgBox.style.top = getPageOffsetTop(obj) + obj.offsetHeight;
		msgBox.style.left = getPageOffsetLeft(obj);
	} else {
		msgBox.style.top = (obj.offsetTop + obj.offsetHeight) + 'px';
		msgBox.style.left = (obj.offsetLeft) + 'px';
	}
	msgBox.style.display="block";
}

function hideCouponPopup(index){
	var msgBox = getHtmlControl("couponPopup" + index);
	msgBox.style.display = "none";
}

function hidePopup(){
	getHtmlControl("messageBox").style.display="none";
}

function getPageOffsetLeft(el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) {
		ol += el.offsetLeft;
	}
	return ol;
}

function getPageOffsetTop(el) {
	var ol=el.offsetTop;
	while ((el=el.offsetParent) != null) {
		ol += el.offsetTop;
	}
	return ol;
}

function showMessage(text){
	showMessageTimed(text, 4000);	// default delay of 4 sec
}

function showMessageTimed(text, delay){
	if (isUndefined(delay) || delay < 2000 || delay > 9000) {
		delay = 4000;
	}
	var msgBox = getHtmlControl("popupMessage");
	msgBox.innerHTML = text;
	if (isIE()) {
		msgBox.style.top = document.documentElement.scrollTop;
		msgBox.style.left = document.documentElement.scrollLeft+400;
	} else {
		msgBox.style.top = (window.pageYOffset) + 'px';
		msgBox.style.left = (window.pageXOffset+400) + 'px';
	}
	msgBox.style.display="block";
	setTimeout("hideMessage()", delay);
}

function hideMessage(){
	var msgBox = getHtmlControl("popupMessage");
	msgBox.style.display = "none";
}

function viewStoreList(prefix) {
	setControlValue("storePrefix", prefix);
	submitForm();
}

function applyCouponFilter(type) {
	setControlValue("userAction", "APPLY_FILTER");
	setControlValue("couponFilter", type);
	submitForm();
}

function editSearch() {
	alert("editSearch called...");
	setControlValue("userAction", "ADVANCED_SEARCH");
	alert(grtHtmlControl("userAction").value);
	submitForm();
}

function removeFavoriteStore(sid) {
	setControlValue("id", sid);
	setControlValue("userAction", "REMOVE_STORE");
	submitForm();
}

function removeFavoriteBrand(bid) {
	setControlValue("id", bid);
	setControlValue("userAction", "REMOVE_BRAND");
	submitForm();
}

function removeDeal(dealid) {
	setControlValue("id", dealid);
	setControlValue("userAction", "REMOVE_DEAL");
	submitForm();
}

function removeCoupon(couponid) {
	setControlValue("id", couponid);
	setControlValue("userAction", "REMOVE_COUPON");
	submitForm();
}

function removeAll() {
	setControlValue("userAction", "REMOVE_ALL");
	submitForm();
}

function removeAllDeals() {
	setControlValue("userAction", "REMOVE_DEAL_ALL");
	submitForm();
}

function removeAllCoupons() {
	setControlValue("userAction", "REMOVE_COUPON_ALL");
	submitForm();
}

function unsubscribe() {
	setControlValue("userAction", "NEWSLETTER_UNSUBSCRIBE");
}

function setPersonalInfoUpdated() {
	setControlValue("personalInfoUpdated", "true");
}

function onSelectFrequency() {
	var oFreq = getSelectedRadio("prefs.frequencyAsInt");
	if (oFreq) {
		var oWeek = getHtmlControl("dayOfWeek");
		var oMonth = getHtmlControl("dayOfMonth");
		if (oFreq.value == 1) {		// EnumConstants.Frequency.Weekly.intValue
			oWeek.disabled = false;
			oMonth.disabled = true;
		}
		else {
			oWeek.disabled = true;
			oMonth.disabled = false;
		}
	}
	else {
		oWeek.disabled = true;
		oMonth.disabled = true;
	}
}

function submitComments() {
	setControlValue("userAction", "FEEDBACK_SUBMIT");
}

function validateCommentsLength() {
	var oComments = getHtmlControl("feedback.comments");
	if (oComments.value.length > 1000) {
		alert("You have reached the maximum allowed length for Comments."
			+ "\nPlease limit your Comments to 1000 chars."
			+ "\n\nThank you!");
		oComments.value = oComments.value.substring(0, 1000);
	}
	var oCharsLeft = getHtmlControl("charsLeft");
	oCharsLeft.innerHTML = 1000 - oComments.value.length; 
}

function viewPage(pageNo) {
	setControlValue("requestedPageNo", pageNo);
	setControlValue("userAction", "VIEW_PAGE_NO");
	submitForm();
}

function changePageSize() {
	setControlValue("userAction", "CHANGE_PAGE_SIZE");
	submitForm();
}

function changeSortOption() {
	setControlValue("userAction", "CHANGE_SORT_OPTION");
	submitForm();
}

function refreshPage() {
	showDiv("RefreshMessage");
	showProgress();
	setControlValue("userAction", "REFRESH_PAGE");
	submitForm();
}

function showProgress() {
	var oProgress = getHtmlControl("progress");
	var dots = oProgress.innerHTML;
	if (dots.length < 10) {
		dots += ".";
	} else {
		dots = ".";
	}
	oProgress.innerHTML = dots;
	window.setTimeout('showProgress()', 400);
}

function viewBrandList(prefix) {
	setControlValue("brandPrefix", prefix);
	submitForm();
}

// JavaScript Document