﻿
/*
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 == "none") {
		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;
	} else {
		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;
		}
	return false;
}




function SwapImage(img, caption) {
	ELEM("imgItem").src = eval(img + ".src");
	ELEM("spnImageCaption").innerHTML = caption;
}

function CalculateAmount() {
    var qty = 0;
    if (document.getElementById(ClientID.txtQuantity)) {
        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);
		}
	if (ELEM(ClientID.hidVarieties)) {
		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;
	}

    Amount = ((Amount.toString()).indexOf(".") == -1) ? Amount + '.00' : Amount;
    if (ELEM(ClientID.lblTotalAmount)) {
        ELEM(ClientID.lblTotalAmount).innerHTML = curr + FormatCommas(Amount);
    }
	
}


var SelectedItemDetailsTab = 1;
function SwapItemDetailsTabs(id) {
	if (SelectedItemDetailsTab == id) return;
	SwapAllItemDetailsTabs();
	switch (id) {
		case 1:	// Description Tab
			SelectedItemDetailsTab = 1;
			ELEM("ItemDetails_Description").style.display = "block";
			break;
		case 2:	// Reviews Tab
			SelectedItemDetailsTab = 2;
			ELEM("ItemDetails_Comments").style.display = "block";
			break;
		case 3:	// Delivery Charges Tab
			SelectedItemDetailsTab = 3;
			ELEM("ItemDetails_MoreFromVendor").style.display = "block";
			break;
		case 4:	// Vendor Reviews Tab
			SelectedItemDetailsTab = 4;
			ELEM("divVendorReviews").style.display = "block";
			break;
		case 5:	// Vendor Info
			SelectedItemDetailsTab = 5;
			ELEM("ItemDetails_VendorInfo").style.display = "block";
			break;
		}
}
function SwapAllItemDetailsTabs() {
	document.getElementById("ItemDetails_Description").style.display = "none";
	document.getElementById("ItemDetails_Comments").style.display = "none";
	document.getElementById("ItemDetails_MoreFromVendor").style.display = "none";
	document.getElementById("divVendorReviews").style.display = "none";	
	document.getElementById("ItemDetails_VendorInfo").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 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 DetailLoginOnEnter(event) {
	var key =  event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (key != 13) return true;
	if (!DetailValidateLogin()) return false;
	ELEM(DetailClientID.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.btnSearch).click();
	return false;
}
function ValidateLogin() {
	ELEM("divLoaderLogin").style.display = "block";
	//TODO
	
	if (false) ELEM("divLoaderLogin").style.display = "none";
	return true;
}
function DetailValidateLogin() {
	ELEM("divDetailLoaderLogin").style.display = "block";
	//TODO
	
	if (false) ELEM("divDetailLoaderLogin").style.display = "none";
	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();
}

function ShowMyAccount() {
    HideAllPopups();
    ELEM("divMyAccount").style.display = "block";
    if (ELEM("ifrmMyAccount")) ELEM("ifrmMyAccount").src = MyAccountURL + "?Tab=BazaarOrders&cTranx=RECENT";
}
function ClearMyAccountURL() {
    ELEM("ifrmMyAccount").src = "about:blank";
}
function HideMyAccount() {
    ELEM("divMyAccount").style.display = "none";
}

function ShowUserRegistration() {
	HideAllPopups();
	ELEM("divContent").style.display = "none";
	ELEM("divRegister").style.display = "block";
	//if (ELEM("ifrmRegister").src != UserRegistrationURL) 
	ELEM("ifrmRegister").src = UserRegistrationURL;
}
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(ev) {
	HideAllPopups();
	ELEM("divContactUs").style.display = "block";
	var ev = new EventVariable(ev);
	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";
}


function VendorRating_Show(event,itemId) {
	//ELEM("ldrRC").style.display = "inline";
	ELEM(ClientID.hidVRItemID).value=itemId;
	Panel(new EventVariable(event), "pnlVendorReview", "Vendor Review", -20, 20);
}


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;
}


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.");
	if (IsUserLoggedIn){
		ELEM(ThemeClientID.btnThemeBackgroundTasks).click();
	}
}


function ConfirmUser(){
	if(confirm('Are you sure you want to remove this item from Cart?'))
		return true;
	else
		return false;
}

function ShowContact(){
	ELEM(ClientID.phContactUs).style.display="block";
}

function ShowAddNewContact(event) {
	document.getElementById("ContactAddEdit_Loader").style.display = "none";
	HideAllLayers();
	document.getElementById("divContactAddEdit").style.display = "block";
	ResetContactForm();
	document.getElementById("ContactAddEdit_lblMessage").innerHTML = "Add a new contact address...";
	
}

function HideAllLayers() {
	document.getElementById("divContactInfo").style.display = "none";
	document.getElementById("divContactAddEdit").style.display = "none";

}
function ShowEditContact(event, contactID) {

	HideAllLayers();
	document.getElementById("divContactAddEdit").style.display = "block";
	document.getElementById("ContactAddEdit_Loader").style.display = "block";

	ResetContactForm();

	document.getElementById("ContactAddEdit_lblMessage").innerHTML = "Edit your contact";
	document.getElementById(ClientID.ContactAddEdit_txtQuickName).disabled = "disabled";
	document.getElementById(ClientID.ContactAddEdit_txtFullName).disabled = "disabled";
	document.getElementById(ClientID.ContactAddEdit_txtEmail).disabled = "disabled";
	document.getElementById(ClientID.ContactAddEdit_txtPhone).disabled = "disabled";
	document.getElementById(ClientID.ContactAddEdit_txtMobile).disabled = "disabled";
	document.getElementById(ClientID.ContactAddEdit_txtFax).disabled = "disabled";
	document.getElementById(ClientID.ContactAddEdit_txtAddressLine1).disabled = "disabled";
	document.getElementById(ClientID.ContactAddEdit_txtAddressLine2).disabled = "disabled";
	document.getElementById(ClientID.ContactAddEdit_ddlCountry).disabled = "disabled";
	document.getElementById(ClientID.ContactAddEdit_ddlCity).disabled = "disabled";
	document.getElementById(ClientID.ContactAddEdit_btnSave).disabled = "disabled";
	document.getElementById(ClientID.ContactAddEdit_btnCancel).disabled = "disabled";

	document.getElementById(ClientID.hidContactID).value = contactID;
	

	document.getElementById(ClientID.hidCommandName).value = "EDIT_CONTACT";
	document.getElementById(ClientID.hidCommandArgument).value = contactID;
	document.getElementById(ClientID.btnBackgroundTasks).click();
	
}

function EditContactData_OnLoad() {
	document.getElementById("ContactAddEdit_lblMessage").innerHTML = "Edit your contact";
	document.getElementById(ClientID.ContactAddEdit_btnSave).disabled = false;
	document.getElementById(ClientID.ContactAddEdit_btnCancel).disabled = false;
	
}

function ValidateRequiredTextbox(control){
	if(document.getElementById(control).value=="")
		return false;
	else
		return true;
}
function ValidateSaveRecipient() {
	document.getElementById('ContactAddEdit_Loader').style.display = "block";
	var isValid = true;
	if (!ValidateRequiredTextbox(ClientID.ContactAddEdit_txtQuickName)) isValid = false;
	if (!ValidateRequiredTextbox(ClientID.ContactAddEdit_txtFullName)) isValid = false;
	if (!ValidateRequiredTextbox(ClientID.ContactAddEdit_txtPhone)) isValid = false;
	if (!ValidateRequiredTextbox(ClientID.ContactAddEdit_txtAddressLine1)) isValid = false;
	if (document.getElementById(ClientID.ContactAddEdit_txtEmail).value != "")
	//	if (!ValidateEmailAddress(ClientID.ContactAddEdit_txtEmail)) isValid = false;
	if (!isValid) {
		document.getElementById("ContactAddEdit_Loader").style.display = "none";
		document.getElementById("ContactAddEdit_lblMessage").innerHTML = "There were errors in the data you provided.<br />Please revise your items again.";
		}

	return isValid;
}

function ContactSaveComplete(selectedContactID) {
	HideContacts();
	ShowAddress(event, selectedContactID);
}

function ShowAddress(event, id) {
	document.getElementById("divRecepientChangeLoader").style.display="block";
	if (id < 0) {
		ShowAddNewContact(event, true);
		document.getElementById("AddressDetails").style.display = "none";
	} else if (id == 0) {
		document.getElementById("AddressDetails").style.display = "none";
	} else {
//		document.getElementById("ddlRecipient").value = id;
		document.getElementById("AddressDetails").innerHTML = document.getElementById("AddressDetails"+id).innerHTML;
		document.getElementById("AddressDetails").style.display = "block";
	}
	
	if(document.getElementById("msgError")){document.getElementById("msgError").style.display="none";}
	
	performBgTask("GET_DELIVERY_CHARGE", "");

}

function ResetContactForm() {
	document.getElementById("ContactAddEdit_lblMessage").innerHTML = "";
	document.getElementById(ClientID.hidContactID).value = "0";
	document.getElementById(ClientID.ContactAddEdit_txtQuickName).value = "";
	document.getElementById(ClientID.ContactAddEdit_txtFullName).value = "";
	document.getElementById(ClientID.ContactAddEdit_txtEmail).value = "";
	document.getElementById(ClientID.ContactAddEdit_txtPhone).value = "";
	document.getElementById(ClientID.ContactAddEdit_txtMobile).value = "";
	document.getElementById(ClientID.ContactAddEdit_txtFax).value = "";
	document.getElementById(ClientID.ContactAddEdit_txtAddressLine1).value = "";
	document.getElementById(ClientID.ContactAddEdit_txtAddressLine2).value = "";
	document.getElementById(ClientID.hidCommandArgument).value = "";
	
}

function HideContacts() {
	HideAllLayers();
	document.getElementById("divContactInfo").style.display = "block";
}


function ShowMainProcessing() {
	document.getElementById("divContactInfo").style.display = "block";
}

function OpenReferenceURL(URL){
    window.open(URL,URL,"width=850,height=600,status=yes,resizable=yes,scrollbars=yes,menubar=yes,toolbar=no,left=100,top=50,screenX=100,screenY=50");
    return false;
}



function RecalculateCart(val){
	ShowCartCheckoutRefresh();
    ELEM(ClientID.btnRecalculate).click();
}
function ShowCartCheckoutRefresh() {
	ELEM("divCartReload").style.display = "block";
	ELEM(ClientID.ibtnNext).style.visibility = "hidden";
}
function ClearMyAccountURL() {
	//ELEM("ifrmMyAccount").src = "about:blank";
}





var SelectedSellItemsTab = 1;
function SwapSellItemsTabs(id) {
	if (SelectedSellItemsTab == id) return;
	SwapAllSellItemsTabs();
	switch (id) {
		case 1:	// My Items
			SelectedSellItemsTab = 1;
			ELEM("SellItems_MyItems").style.display = "block";
			break;
		case 2:	// Expired Items
			SelectedSellItemsTab = 2;
			ELEM("SellItems_ExpiredItems").style.display = "block";
			break;
		case 3:	// Sold Items
			SelectedSellItemsTab = 3;
			ELEM("SellItems_SoldItems").style.display = "block";
			break;
		case 4:	// Settings
			SelectedSellItemsTab = 4;
			ELEM("SellItems_UserSettings").style.display = "block";
			break;
		}
}
function SwapAllSellItemsTabs() {
	document.getElementById("SellItems_MyItems").style.display = "none";
	document.getElementById("SellItems_ExpiredItems").style.display = "none";
	document.getElementById("SellItems_SoldItems").style.display = "none";
	document.getElementById("SellItems_UserSettings").style.display="none";
}


function MerchantPop(merchantID) {
    if (!ELEM('divMerchantPop' + merchantID)) return;
    ELEM('divMerchantPop' + merchantID).style.display = 'block';
    ELEM('imgMerchantPop' + merchantID).src = svPath + '/App_Files/Merchants/M' + merchantID + '_logo.gif';
    ELEM('divMerchantPop' + merchantID).style.zIndex = 999;
}
function MerchantHidePop(merchantID) {
    if (!ELEM('divMerchantPop' + merchantID)) return;
    ELEM('divMerchantPop' + merchantID).style.display = 'none';
}

