// JScript File
function $get(id)
{
	return document.getElementById(id);
}
function hideElement(e)
{
	e.style.display = "none";
}
function displayElement(e)
{
	e.style.display = "";
}
function toggleDisplay(e)
{
	if (e.style.display == "none")
		displayElement(e);
	else
		hideElement(e);
}

function setHtml(e, html)
{
	e.innerHTML = html;
}

function openMenu(id, div)
{
    menu = document.getElementById(id); 
	  ShowMenu(menu, div);
}


function ShowHim(div)
{
    menu = document.getElementById("menuShopHim"); 
	  ShowMenu(menu, div);
}

function ShowHers(div)
{
    menu = document.getElementById("menuShopHer");

	ShowMenu(menu, div);
}

function ShowOccasions(div)
{
    menu = document.getElementById("menuShopOccasion");

	ShowMenu(menu, div);
}

function ShowBday(div)
{
    menu = document.getElementById("menuShopBday");

	ShowMenu(menu, div);
}

function ShowBaby(div)
{
    menu = document.getElementById("menuShopBaby");

	ShowMenu(menu, div);
}

function ShowJewl(div)
{
    menu = document.getElementById("menuShopJewl");

	ShowMenu(menu, div);
}

function ShowSpcl(div)
{
    menu = document.getElementById("menuShopSpcl");

	ShowMenu(menu, div);
}

function ShowMenu(menu, div)
{
	if (open_menu != null)
		open_menu.style.display = "none";
    
    positionMenu(menu, div, -8, 17);
    
    // hide all SELECT objects
    sels = document.getElementsByTagName("select");
    for (i=0; i<sels.length; i++)
		sels[i].style.visibility = "hidden";
		
    
    if (menu.filters && menu.filters.length > 0)
    {
        menu.filters[0].Apply();
        menu.style.display = "";
        menu.filters[0].Play();
    }
    else
        menu.style.display = "";
    
    open_menu = menu;
    menu_to_hide = null;
}

function getWebURL(webURL, storeID, deptID, productID)
{
	if (productID > 0)
		return webURL + "Product.aspx?storeID=" + storeID + "&categoryID=" + deptID + "&productID=" + productID;
	else if (deptID > 0)
		return webURL + "Category.aspx?storeID=" + storeID + "&categoryID=" + deptID;
	else if (storeID > 0)
		return webURL + "Store.aspx?storeID=" + storeID;
	else
		return webURL;
}

function timer_hide_menu()
{
	if (menu_to_hide != null) 
	{
		menu_to_hide.style.display = 'none';
		menu_to_hide = null;
		
		// hide all SELECT objects
		sels = document.getElementsByTagName("select");
		for (i=0; i<sels.length; i++)
			sels[i].style.visibility = "visible";
			
		
	}
}

open_menu = null;
menu_to_hide = null;

function menu_over(td)
{
	//td.style.backgroundColor = '#DDDDDD';
	td.className = "menu-item-over";
}
function menu_out(td)
{
	//td.style.backgroundColor = '';
	td.className = "menu-item";
}

function keep_menu(menu)
{
	menu_to_hide = null;
	open_menu = menu;
}
function keep_menu_out()
{
	menu_to_hide = open_menu;
	setTimeout(timer_hide_menu, 500);
}

