var browserName  = navigator.appName;
notMSIE=0

blankvar ="&nbsp;"



function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}



function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}





function clear() {
totalprice=0
totalpricethispage=0
i = -1 
w = -1
doublestbl = document.getElementById('doubles');
totalsrow = doublestbl.rows.length - 2;
alltotalsrow = doublestbl.rows.length - 1;
totalsheaderrownumber = 0 
alltotalsheaderrownumber = 1
placeholder=""
placeholderthispage =""
invoiceline=new Array(100)
for (c=0; c <100; c++) {
invoiceline[c]=new Array('Card1', 'Player1', 'Price1') }
bgvar=0
rowspanflag=0


}





function readinvcookies() {


for  (a = 0; a < 20; a++) {
    for  (b = 0; b < 3; b++) {
cookiefieldname = 'invoiceline'+a+'field'+b


var x = readCookie(cookiefieldname)


 if (x) {i++

invoiceline[a][b]=x 
if (b==2) {
totalprice = totalprice + Number(invoiceline[a][b].replace(/[^0-9-.]/g, ""));

}

} else break

}}

i = (i-2)/3
totalprice = (parseInt(totalprice*100,10))/100 
if (totalprice * 10 != Math.floor(totalprice*10)) {placeholder=""} 
if (totalprice * 10 == Math.floor(totalprice*10)) {placeholder="0"} 
if (totalprice == Math.floor(totalprice)) {placeholder=".00"} 
if (totalprice == 0) {placeholder=""
makealltotalsrowsblank()
 } else {populatealltotalsrows() }


 }

function makealltotalsrowsblank() {




doublestbl.rows[alltotalsheaderrownumber].cells[1].innerHTML= blankvar
doublestbl.rows[alltotalsheaderrownumber].cells[4].innerHTML= blankvar
doublestbl.rows[alltotalsrow].cells[1].innerHTML= blankvar
doublestbl.rows[alltotalsrow].cells[4].innerHTML= blankvar}

function maketotalsrowsblank() {




doublestbl.rows[totalsheaderrownumber].cells[1].innerHTML= blankvar
doublestbl.rows[totalsheaderrownumber].cells[4].innerHTML= blankvar
doublestbl.rows[totalsrow].cells[1].innerHTML= blankvar
doublestbl.rows[totalsrow].cells[4].innerHTML= blankvar}


function populatealltotalsrows() {
if (i ==0) {itemvar = "ITEM"} else {itemvar = "ITEMS"}
if (notMSIE==1)

{doublestbl.rows[alltotalsheaderrownumber].cells[1].innerHTML= "TOTALS IN CART "+(i+1) +" " + itemvar + ":"
doublestbl.rows[alltotalsheaderrownumber].cells[4].innerHTML= "$" + totalprice + placeholder
doublestbl.rows[alltotalsrow].cells[1].innerHTML= "TOTALS IN CART "+(i+1) +" " + itemvar + ":"
doublestbl.rows[alltotalsrow].cells[4].innerHTML= "$" + totalprice + placeholder}

else 

{doublestbl.rows[alltotalsheaderrownumber].cells[1].firstChild.nodeValue= "TOTALS IN CART "+(i+1) +" " + itemvar + ":"
doublestbl.rows[alltotalsheaderrownumber].cells[4].firstChild.nodeValue= "$" + totalprice + placeholder
doublestbl.rows[alltotalsrow].cells[1].firstChild.nodeValue= "TOTALS IN CART "+(i+1) +" " + itemvar + ":"
doublestbl.rows[alltotalsrow].cells[4].firstChild.nodeValue= "$" + totalprice + placeholder
}}

