﻿function ELEM(id) {
	return document.getElementById(id);
}

isIE = document.all ? true : false;

function Coordinates(x, y) {
	this.x = x;
	this.y = y;
}

function EventVariable(event, variables) {
	if (isIE)
		this.xy = new Coordinates(window.event.clientX + document.body.scrollLeft, window.event.clientY + document.body.scrollTop);
	else
		this.xy = new Coordinates(event.pageX, event.pageY);
	this.CallerObject = isIE ? window.event.srcElement : event.target;
	this.KeyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	this.MouseButton = isIE ? event.button : event.which;
	this.ControlKey = (event.altKey ? 1 : 0) + (event.ctrlKey ? 2 : 0) + (event.shiftKey ? 4 : 0);
	this.Type = event.type;
	this.Variables = variables;
}



function FormatCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

// --------------------------------------------------------------------------------------------------------------
// script for left categories menu

img_ico_Plus = new Image();
img_ico_Minus = new Image();
img_ico_FolderOn = new Image();
img_ico_FolderOff = new Image();

img_ico_Plus.src = svPath+"/App_Files/img/plus.gif";
img_ico_Minus.src = svPath+"/App_Files/img/minus.gif";
img_ico_FolderOn.src = svPath+"/App_Files/img/FolderOn.gif";
img_ico_FolderOff.src = svPath+"/App_Files/img/FolderOff.gif";

function menu_alter(id1, ico_PlusMinus, ico_Folder) {
	if (!ELEM(id1)) return;

	if (ELEM(id1).style.display == "block") {
		ELEM(id1).style.display = "none";
		document[ico_PlusMinus].src = img_ico_Plus.src;
		if (document[ico_Folder]) document[ico_Folder].src = img_ico_FolderOff.src;
	} else {
		ELEM(id1).style.display = "block";
		document[ico_PlusMinus].src = img_ico_Minus.src;
		if (document[ico_Folder]) document[ico_Folder].src = img_ico_FolderOn.src;
		}
	return false;
}




function SwapImage(img, caption) {
	ELEM("imgItem").src = eval(img + ".src");
	ELEM("spnImageCaption").innerHTML = caption;
}

function ShowItemLargeImage() {

}


function CalculateAmount() {
	var qty = ELEM(ClientID.txtQuantity).value;
	if (isNaN(qty)) {
		ELEM(ClientID.lblTotalAmount).innerHTML = "Invalid Quantity Value";
		return;
		}
	if (qty != parseInt(qty)) {
		ELEM(ClientID.lblTotalAmount).innerHTML = "Invalid Quantity Value";
		return;
		}

	var temp_amount = 0;
	var temp_qty = qty;

	var PriceDifference = 0;
	var vid = "";
	for (i = 0; i != GetVGroupsCount(); i++) {
		vid += ELEM("ddlVarieties" + i).value + ",";
		PriceDifference += GetPriceDiff(ELEM("ddlVarieties" + i).value);
		}
	ELEM(ClientID.hidVarieties).value = vid;
	currRate = GetQtyRate(GetNearestQty(qty)) + PriceDifference;
	var Amount = Math.round(currRate * qty * 100) / 100;

	var curr;
	switch (UserCurrencyCode) {
		case "NPR" : curr = "Rs."; break;
		case "USD" : curr = "$"; break;
		case "AUD" : curr = "AU$"; break;
	}

	ELEM(ClientID.lblTotalAmount).innerHTML = curr + FormatCommas(Amount);
	
}

function SwapItemDetailsTabs(id) {
	SwapAllItemDetailsTabs();
	switch (id) {
		case 1:	// Description Tab
			ELEM("ItemDetails_Description").style.display = "block";
			break;
		case 2:	// Reviews Tab
			ELEM("ItemDetails_Reviews").style.display = "block";
			break;

		case 3: // Delivery Charges Tab
			ELEM("ItemDetails_DelCharges").style.display = "block";
			break;
			
		case 4:	// Related Items Tab
			ELEM("ItemDetails_RelatedItems").style.display = "block";
			break;
        }
}
function SwapAllItemDetailsTabs() {
	ELEM("ItemDetails_Description").style.display = "none";
	ELEM("ItemDetails_Reviews").style.display = "none";
	ELEM("ItemDetails_DelCharges").style.display = "none";
	ELEM("ItemDetails_RelatedItems").style.display = "none";
}

