// JavaScript Document

function user_nav_conditional() {
	this.navs=new Array();
	
	allDivs=document.getElementsByTagName("div");
	for (divCount=0; divCount<allDivs.length; divCount++) {
		if (typeof allDivs[divCount].className != "undefined") {
			if (allDivs[divCount].className!=null) {
				if (allDivs[divCount].className.indexOf("user_nav_conditional")!=-1) {
					this.navs[this.navs.length]=allDivs[divCount];
				}
			}
		}
	}
	
	// Attach onclick events to all as with an id starting with unclink
	allAs=document.getElementsByTagName("a");
	for (aCount=0; aCount<allAs.length; aCount++) {
		if (typeof allAs[aCount].id != "undefined") {
			if (allAs[aCount].id!=null) {
				if (allAs[aCount].id.indexOf("unclink")==0) {
					allAs[aCount].unc=this;
					allAs[aCount].onclick=function() {
						this.unc.show(this);
						return false;
					}
				}
			}
		}
	}
}

user_nav_conditional.prototype.clear=function() {
	for (divCount=0; divCount<this.navs.length; divCount++) {
		if (this.navs[divCount].style.display != "none") {
			this.navs[divCount].style.display="none";
		}
	}
}

user_nav_conditional.prototype.show=function(linkRef) {
	this.clear();
	divId=linkRef.id.toString().replace(/unclink/, "userNavConditional");
	document.getElementById(divId).style.display="block";
}

// Call this function to populate, move and display the cart and favorites window.
function displayCartAndFavs(bCart) {
	if (bCart) {
		copyContents("cart_content", "cart_and_favs_win_content");
	} else {
		copyContents("favs_content", "cart_and_favs_win_content");
	}
	moveToAndShow("cart_and_favs_win", "userModule", true, false, 0, 0);
}

// Call this onload to initialize the cart and favorites.
function initCartAndFavs() {

	if (document.getElementById("cart_btn")) {
		document.getElementById("cart_btn").onclick=function() {
			displayCartAndFavs(true);
			codeDisplay('show','cart_edit');
			codeDisplay('hide','fav_edit');
			getMiniCart();
			return false;
		}
	}
	
	if (document.getElementById("fav_btn")) {
		document.getElementById("fav_btn").onclick=function() {
			displayCartAndFavs(false);
			codeDisplay('show','fav_edit');
			codeDisplay('hide','cart_edit');
			getMiniQuickList();
			return false;
		}
	}
	
	if (document.getElementById("cart_and_favs_close")) {
		document.getElementById("cart_and_favs_close").onclick=function() {
			hideObj("cart_and_favs_win");
			return false;
		}
	}
}


/*..........Code added temporary for testing purpose: Will be consolidated once all the js files are available Start...........*/

var req;

/* Will return the Cookie Value */
function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );

  if ( results )
    return ( unescape ( results[1] ) );
  else
    return null;
}

/* This function will set the content value */
function setcartandfavwincontent( message )
{
		var elementId = document.getElementById("cart_and_favs_win_content");
		elementId.innerHTML = message;
}


/* This function will create an instance of request object based on the type of client browser */
function getXMLHTTPRequest()
{
var request = false;
try
  {
	 request = new XMLHttpRequest(); /* e.g. Firefox */
	 if (request.overrideMimeType) {
			request.overrideMimeType('text/plain');
		}
  }
catch(err1)
  {
  try
    {
	  request = new ActiveXObject("Microsoft.XMLHTTP");
  /* some versions IE */
    }
  catch(err2)
    {
    try
      {
    request = new ActiveXObject("Msxml2.XMLHTTP");
	      
  /* some versions IE */
      }
      catch(err3)
        {
        request = false;
        }
    }
  }
return request;
}

/* Will retrieve the Mini Cart and register the callback function populateMiniCart */
function getMiniCart()
{
		
			rand = parseInt(Math.random()*999999999999999);	
			var url = "/ec/minicart.do?id=" + encodeURIComponent(rand);
			req = getXMLHTTPRequest();
			req.open("GET", url, true);	
			req.onreadystatechange = populateMiniCart;
			req.send(null);
		
}


/*	This method would populate the Mini Cart Contents */
function populateMiniCart()
{
	if(req.readyState == 4)
	{
		if(req.status == 200)
		{
			var textmessage = req.responseText;
			if(textmessage != null)
			{
				setcartandfavwincontent(textmessage);
			}
			
		}
		else
		{
			setcartandfavwincontent('Cart Temporarily Not Available');
		}
	}
}



/* Will retrieve the Mini Quick List and register the callback function populateMiniQuickList */
function getMiniQuickList()
{
		
			rand = parseInt(Math.random()*999999999999999);	
			var url = "/accounts/miniquicklist.do?id=" + encodeURIComponent(rand);
			req = getXMLHTTPRequest();
			req.open("GET", url, true);	
			req.onreadystatechange = populateMiniQuicklist;
			req.send(null);
				
}

/* It will populate the mini quick list */
function populateMiniQuicklist()
{
	if(req.readyState == 4)
	{
		if(req.status == 200)
		{
			var textmessage = req.responseText;
			if(textmessage != null)
			{
				setcartandfavwincontent(textmessage);
			}
			
		}
		else
		{
				setcartandfavwincontent('Favorites Temporarily Not Available');
		}
	}
}

/*..........Code added temporary for testing purpose End...........*/