function populatetotalsrows() {
if (w ==0) {itemvarthispage = "ITEM"} else {itemvarthispage = "ITEMS"}
if (notMSIE==1)

{doublestbl.rows[totalsheaderrownumber].cells[1].innerHTML= "TOTALS THIS PAGE "+(w+1) +" " + itemvarthispage + ":"
doublestbl.rows[totalsheaderrownumber].cells[4].innerHTML= "$" + totalpricethispage + placeholderthispage
doublestbl.rows[totalsrow].cells[1].innerHTML= "TOTALS THIS PAGE  "+(w+1) +" " + itemvarthispage + ":"
doublestbl.rows[totalsrow].cells[4].innerHTML= "$" + totalpricethispage + placeholderthispage}

else 

{doublestbl.rows[totalsheaderrownumber].cells[1].firstChild.nodeValue= "TOTALS THIS PAGE "+(w+1) +" " + itemvarthispage + ":"
doublestbl.rows[totalsheaderrownumber].cells[4].firstChild.nodeValue= "$" + totalpricethispage + placeholderthispage
doublestbl.rows[totalsrow].cells[1].firstChild.nodeValue= "TOTALS THIS PAGE "+(w+1) +" " + itemvarthispage + ":"
doublestbl.rows[totalsrow].cells[4].firstChild.nodeValue= "$" + totalpricethispage + placeholderthispage
}}

function printinvoice(){

if (invoiceline[0][1]==" " || invoiceline[0][1]=="Player1") {alert('There\'s Nothing In Your Cart!')} else{

addtocartsubtotal('norow') 

}
}

   function roll(obj, highlightcolor, textcolor){
                offset = 0


                if (obj.cells[0].rowspan > 1)  
                {offset=0
                obj.cells[0].style.backgroundColor=highlightcolor
                obj.cells[0].style.color = textcolor;
}
                if (obj.cells[5]== undefined) {offset = 1}
                obj.cells[1-offset].style.backgroundColor=highlightcolor
                obj.cells[1-offset].style.color = textcolor;
                obj.cells[2-offset].style.backgroundColor=highlightcolor
                obj.cells[2-offset].style.color = textcolor;
                obj.cells[3-offset].style.backgroundColor=highlightcolor
                obj.cells[3-offset].style.color = textcolor;
                obj.cells[4-offset].style.backgroundColor=highlightcolor
                obj.cells[4-offset].style.color = textcolor;
                obj.cells[5-offset].style.backgroundColor=highlightcolor
                obj.cells[5-offset].style.color = textcolor;
            }

function redefault(obj){

          
            if (obj.cells[0].rowspan > 1)  
            {offset=0
            obj.cells[0].style.backgroundColor ="black";
            obj.cells[0].style.color = "white"
}
            if (obj.cells[5]== undefined) {offset = 1}
            obj.cells[1-offset].style.backgroundColor ="black";
            obj.cells[1-offset].style.color = "white"
            obj.cells[2-offset].style.backgroundColor ="black";
            obj.cells[2-offset].style.color = "white"
            obj.cells[3-offset].style.backgroundColor ="black";
            obj.cells[3-offset].style.color = "white"
            obj.cells[4-offset].style.backgroundColor ="black";
            obj.cells[4-offset].style.color = "white"
            obj.cells[5-offset].style.backgroundColor ="black";
            obj.cells[5-offset].style.color = "white"
}