function CheckDeliveryPrice() {
	var deliveryMethodID = parseInt(ELEM(ClientID.ddlDeliveryMethod).value);
	var cityID = parseInt(ELEM(ClientID.ddlCities).value);
	
	if (cityID == 0) {
		alert("Please select a city.");
		return false;
	}
	if (deliveryMethodID == 3 && ShippingEnabledCities.indexOf("," + cityID + ",") < 0) {
		ELEM(ClientID.lblDeliveryPrice).innerHTML = "Shipping is not available for this city.";
		ELEM(ClientID.lblDeliveryPrice).style.display = "block";
		ELEM("ldrDeliveryPrices").style.display = "none";
		return false;
	} else {
		ELEM(ClientID.lblDeliveryPrice).style.display = "none";
		ELEM("ldrDeliveryPrices").style.display = "block";
		return true;
	}
}

function DisableEnterKeyOnPage(event) {
	var key =  event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (key == 13) return false; else return true;
}

function LoginOnEnter(event) {
	var key =  event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (key != 13) return true;
	if (!ValidateLogin()) return false;
	ELEM(ThemeClientID.btnLogin).click();
	return false;
}

function SearchOnEnter(event) {
	var key =  event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (key != 13) return true;
	ELEM(ThemeClientID.ibtnSearch).click();
	return false;
}

function ValidateLogin() {
	ELEM("divLoaderLogin").style.display = "none";
	var isValid = true;
	
	if (ELEM(ThemeClientID.txtLoginUserName).value == "" || ELEM(ThemeClientID.txtLoginPassword).value == "") isValid = false;
	
	if (!isValid) {
		alert("Please provide your login and password.");
		return false;
	}
	
	ELEM("divLoaderLogin").style.display = "block";
	return true;
}

function Logout() {
	ELEM("divLoaderLogout").style.display = "block";
}

function CloseRecentViewsBox() {
	var box = ELEM("divRecentViews");
	var w = parseInt(box.style.width);
	if (w > 0) {
		box.style.width = (w - 40) + "px";
		box.style.right = 0;
		box.style.overflowX = "hidden";
		box.style.overflowY = "hidden";
		setTimeout(CloseRecentViewsBox,2);
	} else {
		box.style.display = "none";
	}
}
function ShowRecentViewsBox() {
	HideAllPopups();
	var box = ELEM("divRecentViews");
	var w = parseInt(box.style.width);
	if (w < 680) {
		box.style.overflowX = "hidden";
		box.style.overflowY = "hidden";
		box.style.display = "block";
		box.style.width = (w + 40) + "px";
		box.style.right = 0;
		setTimeout(ShowRecentViewsBox,2);
	} else {
		box.style.display = "block";
		box.style.width = "680px";
		box.style.overflowX = "scroll";
		box.style.overflowY = "auto";
	}
}

function HideAllPopups() {
	//HideMerchantsAll();
	HideMyAccount();
	//HideUserRegistration();
	HideForgotPassword();
	HideContactUs();
	if (ELEM("divRecentViews")) ELEM("divRecentViews").style.display = "none";
}

function ShowMerchantsAll() {
	HideAllPopups();
	ELEM("divMerchantsAll").style.display = "block";
	ELEM("ifrmMerchantsAll").style.display = "block";
}
function HideMerchantsAll() {
	ELEM("divMerchantsAll").style.display = "none";
	ELEM("ifrmMerchantsAll").style.display = "none";
}
function ShowMyAccount() {
	HideAllPopups();
	ELEM("divMyAccount").style.display = "block";
	if (ELEM("ifrmMyAccount")) ELEM("ifrmMyAccount").src = svPath + "/MyAccount.aspx?Random=" + Math.random();
}
function ClearMyAccountURL() {
	ELEM("ifrmMyAccount").src = "about:blank";
}
function HideMyAccount() {
	ELEM("divMyAccount").style.display = "none";
}
function ShowUserRegistration(returnTo) {
	HideAllPopups();
	ELEM("divContent").style.display = "none";
	ELEM("divRegister").style.display = "block";
	//if (ELEM("ifrmRegister").src != UserRegistrationURL) 
	if (ELEM("ifrmRegister").src == 'about:blank') ELEM("ifrmRegister").src = UserRegistrationURL + (returnTo ? "?RETURN=" + returnTo : "");
}
function HideUserRegistration() {
	if (ELEM("divRegister")) ELEM("divRegister").style.display = "none";
}
function CancelUserRegistration() {
	ELEM("divContent").style.display = "block";
	ELEM("divRegister").style.display = "none";
}
function ShowForgotPassword() {
	HideAllPopups();
	ELEM("divForgotPassword").style.display = "block";
	if (ELEM("ifrmForgotPassword").src != ForgotPasswordURL) ELEM("ifrmForgotPassword").src = ForgotPasswordURL;
}
function HideForgotPassword() {
	if (ELEM("divForgotPassword")) ELEM("divForgotPassword").style.display = "none";
}
function ShowContactUs() {
	HideAllPopups();
	ELEM("divContactUs").style.display = "block";
	var ev = new EventVariable(EventVariable);
	ELEM("divContactUs").style.top = (ev.xy.y - 350) + "px";
	ELEM("divContactUs").style.left = (ev.xy.x - 300) + "px";	
}
function HideContactUs() {
	ELEM("divContactUs").style.display = "none";
}



