// JavaScript Document



DOM = (document.getElementById) ? 1 : 0;

NS4 = (document.layers) ? 1 : 0;

Konqueror = (navigator.userAgent.indexOf("Konqueror") > -1) ? 1 : 0;

Konqueror21 = (navigator.userAgent.indexOf("Konqueror 2.1") > -1 || navigator.userAgent.indexOf("Konqueror/2.1") > -1) ? 1 : 0;

Konqueror22 = (navigator.userAgent.indexOf("Konqueror 2.2") > -1 || navigator.userAgent.indexOf("Konqueror/2.2") > -1) ? 1 : 0;

Konqueror2 = Konqueror21 || Konqueror22;

Opera = (navigator.userAgent.indexOf("Opera") > -1) ? 1 : 0;

Opera5 = (navigator.userAgent.indexOf("Opera 5") > -1 || navigator.userAgent.indexOf("Opera/5") > -1) ? 1 : 0;

Opera6 = (navigator.userAgent.indexOf("Opera 6") > -1 || navigator.userAgent.indexOf("Opera/6") > -1) ? 1 : 0;

Opera56 = Opera5 || Opera6;

IE = (document.all) ? 1 : 0;

IE4 = IE && !DOM;





function getWindowWidth() {

     var value = 0;

     if ((DOM && !IE) || NS4 || Konqueror || Opera) {

          value = top.innerWidth;

//     } else if (NS4) {

//          value = document.width;

     } else {     // IE

          if (document.documentElement && document.documentElement.clientWidth) {

               value = document.documentElement.clientWidth;

          } else if (document.body) {

               value = document.body.clientWidth;

          }

     }

     if (isNaN(value)) {

          value = top.innerWidth;

     }

     return (value);

}



function getWindowHeight() {

     var value = 0;

     if ((DOM && !IE) || NS4 || Konqueror || Opera) {

          value = top.innerHeight;

     } else {     // IE

          if (document.documentElement && document.documentElement.clientHeight) {

               value = document.documentElement.clientHeight;

          } else if (document.body) {

               value = document.body.clientHeight;

          }

     }

     if (isNaN(value)) {

          value = top.innerHeight;

     }

     return (value);

}



function showCardList(){

	width=getWindowWidth();

	newleft=(width/2)-43;

	document.getElementById('cardlist_window').style.left=newleft+'px';

	document.getElementById('cardlist_window').style.display='block';

}



function closeCardList(){

	document.getElementById('cardlist_window').style.display='none';

}



function uppercaseFirst(val){

	var firstletter=val.substr(0,1);

	var restofword=val.substr(1,(val.length-1));

	return firstletter.toUpperCase()+restofword.toLowerCase();

}



var XmlHttpObj;



// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla

function CreateXmlHttpObj()

{

	// try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)

	try

	{

		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");

	}

	catch(e)

	{

		try

		{

			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");

		} 

		catch(oc)

		{

			XmlHttpObj = null;

		}

	}

	// if unable to create using IE specific code then try creating for Mozilla (FireFox) 

	if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") 

	{

		XmlHttpObj = new XMLHttpRequest();

	}

}



// called from onChange or onClick event of the continent dropdown list

function doCardQtyChange(deck_id,card_id,act) 

{

    // url of page that will send xml data back to client browser

    var requestUrl;

    // use the following line if using asp

    requestUrl = "Scripts/changeqty.php?cmd="+act+"&deck="+deck_id+"&card="+card_id;

    // use the following line if using php

    // requestUrl = "xml_data_provider.php" + "?filter=" + encodeURIComponent(selectedContinent);

    

	CreateXmlHttpObj();

	

	// verify XmlHttpObj variable was successfully initialized

	if(XmlHttpObj)

	{

        // assign the StateChangeHandler function ( defined below in this file)

        // to be called when the state of the XmlHttpObj changes

        // receiving data back from the server is one such change

		XmlHttpObj.onreadystatechange = StateChangeHandler;

		

		// define the iteraction with the server -- true for as asynchronous.

		XmlHttpObj.open("GET", requestUrl,  true);

		

		// send request to server, null arg  when using "GET"

		XmlHttpObj.send(null);		

	}

}


function doSideCardQtyChange(deck_id,card_id,act) 

{

    // url of page that will send xml data back to client browser

    var requestUrl;

    // use the following line if using asp

    requestUrl = "Scripts/changeqty_side.php?cmd="+act+"&deck="+deck_id+"&card="+card_id;

    // use the following line if using php

    // requestUrl = "xml_data_provider.php" + "?filter=" + encodeURIComponent(selectedContinent);

    

	CreateXmlHttpObj();

	

	// verify XmlHttpObj variable was successfully initialized

	if(XmlHttpObj)

	{

        // assign the StateChangeHandler function ( defined below in this file)

        // to be called when the state of the XmlHttpObj changes

        // receiving data back from the server is one such change

		XmlHttpObj.onreadystatechange = StateChangeHandlerSide;

		

		// define the iteraction with the server -- true for as asynchronous.

		XmlHttpObj.open("GET", requestUrl,  true);

		

		// send request to server, null arg  when using "GET"

		XmlHttpObj.send(null);		

	}

}