function addtocartsubtotal(elem) {
dupecookieflag = 0

playernamecell=0
cardcell=1
pricecell=2
subtotalcell = 4

if (elem != 'norow') {

var iteration = 0 
var add = document.getElementById(elem).rowIndex



if (doublestbl.rows[add].cells[0].rowSpan > 1) {
rowspanflag=0
playernamecell=0
cardcell=1
pricecell=2
subtotalcell = 4}




if (doublestbl.rows[add].cells[0].rowSpan == 1) {
   for (rowspanchecker = add; rowspanchecker > 0; rowspanchecker--) {
       iteration++
       if  (doublestbl.rows[rowspanchecker].cells[0].rowSpan > 1 && iteration <= doublestbl.rows[rowspanchecker].cells[0].rowSpan) {

              rowspanflag=1
              playernamecell=0
              cardcell=0
              pricecell=1
              subtotalcell = 3
              break} else {rowspanflag=0}
      
  } 


}


  for  (a = 0; a < 40; a++) {
                 for  (b = 0; b < 3; b++) {

                 cookiefieldname = 'invoiceline'+a+'field'+b
                  

                 var x = readCookie(cookiefieldname)
                     if (x != null) {
                 if (x == doublestbl.rows[add].cells[cardcell].firstChild.nodeValue) {
                    alert ('This Card had already been in your cart!')
                    dupecookieflag = 1
                    


                         }                                   }
                 

                                            } 

                                               }




if (doublestbl.rows[add].cells[subtotalcell].firstChild.nodeValue != "$" + doublestbl.rows[add].cells[pricecell].firstChild.nodeValue && dupecookieflag == 0) {





i++
w++

if (doublestbl.rows[add].cells[0].rowSpan > 1) {invoiceline[i][1] =        doublestbl.rows[add].cells[playernamecell].firstChild.nodeValue;}

if (doublestbl.rows[add].cells[0].rowSpan == 1) {

   if (rowspanflag==1) {invoiceline[i][1] =    doublestbl.rows[rowspanchecker].cells[0].firstChild.nodeValue;}else{
   invoiceline[i][1] =        doublestbl.rows[add].cells[0].firstChild.nodeValue}

}



var newprice = doublestbl.rows[add].cells[pricecell].firstChild.nodeValue
doublestbl.rows[add].cells[subtotalcell].firstChild.nodeValue= "$"+newprice



invoiceline[i][0] = doublestbl.rows[add].cells[cardcell].firstChild.nodeValue;

invoiceline[i][2] = "$"+doublestbl.rows[add].cells[pricecell].firstChild.nodeValue;



invoiceline.sort();


var newprice = doublestbl.rows[add].cells[pricecell].firstChild.nodeValue
totalprice = (parseInt(totalprice*100,10) + parseInt(newprice*100,10))/100
totalpricethispage = (parseInt(totalpricethispage*100,10) + parseInt(newprice*100,10))/100
doublestbl.rows[add].cells[subtotalcell].firstChild.nodeValue= "$"+newprice





if (totalpricethispage * 10 != Math.floor(totalpricethispage*10)) {placeholderthispage=""} 
if (totalpricethispage * 10 == Math.floor(totalpricethispage*10)) {placeholderthispage="0"} 
if (totalpricethispage == Math.floor(totalpricethispage)) {placeholderthispage=".00"} 
if (totalpricethispage == 0) {placeholderthispage=""} 
if (totalprice * 10 != Math.floor(totalprice*10)) {placeholder=""} 
if (totalprice * 10 == Math.floor(totalprice*10)) {placeholder="0"} 
if (totalprice == Math.floor(totalprice)) {placeholder=".00"} 
if (totalprice == 0) {placeholder=""} 

if (i == -1) {makealltotalsrowsblank()} else
              {populatealltotalsrows()}


if (w == -1) {maketotalsrowsblank()} else
              {populatetotalsrows()}


} 

else return } else




{

if (i < 5) {var shipping=200} else {shipping=300}
invheight=500+ 15*i
config = 'width=700,height='+invheight+',scrollbars=yes,resizable=yes'
invoice = window.open ("","",config)
invoice.reload;
invoice.moveTo(0,0);
invoice.document.write('<body bgcolor=\"#CECAC8\">')
invoice.document.write('<title>Invoice</title><center><font color=navy><h3>Joshua Raisen<br>6800 NW 15 Street<br>Plantation, FL<br><font color=red>http://www.astroland.net</h3><hr style=color:navy width=80%><table cellpadding=2 cellspacing = 0 style=\"width:660;border: 2px double black\"><caption><b><font style=\"font-size:24px\" color=navy>Invoice</font></b></caption><th align=left width=250>&nbsp;&nbsp;Card<th align=left width=150>&nbsp;&nbsp;Player<th align=right width=100>Price&nbsp;&nbsp;<tr>')
for (a = 0; a <=i;a++) {
    for (b = 0; b < 3; b++) {
if (a%2==1) {bgvar=0} else {bgvar="c"}
if (b==0) {alignvar='left'} 
if (b==1) {alignvar='left'} 
if (b==2) {alignvar='right'} 
invoice.document.write('<td style=\"background-color:#'+"F"+bgvar+"F"+bgvar+"F"+bgvar+'\" align='+alignvar+'>&nbsp;&nbsp;'+invoiceline[a][b]+'&nbsp;&nbsp;')}
invoice.document.write('<tr>')

}
invoice.document.write('<td>&nbsp;<tr><td><td style=\"background-color:#'+"F"+bgvar+"F"+bgvar+"F"+bgvar+'\" align=right><b>Shipping</b><td align=right style=\"text-decoration:underline;background-color:#'+"F"+bgvar+"F"+bgvar+"F"+bgvar+'\"><b>$'+(parseInt(shipping,10)/100)+'.00&nbsp;&nbsp;<tr>')
a=a++
if (a%2==1) {bgvar=0} else {bgvar="c"}
invoice.document.write('<td><td style=\"background-color:#'+"F"+bgvar+"F"+bgvar+"F"+bgvar+'\" align=right><b>Total</b><td style=\"background-color:#'+"F"+bgvar+"F"+bgvar+"F"+bgvar+'\" align=right><b>$'+(parseInt(totalprice*100,10) + shipping)/100+placeholder+'&nbsp;&nbsp;</b>')
invoice.document.write('</table></center>')
invoice.document.write('<p><table style=\"width:550\" border=0 cellspacing=0 cellpadding=3><td colspan=3><a style=\"color:navy\" href=\"javascript:window.print()\" onClick=\"window.location.reload()\">Click to Print this page and mail me cash, check, or money order</a><tr><td width=550 colspan=3>&nbsp;<tr>')>
invoice.document.write('<td width= 240 height=65 valign=middle style=\"font-weight:700;font-size:16px;border-bottom: 3px solid black;border-top: 3px solid black;border-left:3px solid black\"><font color=red>Click the Astroland-PayPal logo to be taken to Paypal and pay now')
invoice.document.write('<td valign=middle height=65 width=65 style=\"border-top: 3px solid black; border-bottom: 3px solid black\">') 
invoice.document.write('<form target=\"_blank\" action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\">')
invoice.document.write('<input type=\"hidden\" name=\"cmd\" value=\"_cart\">')

invoice.document.write('<input type=\"hidden\" name=\"upload\" value=\"1\">')
invoice.document.write('<input type=\"hidden\" name=\"Cn\" value=\"Thank you for your purchase of Astroland Baseball Cards\">')

invoice.document.write('<input type=\"hidden\" name=\"business" value=\"r&#97;is&#101;nj@&#98;&#101;llsout&#104;.n&#101;t\">')

invoice.document.write('<input type=\"hidden\" name=\"currency_code" value=\"USD\">')
i = i+1
if (i==1) {var paypaltense=" Card"} else {var paypaltense="  Cards"}
      for (var paypalloop = 0; paypalloop < i;paypalloop++) {


invoice.document.write('<input type=\"hidden\" name=\"item_name_'+parseInt(paypalloop+1)+'\" value=\"'+invoiceline[paypalloop][0]+' '+invoiceline[paypalloop][1]+'\">')
invoice.document.write('<input type=\"hidden\" name=\"amount_'+parseInt(paypalloop+1)+'\" value=\"'+invoiceline[paypalloop][2].slice(1)+'\">')



}

i=i-1
invoice.document.write('<input type=\"hidden\" name=\"shipping_1\" value=\"'+(parseInt(shipping,10)/100)+'\">')
invoice.document.write('<input type=\"hidden\" name=\"image_url\" value=\"https://static.e-junkie.com/sslpic/20163.649f469009d83765ada5086044b4c12e.jpg\">')
invoice.document.write('<input style=\"border: 3px ridge black\"type=\"image\" src=\"http://www.astroland.net/astropaypal.jpg\" border=0 name=\"submit\" alt=\"Click the Button to pay via Paypal\">')

invoice.document.write('</form>')
invoice.document.write('<td width=245 height=65 valign=middle style=\"font-weight:700;font-size:16px;border-bottom: 3px solid black;border-top: 3px solid black;border-right:3px solid black\"><font color=red>Click the Astroland-PayPal logo to be  taken to Paypal and pay now')
invoice.document.write('<tr><td valign=\"middle\"  colspan=3>&nbsp;')
invoice.document.write('<tr><td valign=\"middle\"  colspan=3><a style=\"color:navy\" href=\"javascript:window.close()\">Keep Shopping, Close this window</a></table>') 

}

}