//Item Rating

function Ratings_Change(val){
	ELEM('starTable').style.backgroundImage = "url(../../../../../../App_Files/img/rating"+(val*10)+".gif)";
	ELEM('spnRatingValue').innerHTML = val;
}
function Ratings_Set(val){
	ELEM(HiddenRatingsID).value = val;
}
function Ratings_Reset(elem){
	var val = ELEM(HiddenRatingsID).value;
	if(val == "")
		elem.style.backgroundImage="url(App_Files/img/rating0.gif)";
	else
		elem.style.backgroundImage="url(App_Files/img/rating"+(val*10)+".gif)";
	ELEM('spnRatingValue').innerHTML = val;
}

//Merchant Rating

function MerchantRatings_Change(val){
	ELEM('merchantStarTable').style.backgroundImage = "url(App_Files/img/rating"+(val*10)+".gif)";
	ELEM('spnMerchantRatingValue').innerHTML = val;
}
function MerchantRatings_Set(val){
	ELEM(HiddenMerchantRatingsID).value = val;
}
function MerchantRatings_Reset(elem){
	var val = ELEM(HiddenMerchantRatingsID).value;
	if(val == "")
		elem.style.backgroundImage="url(App_Files/img/rating0.gif)";
	else
		elem.style.backgroundImage="url(App_Files/img/rating"+(val*10)+".gif)";
	ELEM('spnMerchantRatingValue').innerHTML = val;
}

//Image Preloading

imgRatingStar00 = new Image();
imgRatingStar01 = new Image();
imgRatingStar02 = new Image();
imgRatingStar03 = new Image();
imgRatingStar04 = new Image();
imgRatingStar05 = new Image();
imgRatingStar06 = new Image();
imgRatingStar07 = new Image();
imgRatingStar08 = new Image();
imgRatingStar09 = new Image();
imgRatingStar10 = new Image();
imgRatingStar00.src = "App_Files/img/rating0.gif";
imgRatingStar01.src = "App_Files/img/rating5.gif";
imgRatingStar02.src = "App_Files/img/rating10.gif";
imgRatingStar03.src = "App_Files/img/rating15.gif";
imgRatingStar04.src = "App_Files/img/rating20.gif";
imgRatingStar05.src = "App_Files/img/rating25.gif";
imgRatingStar06.src = "App_Files/img/rating30.gif";
imgRatingStar07.src = "App_Files/img/rating35.gif";
imgRatingStar08.src = "App_Files/img/rating40.gif";
imgRatingStar09.src = "App_Files/img/rating45.gif";
imgRatingStar10.src = "App_Files/img/rating50.gif";



function performBgTask(cmdName, cmdArgs){
	ELEM(ClientID.hidCommandName).value = cmdName;
	ELEM(ClientID.hidCommandArgument).value = cmdArgs;
	ELEM(ClientID.btnBackgroundTasks).click();
	
}

function ShowCartLoader() {
	if (ELEM("divCartLoader")) ELEM("divCartLoader").style.display = "block";
}

function ShowCartCallout() {
	if (!ELEM("divCartCallout")) return;
	ELEM("divCartCallout").style.display = "block";
	setTimeout(function () { location.href='#top'; }, 500);
	setTimeout(function () { ELEM("divCartCallout").style.display = "none"; }, 7777);
}

function CheckUserCheckout() {
	if (!IsUserLoggedIn) alert("You need to login in-order to checkout.\n\nIf you are a new user then please register.");
	return IsUserLoggedIn; 
}


function ConfirmUser(){
	if(confirm('Are you sure you want to remove this cart item?')) {
		ShowCartCheckoutRefresh();
		return true;
	} else
		return false;
}

function ShowContact(){
	ELEM(ClientID.phContactUs).style.display="block";
}

function RecalculateCart(val){
	ShowCartCheckoutRefresh();
    ELEM(ClientID.btnRecalculate).click();
}
function ShowCartCheckoutRefresh() {
	ELEM("divCartReload").style.display = "block";
	ELEM(ClientID.ibtnNext).style.visibility = "hidden";
}

function space_dash(val){return val.replace(/ /g,"-")}