function StateChangeHandler()

{

	// state ==4 indicates receiving response data from server is completed

	if(XmlHttpObj.readyState == 4)

	{

		// To make sure valid response is received from the server, 200 means response received is OK

		if(XmlHttpObj.status == 200)

		{			

			PopulateCountryList(XmlHttpObj.responseXML.documentElement);

		}

		else

		{

			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);

		}

	}

}

function StateChangeHandlerSide()

{

	// state ==4 indicates receiving response data from server is completed

	if(XmlHttpObj.readyState == 4)

	{

		// To make sure valid response is received from the server, 200 means response received is OK

		if(XmlHttpObj.status == 200)

		{			

			PopulateCountryListSide(XmlHttpObj.responseXML.documentElement);

		}

		else

		{

			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);

		}

	}

}



function PopulateCountryList(countryNode)

{

	var countryNodes = countryNode.getElementsByTagName('result');

	resultval=GetInnerText(countryNodes[0]);

	var countryNodesf = countryNode.getElementsByTagName('cardid');

	cardid=GetInnerText(countryNodesf[0]);

	var countryNodesrr = countryNode.getElementsByTagName('deckid');

	deckid=GetInnerText(countryNodesrr[0]);

	var countryNodesr = countryNode.getElementsByTagName('newqty');

	newqty=GetInnerText(countryNodesr[0]);
	
	var ccou=countryNode.getElementsByTagName('newdecktotal');
	
	newtotal=GetInnerText(ccou[0]);
	
	var sccou=countryNode.getElementsByTagName('newsidedecktotal');
	
	sdnewtotal=GetInnerText(sccou[0]);

	if(resultval == "toomany"){

		alert('Max quantity for this card has been reached.');

	}else if(resultval=="alreadygone"){

		document.getElementById('cardrow_'+cardid).style.display='none';
		document.getElementById('current_card_total').innerHTML=newtotal;
		document.getElementById('sd_current_card_total').innerHTML=sdnewtotal;

	}else if(resultval=="done"){

		document.getElementById('qty_'+deckid+'_'+cardid).innerHTML=newqty;
		document.getElementById('current_card_total').innerHTML=newtotal;
		document.getElementById('sd_current_card_total').innerHTML=sdnewtotal;

	}

}


function PopulateCountryListSide(countryNode)

{

	var countryNodes = countryNode.getElementsByTagName('result');

	resultval=GetInnerText(countryNodes[0]);

	var countryNodesf = countryNode.getElementsByTagName('cardid');

	cardid=GetInnerText(countryNodesf[0]);

	var countryNodesrr = countryNode.getElementsByTagName('deckid');

	deckid=GetInnerText(countryNodesrr[0]);

	var countryNodesr = countryNode.getElementsByTagName('newqty');

	newqty=GetInnerText(countryNodesr[0]);
	
	var ccou=countryNode.getElementsByTagName('newdecktotal');
	
	newtotal=GetInnerText(ccou[0]);
	
	var sccou=countryNode.getElementsByTagName('newsidedecktotal');
	
	sdnewtotal=GetInnerText(sccou[0]);

	if(resultval == "toomany"){

		alert('Max quantity for this card has been reached.');

	}else if(resultval=="sdtoomany"){

		alert('Side Deck "max number of cards" has been reached.');

	}else if(resultval=="alreadygone"){

		document.getElementById('sd_cardrow_'+cardid).style.display='none';
		document.getElementById('current_card_total').innerHTML=newtotal;
		document.getElementById('sd_current_card_total').innerHTML=sdnewtotal;

	}else if(resultval=="done"){

		document.getElementById('sd_qty_'+deckid+'_'+cardid).innerHTML=newqty;
		document.getElementById('current_card_total').innerHTML=newtotal;
		document.getElementById('sd_current_card_total').innerHTML=sdnewtotal;

	}

}



// returns the node text value 

function GetInnerText (node)

{

	 return (node.textContent || node.innerText || node.text) ;

}



function toggle(id){

	if(document.getElementById(id+'_toggleimg').src=='http://bleachgaming.com/themes/decks/images/exp_plus.png'){

	document.getElementById(id+'_toggleimg').src='http://bleachgaming.com/themes/decks/images/exp_minus.png';

	document.getElementById(id+"_inner").style.display='block';

	}else{

	document.getElementById(id+'_toggleimg').src='http://bleachgaming.com/themes/decks/images/exp_plus.png';

	document.getElementById(id+"_inner").style.display='none';

	}

}