// A global onready for worldly stuff
$(document).ready(function() {
    
    // Add login link catch
    $("#loginLink").click(function(event) {
    	if (event.target == this) {
    		$('.newLoginBox').slideToggle()
    		event.stopPropagation();
    	}
    	return false;
    });
    
    $("#userNav").click(function(event) {
    	if (event.target == this) {
    		top.location='/birthday/';
    	}
    	return true;
    });
    $("#header").click(function(event) {
    	if (event.target == this) {
    		top.location='/birthday/';
    	}
    	return true;
    });
    
    // Adjust the login drop-down box position for non-IE browsers and ADs
    if ($('#topAd').height() > 0) {
        // We have ads, let make adjustments based on browser
        if ($.browser.msie) {
            $('.newLoginBox').addClass('newLoginBoxWithAds');
        } else {
            $('.newLoginBox').addClass('newLoginBoxNonIEWithAds');
        }
    } else {
        // No ads, only adjust for non-ie browsers
        if (!$.browser.msie) {
            $('.newLoginBox').addClass('newLoginBoxNonIE');
        }
    }
        
    // Catch enter in the login dd
    $('#ddPassword').keyup( function(event) {
        if (event.keyCode == 13) {
        doDropDownLogin();
        }
    })
    
    // Do Tooltip replacement
    $(document).ready(function() {
        $('.toolTip').tooltip({
           showURL: false,
           showBody: " -*-* ",
           fixPNG: true,
           right: 8,
           fade: 250 
        });
    });
    
    // FancyBox Links
    $("a.fancybox").fancybox({
        'hideOnContentClick': true,
        'overlayShow': true 
    });
    
    // Fix PNG images
    $(document).pngFix();
});

<!--//hide this script from non-javascript-enabled browsers
var FirebugUtils={

	consoleMethods:["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"],

	available:function() {
		return (window.console) ? (window.console.firebug) : false;
	},
	x:function() {
		if(!this.available()) this.disable();
	},
	disable:function() {
		window.console = {};
		$A(this.consoleMethods).each(function(method) {
			window.console[method] = function(){};
		});
	}
}

var FieldErrorRows="";
var FieldErrorText="";
	
function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}
/* Functions that swaps images. */
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

/* Functions that handle preload. */
function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function CheckKeyNumeric(bAllowDecimal) {
	xval=window.event.keyCode
	if ((47 < xval && xval < 58) || (xval==46 && bAllowDecimal)) {
		// Good! 
		}
	else {
		window.event.returnValue=false;
	}
}

function CheckKeyUCase() {
	xval=window.event.keyCode
	if (96 < xval && xval < 123) {
		window.event.keyCode=xval-32;
		}
}

function HTMLStringCharOnly(sOrigString) {
	var sReturn="";
	var sTemp=new String(sOrigString).toUpperCase();
	for (i=0;i<sTemp.length;i++) {
		xval=sTemp.charCodeAt(i);
		if ((64 < xval && xval < 91) || (47 < xval && xval < 58)) { sReturn+=sTemp.substr(i,1); }
		}
	
	return sReturn;
}

function fieldError(sFieldName,sDisplayMessage,nLevel) {
	var sFieldId=HTMLStringCharOnly(sFieldName)+"label";
	if (nLevel<1) nLevel=1;
	var sNewClass="ASKError"+nLevel;
	x=document.getElementById(sFieldId);
	x.className=sNewClass;
	FieldErrorText+=sDisplayMessage+'\n';
	FieldErrorRows+='<tr><td class="'+sNewClass+'" width="10" height="10">&nbsp;</td><td>'+sDisplayMessage+'</td></tr>';
}

function ResetFieldClass(sFieldName) {
	document.getElementById(HTMLStringCharOnly(sFieldName)+"label").className="askHeading";
}
	