function findPosX(obj)
{
	var curLeft = 0;
	if (obj.offsetParent)
	{
		do 
		{
			curLeft += obj.offsetLeft;
		} while (obj = obj.offsetParent);
	}
	else if (obj.x)
		curLeft += obj.x;
	
	return curLeft;	
}
function findPosY(obj)
{
	var curTop = 0;
	if (obj.offsetParent)
	{
		do 
		{
			curTop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	else if (obj.y)
		curTop += obj.y;
	
	return curTop;	
}
function positionMenu(menu, div, offX, offY)
{
	posY = findPosY(div);
	posX = findPosX(div);

	posY += offY;
	posX += offX;

    menu.style.top = posY + "px";
    menu.style.left = posX + "px";
	//alert(menu.style.top);    
}

function changePage(page)
{
	window.open(page, "_self");
}

function showImageList(listid, productid, persid, width, height)
{
    var params = "scrollbars=yes,width=" + width + ",height=" + height;
    window.open("ImageList.aspx?imagelistid=" + listid + "&productid=" + productid + "&persid=" + persid, "_blank", params);
}

function displayProductMedia(filename, w, h)
{
	if (w > 0 && h > 0)
		window.open(filename, "_blank", "width=" + w + ",height=" + h);
	else
		window.open(filename, "_blank");

}

function fillText(sel, arrName)
{
    if (sel.selectedIndex == 0)
        return;
        
    arr = eval(arrName);
    text = arr[sel.selectedIndex-1];
        
    var e = document.getElementById("aspnetForm").elements;
    var found = false;
    for (i=0; i<e.length; i++)
        if (e[i] == sel)
                found = true;
        else if (e[i].type == "textarea" && found)
		{
             e[i].value = text;
             return;
        }
            
}

function ControlValue(field)
{
	if (!field)
		// no pers
		return null;

	/*
	else if ((field.length && !field.type) || field.type == "radio") // radio button
	{
		for (j=0; j<field.length; j++)
		{
			if (field[j].checked)
			{
				ret = field[j].value;

				return ret;
			}
		}
	}
	*/
	else if (field.type == 'radio')
	    return field.checked ? field.value : null;

	else if (field.type == 'select-one') {

		var opt = field.options[field.selectedIndex];
	
		return opt.value;
		
	}
	else if (field.type == 'text') {
		return field.value;
	}
	else if (field.type == 'textarea')
	{
		// need to wrap at maximum size
		var output = "";
		var lines = field.value.split("\n");
		var mx = field.cols;
		
		for (var lineno=0;lineno<lines.length; lineno++)
		{
			var line = lines[lineno];
			if (line.length > mx)
			{
				var words = line.split(" ");
				var count = 0;
				for (var j=0; j<words.length; j++)
				{

					var word = words[j];
					if (count + word.length + 1 <= mx)	// word will fit on line
					{
						output = output + " " + word;
						count += word.length + 1;
					}
					else if (word.length > mx) // word byitself is too big
					{
						output += "\n" + word + "\n";
						count = 0;
					}
					else // word does not fit on this line
					{
						output += "\n" + word;
						count = word.length;
					}
				}
				output += "\n";
			}
			else
				output += line + "\n";
		}		
		return output;
	}
	else
	    return "";
}

function ShowPreview()
{
    if (Page_ClientValidate("") == false)
        return;

    var form = document.getElementById("aspnetForm");
    var e = form.elements;
    var q = "ProductPreviewDialog.aspx?productID=" + previewProductID + "&itemID=" + previewItemID;
    for (i=0; i<e.length; i++)
    {
        var n = e[i].name;
        var ix = n.indexOf("pers");
        if (ix >= 0)
        {
            var vname = n.substr(ix+5);//+5 because control name is $pers$pers3343
            var vvalue = ControlValue(e[i]);
            if (vvalue != null) 
            {
            	vvalue = vvalue.replace(/\+/g, "__PLUS__");
                q += "&" + vname + "=" + escape(vvalue);
            }
        }
    }
    //var params = "width=" + (previewWidth+20) + ",height=" + (previewHeight+100);
    //window.open(q, "_blank", params);

   
    ShowPreviewWithUrl(q);
}

function ShowPreviewWithUrl(q)
{
    
    var pManager = GetRadWindowManager();
    var pWindow = pManager.GetWindowByName("PreviewWindow");
    var h = previewHeight + 50;
    var w = previewWidth + 195;
    var maxH = getClientHeight();
    
	h = h > maxH ? maxH : h;
    
	pWindow.Center();
	pWindow.SetSize(w, h);
	pWindow.SetUrl(q);
	pWindow.ClientCallBackFunction = PreviewCallBackFunction;
	pWindow.Show();

}

function PreviewCallBackFunction(radWindow, returnValue)
{
	if (returnValue) 
	{
		if (returnValue == "ValidateAndSubmit")
		{
			Page_ClientValidate();
			if (Page_IsValid)
				__doPostBack(addToCartButtonID, '');
		}
		else if (returnValue == "Submit")
		{
			__doPostBack(addToCartButtonID, '');
		}
	}
}

function ShowProductImage(src)
{
	if (document.images)
	{
		var theImg = document.images["productImage"];
		if (theImg.filters && theImg.filters.length > 0)
		{
			theImg.filters[0].Apply();
			theImg.src = src;		
			theImg.filters[0].Play();			
		}
		else
			theImg.src = src;		
	}
}

function BoxDefault(box)
{
	if (box.value==box.defaultValue)
		box.value = "";
}

function CheckSearchBox(form)
{
	
	if (form.searchString.value.length == 0 || form.searchString.value == form.searchString.defaultValue)
	{
		alert("Please specify your search keywords.");
		return false;
	}
	else
		return true; 
}

divMore = null;
divExpand = null;
divExpandDivs = null;
divExpandIndex = 0;

function ShowMore(index)
{
	/*
	if (divMore != null)
	{
		divMore.style.display = '';
		divExpand.style.display = 'none';
	}
	*/

	divMore = document.getElementById("navMore" + index);
	divExpand = document.getElementById("navMoreExpanded" + index);
	
	divMore.style.display = 'none';
	divExpand.style.display = '';
	
	divExpandDivs = divExpand.children;
	divExpandIndex = 0;

	for (i=0; i<divExpandDivs.length; i++)
		divExpandDivs[i].style.display = "none";
	
	window.setTimeout(expandNavbar, 30);
}

function expandNavbar()
{
	divExpandDivs[divExpandIndex].style.display = "";
	divExpandIndex++;	
	
	if (divExpandIndex < divExpandDivs.length)
	{
		window.setTimeout(expandNavbar, 30);
	}
}


function PreviewAlsoAvailable(img)
{
	var dest = $get("imgAlsoAvailable");
	
	if (dest)
	{
		if (img.length == 0)
			img = "images/blank.gif";

		if (dest.filters && dest.filters.length > 0)
		{
			dest.filters[0].Apply();
			dest.src = img;
			dest.filters[0].Play();
		}
		else
		{
			dest.src = img;
		}
	}
}

function openLargeImage(productID, width, height)
{
	window.open('ExtraLargeImage.aspx?productID=' + productID, '_blank', 'width='+width+',height='+height);
}

function ValidateImageUpload(source, args)
{

	var mailOption = source.optionMail ? document.getElementById(source.optionMail) : null;
	var uploadOption = source.optionUpload ? document.getElementById(source.optionUpload) : null;
	var keepOption = source.optionKeep ? document.getElementById(source.optionKeep) : null;
	var libraryOption = source.optionLibrary ? document.getElementById(source.optionLibrary) : null;
	var file = document.getElementById(source.fileUpload);
	
	if (mailOption && mailOption.checked)
		args.IsValid = true;
	else if (keepOption && keepOption.checked)
		args.IsValid = true;
	else if (libraryOption && libraryOption.checked)
		args.IsValid = true;
	else if (uploadOption.checked)
	{
		args.IsValid = file.value.length > 0;
	}
	else
		args.IsValid = false;
	
}


function ValidateCheckbox(source, args)
{
    var box = document.getElementById(source.checkboxID); 
    args.IsValid = box.checked;
}

function ValidateKitDropdown(source, args)
{
	//alert(args.Value);
	var val = ValidatorGetValue(source.controlToValidate);
}

function ValidatePersTypeItem(source, args)
{
	var optionYes = $get(source.buttonsID + "_0");
	var optionNo = $get(source.buttonsID + "_1");
	
	
	args.IsValid = optionYes.checked || optionNo.checked;
}

function ValidateDate(source, args)
{
	//alert(source.required);
	var id = source.controlToValidate;
	var m = ValidatorGetValue(id + "_Month");
	var d = ValidatorGetValue(id + "_Day");
	var y = ValidatorGetValue(id + "_Year");
	
	//alert(m + " " + d + " " + y);
	if (m.length == 0 && d.length == 0 && y.length == 0)
	{
		if (source.required == "t")
		{
			source.errormessage = source.orgErrorMessage;
			args.IsValid = false;
		}
		else
			args.IsValid = true;
		return;
	}
	
	m = ValidatorConvert(m, "Integer", null);
	d = ValidatorConvert(d, "Integer", null);
	y = ValidatorConvert(y, "Integer", null);
	
	if (m == null || d == null || y == null)
	{
		args.IsValid = false;
		return;
	}
	
	date = new Date(y, m-1, d);
	if (isNaN(date))
	{
		args.IsValid = false;
		source.errormessage = "Invalid Date";
		return;
	}
	
	earliestDate = new Date(ValidatorConvert(source.earliestDate, "Date", {dateorder:"ymd"}));
	if (date < earliestDate)
	{
		source.errormessage = "Selected date is before earliest allowed date";
		args.IsValid = false;
		return;
	}
	
	var wd = date.getDay();
	
	if (wd == 0 && source.noallow0 == "t")
	{
		source.errormessage = "Sunday is not allowed";
		args.IsValid = false;
		return;
	}
	else if (wd == 1 && source.noallow1 == "t")
	{
		source.errormessage = "Monday is not allowed";
		args.IsValid = false;
		return;		
	}
	else if (wd == 2 && source.noallow2 == "t")
	{
		source.errormessage = "Tuesday is not allowed";
		args.IsValid = false;
		return;		
	}
	else if (wd == 3 && source.noallow3 == "t")
	{
		source.errormessage = "Wednesday is not allowed";
		args.IsValid = false;
		return;		
	}
	else if (wd == 4 && source.noallow4 == "t")
	{
		source.errormessage = "Thursday is not allowed";
		args.IsValid = false;
		return;		
	}
	else if (wd == 5 && source.noallow5 == "t")
	{
		source.errormessage = "Friday is not allowed";
		args.IsValid = false;
		return;		
	}
	else if (wd == 6 && source.noallow6 == "t")
	{
		source.errormessage = "Saturday is not allowed";
		args.IsValid = false;
		return;		
	}
	
	/*
	if (source.allowWeekends == "f")
	{
		var wd = date.getDay();
		
		if (wd == 0 || wd == 6)
		{
			source.errormessage = "Weekends are not allowed";
			args.IsValid = false;
		}
	}
	*/
	
	args.IsValid = true;
	
}

function ShowExpressShippingInfo()
{
	window.open("ExpressShipping.aspx", "_blank", "width=550,height=500,scrollbars=yes");
}

function noIntShip()
{
	window.open("InternationalItem.aspx", "_blank", "width=550,height=500,scrollbars=yes");
}

function staticPageWindow(pageID)
{
	window.open("StaticPageWindow.aspx?pageid=" + pageID, "_blank", "width=550,height=500,scrollbars=yes");
}

function writeUploadMessage()
{
	document.writeln("<div style='position:absolute;display:none;width:400px;height:180px;border:1px solid black;padding:2px;background-color:#FFFFCC;font-size:13px;color:green;font-weight:bold;' id='divUploadImage'>");
	document.writeln("<div style='text-align:center'>Image Upload in Progress:<hr noshade size=1 width=90%></div>");
	document.writeln("<div style='margin:10px'>Please wait while your image is uploaded.<br>Depending on your internet connection and image size, this might take couple minutes.<br>This message will go away once your image is uploaded.</div>");
	//document.writeln("<div style='text-align:center'><img src='images/progress.gif' border=0></div>");	
	document.writeln("</div>");
}

function showProgressMessage(btn, divID, ox, oy)
{
	var div = document.getElementById(divID);

	if (!div)
		return;
		
	div.style.left = findPosX(btn) + ox;
	div.style.top = findPosY(btn) + oy;
	div.style.display = "";	
}

function writePlaceOrderMessage()
{
	document.writeln("<div style='position:absolute;display:none;width:400px;height:180px;border:1px solid black;padding:2px;background-color:#FFFFCC;font-size:13px;color:green;font-weight:bold;' id='divPlaceOrderMessage'>");
	document.writeln("<div style='text-align:center'>Your Order is being completed:<hr noshade size=1 width=90%></div>");
	document.writeln("<div style='margin:10px'>Please wait while your order is being placed.<br>If you're using credit card, this operation might take a minute or two. DO NOT press Place Order button again and do not navigate away from this page.<br>This message will disappear once your order is completed.<br>Thank You.</div>");
	//document.writeln("<div style='text-align:center'><img src='images/progress.gif' border=0></div>");
	document.writeln("</div>");
}

function showInfoInNavbar(info)
{
	var nav = document.getElementById("navbarInfoRight");
	if (nav)
		nav.innerHTML = info;
}
function hideInfoInNavbar()
{
	var nav = document.getElementById("navbarInfoRight");
	if (nav)
		nav.innerHTML = "";
}

function dontLeave()
{
	if (event && event.clientY && event.clientY < -10000) 
		window.open("DontLeave.aspx", "_blank", "width=600,height=460,scrollbars=yes");
}

function ShowVerisignInfo()
{
	window.open("https://seal.verisign.com/splash?form_file=fdf/splash.fdf&dn=WWW.PERSONALIZATIONMALL.COM&lang=en", "_blank", "width=550,height=470");
}

// navbar specials
navbarSpecials = new Array();
navbarSpecialIndex = 0;
navbarSpecialTimeout = 5000;
function addNavbarSpecial(productName,  altTag, productURL, imageURL)
{
    var s = {
        productName: productName,
        productURL: productURL,
        altTag: altTag,
        imageURL: imageURL
       };
    navbarSpecials[navbarSpecials.length] = s;
    return s;
}
function initNavbarSpecial(index)
{
    navbarSpecialIndex = index;
    showNavbarSpecial();
    
    window.setTimeout(rotateNavbarSpecial, navbarSpecialTimeout);
    
}
function rotateNavbarSpecial()
{
    navbarSpecialIndex++;
    if (navbarSpecialIndex >= navbarSpecials.length)
        navbarSpecialIndex = 0;
        
    showNavbarSpecial();
    window.setTimeout(rotateNavbarSpecial, navbarSpecialTimeout);
}
function showNavbarSpecial()
{
    var s = navbarSpecials[navbarSpecialIndex];
    var sContainer = document.getElementById("divSpecials");
    if (sContainer.filters && navbarSpecialIndex > 0 && sContainer.filters.length > 0)
        sContainer.filters[0].Apply();
    
    document.getElementById("hrefSpecialImage").href = s.productURL;
    document.getElementById("imgSpecialImage").src = s.imageURL;
    document.getElementById("imgSpecialImage").alt = s.altTag;
    document.getElementById("hrefSpecialName").innerHTML = s.productName;
    document.getElementById("hrefSpecialName").href = s.productURL;
    
    if (sContainer.filters && navbarSpecialIndex > 0 && sContainer.filters.length > 0)    
        sContainer.filters[0].Play();
}

// home alt. image script
homeImages = new Array();
homeImageIndex = 0;
homeImageTimeout = 5000;
homeImageRound = 0;
homeImageID = null
function addHomeImage(src)
{
    homeImages[homeImages.length] = src;
    return src;
}
function initHomeImages(imgID)
{
    homeImageID = imgID;
    homeImageIndex = 0;
    showHomeImage();
    homeImageRound = 1;
    
    window.setTimeout(rotateHomeImages, homeImageTimeout);
    
}
function rotateHomeImages()
{
    homeImageIndex++;
    if (homeImageIndex >= homeImages.length)
    {
        homeImageIndex = 0;
        homeImageRound++;
    }
        
    showHomeImage();
    window.setTimeout(rotateHomeImages, homeImageTimeout);
}
function showHomeImage()
{
    var s = homeImages[homeImageIndex];
    var sContainer = document.getElementById(homeImageID);
    if (sContainer.filters && homeImageRound > 0 && sContainer.filters.length > 0)
        sContainer.filters[0].Apply();
    
    sContainer.src = "images/" + s;
    
    if (sContainer.filters && homeImageRound > 0 && sContainer.filters.length > 0)    
        sContainer.filters[0].Play();
}

function isDigitOrLetter(ch)
{
	return (ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z');
}

function checkGiftCardNumber(box)
{
	var r = "";
	var g = box.value.toUpperCase();
	var digits = 0;
	
	for (i=0; i<g.length; i++)
	{
		var ch = g.charAt(i);
		
		if (isDigitOrLetter(ch))
		{
			r += ch;
			digits++;
			if (digits >= 12)
				break;
			
			if (digits % 4 == 0)
				r+='-';
		}
	}
	
	box.value = r;
}

function textboxRemainingChars_Display(textbox, divID)
{
	var div = $get(divID);
	if (div)
	{
		if (div.style.display == "none")
			div.style.display = "";
			
		if (textbox.value.length > 0)
		{
			var r = textbox.maxLength - textbox.value.length;
			div.innerHTML = r + " characters left";
		}
		else
			div.innerHTML = "Up to " + textbox.maxLength + " chars.";
		
	}

}

function SetControlValue(field, value)
{
	if (!field)
		// no pers
		return null;
	/*
	else if (field.type == 'radio')
	    return field.checked ? field.value : null;
	*/
	else if (field.type == 'select-one') 
	{
		for (i=0; i<field.options.length; i++)
			if (field.options[i].value == value)
			{
				field.selectedIndex = i;
				return;
			}	
	}
	else if (field.type == 'text' || field.type == 'textarea') 
		field.value = value;

}



function dumpObject(obj)
{
	var str = "";
	for (var f in obj)
	{
		var v = obj[f];
		str += f + ": " + v + "\n";
	}
	alert(str);
}

function getClientHeight()
{
	if (document.documentElement)
		return document.documentElement.clientHeight;
	else if (document.body.clientHeight)
		return document.body.clientHeight > 0 ? document.body.clientHeight : 600;
	else
		return 600;
}

function getScrollTop()
{
	var t1 = (document.documentElement ? document.documentElement.scrollTop : document.body.scrollTop);
	var t2 = (document.body && document.body.scrollTop ? document.body.scrollTop : 0);
	return Math.max(t1, t2) + "px";
}

 