function removefromcart(elem) {
iteration = 0 


var rem = document.getElementById(elem).rowIndex



if (doublestbl.rows[rem].cells[0].rowSpan > 1) {
rowspanflag=0
playernamecell=0
cardcell=1
pricecell=2
subtotalcell = 4} else {



if (doublestbl.rows[rem].cells[5]==undefined){
              rowspanflag=1
              playernamecell=0
              cardcell=0
              pricecell=1
              subtotalcell = 3
}
      else {rowspanflag=0
playernamecell=0
cardcell=1
pricecell=2
subtotalcell = 4}
}





if ("$"+doublestbl.rows[rem].cells[pricecell].firstChild.nodeValue==doublestbl.rows[rem].cells[subtotalcell].firstChild.nodeValue) {
invoiceline[i][0] = " " 
invoiceline[i][1] = " " 
invoiceline[i][2] = " " 

i = i - 1
w = w - 1

var removedprice = doublestbl.rows[rem].cells[pricecell].firstChild.nodeValue
doublestbl.rows[rem].cells[subtotalcell].firstChild.nodeValue= "   "

totalprice = (parseInt(totalprice*100,10) - parseInt(removedprice*100,10))/100
totalpricethispage = (parseInt(totalpricethispage*100,10) - parseInt(removedprice*100,10))/100
 



if (totalprice * 10 != Math.floor(totalprice*10)) {placeholder=""} 
if (totalprice * 10 == Math.floor(totalprice*10)) {placeholder="0"} 
if (totalprice == Math.floor(totalprice)) {placeholder=".00"} 
if (totalprice == 0) {placeholder=""} 
if (totalpricethispage * 10 != Math.floor(totalpricethispage*10)) {placeholderthispage=""} 
if (totalpricethispage * 10 == Math.floor(totalpricethispage*10)) {placeholderthispage="0"} 
if (totalpricethispage == Math.floor(totalpricethispage)) {placeholderthispage=".00"} 
if (totalpricethispage == 0) {placeholderthispage=""} 

if (i == -1) {makealltotalsrowsblank()} else
              {populatealltotalsrows()}


if (w == -1) {maketotalsrowsblank()} else
              {populatetotalsrows()}




}
else return


}