function ShowFieldErrors(bShowAlertBox) {
	var sNewErrorDisplay = ""
	if (FieldErrorText.length > 0) {
		sNewErrorDisplay = '<table border="0" width="100%">'
	
		if (document.getElementById("SystemMessages").innerHTML.length == 0) {
			sNewErrorDisplay+='<tr><td class="ASKtblHeading" colspan="2">Processing Messages</td></tr>' }
	
		sNewErrorDisplay+=FieldErrorRows;

		if (bShowAlertBox) alert(FieldErrorText);
		
		// Not sure whether I should do this or not yet:
		FieldErrorText="";
		FieldErrorRows="";
		}
	x=document.getElementById("FieldErrorMessages");
	x.innerHTML=sNewErrorDisplay;
	
}

function makeStringFromSelect(selectCtrl) {
	var i;
	var j = 0;
	var outlist = "";

	for (i = 0; i < selectCtrl.options.length; i++) {
		if (j > 0) {
			outlist = outlist + ", ";
		}
		outlist = outlist + selectCtrl.options[i].value;
		j++;
	}
	return outlist;
}

function moveItems(MoveDirection,listACtrl, listBCtrl, listAValueField, listBValueField) {
	if (MoveDirection=="AtoB") {
		addItems(listACtrl, listBCtrl); 
		removeItems(listACtrl); }
	else {
		addItems(listBCtrl, listACtrl); 
		removeItems(listBCtrl); }
	

	listAValueField.value = makeStringFromSelect(listACtrl); 
	listBValueField.value = makeStringFromSelect(listBCtrl);
}

function addItems(fromCtrl, toCtrl) {
	var i;
	var j;
	var itemexists;
	var nextitem;

	// step through all items in fromCtrl
	for (i = 0; i < fromCtrl.options.length; i++) {
		if (fromCtrl.options[i].selected) {
			// search toCtrl to see if duplicate
			j = 0;
			itemexists = false;
			while ((j < toCtrl.options.length) && (!(itemexists))) {
				if (toCtrl.options[j].value == fromCtrl.options[i].value) {
					itemexists = true;
					alert(fromCtrl.options[i].value + " found!");
				}
				j++;
			}
			if (!(itemexists)) {
				// add the item
				nextitem = toCtrl.options.length;
				toCtrl.options[nextitem] = new Option(fromCtrl.options[i].text);
				toCtrl.options[nextitem].value = fromCtrl.options[i].value;
			}
		}
	}
}

function removeItems(fromCtrl) {
	var i = 0;
	var j;
	var k = 0;

	while (i < (fromCtrl.options.length - k)) {
		if (fromCtrl.options[i].selected) {
			// remove the item
			for (j = i; j < (fromCtrl.options.length - 1); j++) {
				fromCtrl.options[j].text = fromCtrl.options[j+1].text;
				fromCtrl.options[j].value = fromCtrl.options[j+1].value;
				fromCtrl.options[j].selected = fromCtrl.options[j+1].selected;
			}
			k++;
		} else {
			i++;
		}
	}
	for (i = 0; i < k; i++) {
		fromCtrl.options[fromCtrl.options.length - 1] = null;
	}
}

function showPopupIframe(bDisplay,sSrc) {
	if (bDisplay) {
		document.getElementById('PopupIframe').src = sSrc;
		document.getElementById('PopupIframeDiv').style.top = document.body.scrollTop + 200;
		document.getElementById('PopupIframeDiv').style.visibility = "visible"; 
		}
	else {
		document.getElementById('PopupIframe').src = '';
		document.getElementById('PopupIframeDiv').style.visibility = "hidden"; 
		}
}

function showFieldHelp(lIndex) {
	if (FieldHelpArray) {
		var sFieldHelpIconName='FieldHelpImage'+lIndex;
		var sFieldHelpTop='<table border="0" width="100%"><tr bgcolor="silver"><td align="right">'+
			'<a href="javascript:hideFieldHelp();">[close]</a></td></tr><tr><td align="left">'
		var sFieldHelpBottom='</td></tr></table>';
		var FHDiv=document.getElementById('FieldHelpDiv');
		FHDiv.innerHTML=sFieldHelpTop+FieldHelpArray[lIndex]+sFieldHelpBottom;
		//FHDiv.style.top = document.body.scrollTop + 200;
		x=(getAnchorPosition(sFieldHelpIconName).x - 175);
		if (x > 600) { x=x-235; }
		FHDiv.style.left = x;
		FHDiv.style.top  = (getAnchorPosition(sFieldHelpIconName).y + 20);
		showSelect(false);
		FHDiv.style.visibility = "visible"; 
		}
	else {
		alert('Field Help Array Not Available.');
		}
}

function hideFieldHelp() {
	var FHDiv=document.getElementById('FieldHelpDiv');
	FHDiv.style.visibility = "hidden"; 
}

	
function validateLogin()
{
	var bContinue = true;
	var sMsg;
	if ((bContinue==true) && (noValueEntered(document.frmLogin.txtUserId.value)))
	{
		document.frmLogin.txtUserId.focus();
		sMsg = 'Please enter a User ID.';
		bContinue = false;
	}
	if ((bContinue==true) && (noValueEntered(document.frmLogin.txtPwd.value)))
	{
		document.frmLogin.txtPwd.focus();
		sMsg = 'Please enter a Password.';
		bContinue = false;
	}
	if (bContinue==false)
	{
		alert(sMsg);
		return false;
	}
	return true;
}//end CheckReqFlds

function showItem(sItemName) {
	x=document.getElementById(sItemName);
	nav=navigator.appName.toLowerCase().substring(0,3);
	//alert(nav);
	if (nav=='net' || nav=='nav')
	{
		x.style.display="table-row";
	}
	else
	{
		x.style.display="block";
	}
}

function hideItem(sItemName) { document.getElementById(sItemName).style.display='none' }

function toggleItem(sItemName) {
	if ( document.getElementById(sItemName).style.display=='none' )
	{
		x=document.getElementById(sItemName);
		nav=navigator.appName.toLowerCase().substring(0,3);
		//alert(nav);
		if (nav=='net' || nav=='nav')
		{
			x.style.display="table-row";
		}
		else
		{
			x.style.display="block";
		}
		
	}
	else
	{
		document.getElementById(sItemName).style.display='none';
	}
}


function noValueEntered(strText)
{
	var SPACE = " ";
	var blnContinue = false;
	for (var i = 0; i < strText.length; i++)
	{
		if (strText.charAt(i) != SPACE)
		{
			blnContinue = true;
		}
	}
	if (blnContinue)
	{
		if (strText == "")
		{
			return true;
		} else {
			return false;
		}
	} else {
		return true;
	}
}//end ValueEntered

function doselectall(strForm,srcObject) {
	sTheForm=eval('document.'+strForm);
	
	for(j=0;j < sTheForm.elements.length;++j)
	{
		if(sTheForm.elements[j].type=="checkbox" && sTheForm.elements[j] != srcObject )
			sTheForm.elements[j].checked=srcObject.checked;
	}
}



function copyrightYear(){
	if(document.getElementById('insertYear')) document.getElementById('insertYear').innerHTML = new Date().getFullYear();
}


function externalLinks(){
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		if (anchors[i].getAttribute("href") && anchors[i].getAttribute("rel") == "external") anchors[i].target = "_blank";
	}
}



//Checks to see if safari is on, boolean
var detect = navigator.userAgent.toLowerCase();
var safStatus,thestring;
function checkSafari(){	
	if (checkIt('safari')) safStatus = true;
	else safStatus = false;
	function checkIt(string){
		place = detect.indexOf(string) + 1;
		return place;
	}
}


siteOnLoad = function (){
	checkSafari();
	copyrightYear();
	externalLinks();
}

function addLoadEvent(func){
	var oldOnLoad = window.onload;
	if(typeof window.onload != 'function'){ 
		window.onload = func;
	}
	else{ 
		window.onload = function(){ 
			oldOnLoad();
			func();
		}
	}
}


addLoadEvent(siteOnLoad);

/*
    Login
*/
function doDropDownLogin() {
    // Let them know we are trying
    $('#loginStatusBar').html("Attempting Login...").css("color", "black").slideDown();
    
    var username = document.getElementById("ddUsername").value;
    var password = document.getElementById("ddPassword").value;
    var remember = document.getElementById("loginRemember").value;
    
    var data = {username: username, password: password, loginRemember: remember}
    $.post('/login.asp?ajax=true', data, function(data) {
        if (data == 'SUCCESS') {
            $('#loginStatusBar').html("Success!").css("color", "green");
            window.location = window.location;
        } else {
            $('#loginStatusBar').html("Invalid Username/Password").css("color", "red");
        }
    });
}