function invoicetocookie() {
if (i==-1) {return}
cookiename=new Array(i+1)
for (d=0; d <=i; d++) {
cookiename[d]=new Array(3) }

for (var cookieloop = 0; cookieloop <=i;cookieloop++) {


    for (var cookiefield = 0; cookiefield < 3; cookiefield++) {

    cookiename[cookieloop][cookiefield] = "invoiceline"+cookieloop+"field"+cookiefield
    createCookie(cookiename[cookieloop][cookiefield],invoiceline[cookieloop][cookiefield],1)
}
}
}

function removeall() {
pricecell = 3

i = i - w - 1
totalprice = (parseInt(totalprice*100,10) - parseInt(totalpricethispage*100,10))/100

 for (loop = 1; loop < document.getElementById('doubles').rows.length; loop++) {
if (document.getElementById('doubles').rows[loop].cells[1] == undefined){} else {

if (document.getElementById('doubles').rows[loop].cells[5] == undefined){
pricecell=3} else {pricecell=4}

document.getElementById('doubles').rows[loop].cells[pricecell].innerHTML = blankvar 

}}

clear()
maketotalsrowsblank()


if (totalprice * 10 != Math.floor(totalprice*10)) {placeholder=""} 
if (totalprice * 10 == Math.floor(totalprice*10)) {placeholder="0"} 
if (totalprice == Math.floor(totalprice)) {placeholder=".00"} 
if (totalprice == 0) {placeholder=""} 


populatealltotalsrows()


readinvcookies()

}


 function eraseallcookies() {
counter=-1

for  (a = 0; a < 40; a++) {
    for  (b = 0; b < 3; b++) {
cookiefieldname = 'invoiceline'+a+'field'+b
counter++
var x = readCookie(cookiefieldname)
createCookie(cookiefieldname,"",-1)

 if (!x) {break} }}


clear()
readinvcookies()	
}