function doLocalLogin() {
    // Let them know we are trying
    $('#localLoginStatus').html("Attempting Login...").css("color", "black").slideDown();
    
    var username = document.getElementById("localLoginUsername").value;
    var password = document.getElementById("localLoginPassword").value;
    var remember = document.getElementById("loginLocalRemember").value;
    
    var data = {username: username, password: password, loginLocalRemember: remember}
    $.post('/login.asp?ajax=true', data, function(data) {
        if (data == 'SUCCESS') {
            $('#localLoginStatus').html("Success!").css("color", "green");
            window.location = window.location;
        } else {
            $('#localLoginStatus').html("Invalid Username/Password").css("color", "red");
        }
    });
}


/* Soon to be */
function addToWishV2(textSpan,product_id) {
	$('#textSpan').html("<span style='color:red;'>Adding to your Wishlist...</span>");
    $.get('/wish_list_add.asp', {ajax: 'true', wllid: textSpan, p: product_id}, function(data) {
        document.getElementById(data.substring(5)).innerHTML = "<span class=\"standouttext\">Item added to your Wishlist.</span>";
		return false;
    });
	return false;
}

/*
function addToWishV2(textSpan,product_id) {
	document.getElementById(textSpan).innerHTML = "<span style='color:red;'>Adding to your Wishlist...</span>";
	YAHOO.util.Connect.asyncRequest("GET", "/wish_list_add.asp?ajax=true&wllid=" + textSpan + "&p=" + product_id, addToWishCallbackV2);
	return false;
}

var addToWishCallbackV2 = {
	success: function(o) {
		document.getElementById(o.responseText.substring(5)).innerHTML = "<span class=\"standouttext\">Item added to your Wishlist.</span>";
		return false;
		}
}
*/

function j_addToBOS(textSpan, object_type, object_id) {
    document.getElementById(textSpan).innerHTML = "<span style='color:red;'>Adding to your Bookmarks...</span>";
    $.get('/bos_add_ajax.asp', {boslid: textSpan, t: object_type, id: object_id}, function(data) {
        document.getElementById(data).innerHTML = "<span class=\"standouttext\">Item added to your Bookmarks.</span>";
		return false;
    });
    return false;
}

/*
function j_addToBOS(textSpan,object_type,object_id) {
	document.getElementById(textSpan).innerHTML = "<span style='color:red;'>Adding to your Bookmarks...</span>";
	YAHOO.util.Connect.asyncRequest("GET", "/bos_add_ajax.asp?boslid=" + textSpan + "&t=" + object_type+"&id="+object_id, j_addToBOSCallback);
	return false;
}

var j_addToBOSCallback = {
	success: function(o) {
		document.getElementById(o.responseText).innerHTML = "<span class=\"standouttext\">Item added to your Bookmarks.</span>";
		return false;
		}
}
*/

function add_related_product(product_id, brief_description, object_id, object_type) {
    $.getJSON('/shop/add_related_product/' + object_type + '/' + object_id + '/' + product_id + '/', {}, 
        function (data) {
            if (data) {
                $('#addRelatedLink_' + product_id).parent().html("Added").css("color", "blue");
            } else {
                $('#addRelatedLink_' + product_id).parent().html("Error Saving Related Product").css("color", "red");
            }
        }
    );
}

function remove_related_product(object_id, object_type, product_id) {
    $.getJSON('/shop/remove_related_product/' + object_type + '/' + object_id + '/' + product_id + '/', {}, 
        function (data) {
            if (data) {
                $('#relatedProduct_' + product_id).slideUp();
                
                if ($('#relatedProducts ul li').length == 0) {
                    window.location.reload();
                }
            }
        }
    );
}

function moderateObject(object_id, object_type) {
    
    $('#moderationRow' + object_id).html("Submitting...").css("color", "blue");
    
    $.get('/moderate.asp', { 'object_id': object_id, 'object_type': object_type }, function(data) {
        $('#moderationRow' + object_id).html("Layout Private");
    });
}    