/*
 * Adorable Doll Clothes
 * Copyright (c)2002-2009
 * Written By Steve Duncan
 *
 * This javascript builds a product page and has support for Pay Pal.
 *
 */


var i;
var debug = false;
var my_window="";
var NumberOfItems = 0;

//the product object constructor
function productDetails(title, description, number, size, price, inventory, picture)
{
    this.title = title;
	this.description = description;
	this.number = number;
	this.size = size;
	this.price = price;
	this.inventory = inventory;
	this.picture = picture;
	return this;
}


// GetItemDetails requires the file to be in the SKU folder with a .XML file extension
function GetItemDetails(myXmlFile)
{
    NumberOfItems++;
    
    myXmlFile = "..\\Sku\\" + myXmlFile + ".xml"; //Note: the xml element cannot be empty.
	
   	if (debug) document.write("<b>" + myXmlFile + "</b><br \>");
	
	if (document.implementation && document.implementation.createDocument)
	{
        var req = new XMLHttpRequest();
        req.open("GET", myXmlFile, false); 
        req.send(null);
        xmlDoc = req.responseXML;
        if (xmlDoc.documentElement.nodeName == "parsererror")
            return;
	}
    else if (window.ActiveXObject)
    {	
	    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    	xmlDoc.async=false;
	    xmlDoc.load(myXmlFile);    	
	}
	else
	{
	    alert('Please use Microsoft Internet Explorer when visiting our site.');
	    return;
	}
		         
	var myTitle=xmlDoc.getElementsByTagName("title");
	for (var i=0;i<myTitle.length;i++)
	{ 
	  this.title = myTitle[i].childNodes[0].nodeValue;
	  if (debug) document.write("Title: "+this.title + "<br \>");
	}

	var myDescription=xmlDoc.getElementsByTagName("description");
	for (var i=0;i<myDescription.length;i++)
	{ 
	  this.description = myDescription[i].childNodes[0].nodeValue;
	  if (debug) document.write("Description: " + this.description + "<br \>");
	}
  
	var mySku=xmlDoc.getElementsByTagName("sku");
	for (var i=0;i<mySku.length;i++)
	{ 
	  this.sku = mySku[i].childNodes[0].nodeValue;
	  if (debug) document.write("SKU: " + this.sku + "<br \>");
	}

	var mySize=xmlDoc.getElementsByTagName("size");
	for (i=0;i<mySize.length;i++)
	{
	  this.size = mySize[i].childNodes[0].nodeValue;
	  if (debug) document.write("Size: " + this.size + "<br />");
	} 

	var myPrice=xmlDoc.getElementsByTagName("price");
	for (i=0;i<myPrice.length;i++)
	{
	  this.price = myPrice[i].childNodes[0].nodeValue;
	  if (debug) document.write("Price: " + this.price + "<br />");
	}   
	    
	var myInventory=xmlDoc.getElementsByTagName("inventory");
	for (i=0;i<myInventory.length;i++)
	{
	  this.inventory = myInventory[i].childNodes[0].nodeValue;
	  if (debug) document.write("Inventory: " + this.inventory + "<br />");
	}   

	var myPicture=xmlDoc.getElementsByTagName("picture");
	for (i=0;i<myPicture.length;i++)
	{
	  this.picture = myPicture[i].childNodes[0].nodeValue;
	  if (debug) document.write("Picture: " + this.picture + "<br />");
	}   
	if (debug) document.write("<br /><br />");

    var itemDetails = new productDetails(this.title,this.description,this.sku,this.size,this.price,this.inventory,this.picture);      
	return itemDetails;
}


function buildXmlPage()
{
    document.write('<link rel="stylesheet" type="text/css" href="../products.css">\n');
    document.write("<title>" + myTitle + windowTitle + "</title>");
    document.write("<meta name='format-detection' content='telephone=no' />");
	window.status="Adorable Doll Clothes"+windowTitle;

	document.write("<table border='0' cellspacing='4' cellpadding='10' style='border-collapse: collapse' bordercolor='#111111' width='738'>");
	document.write("  <tr>");

	// display doll picture
	document.write("    <td bgcolor='#FFF2F9' width='247'>");
	document.write("      <p align='center'>");
	document.write("      <img src="+myPic+" alt='"+myTitle+windowTitle+"' border='1'>");
    document.write("  <div class='ProductSizes'>"+myPicSize+"</div>");
    document.write("  </p>");
	document.write("    </td>");

	// display subtitle
	document.write("    <td valign='top' bgcolor='#FFF2F9' width='439'>");
	document.write("      <table border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' id='AutoNumber4' height='46' width='440'>");
	document.write("        <tr>");
	document.write("          <td width='439' height='46'>");
    document.write("  <div class='ProductTitle'>"+myTitle+"</div>");
	document.write("    </td>");

    
	document.write("        </tr>");
	document.write("      </table>");

    if (NumberOfItems > 1)
        myDescription += " Each item sold separately."; // add to each description paragraph
     

	// display description paragraph
	document.write("<table border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse' id='AutoNumber2' width='440'>");
	document.write("  <tr>");
    document.write("    <td width='437'><div class='ProductDescription'>"+myDescription+"</div><br /></td>");
	document.write("  </tr>");
	document.write("</table>");

	// display Description, ItemNumber, Size, Price, Order Table
	document.write("<table border='1' cellpadding='1' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='440' id='AutoNumber1' height='64'>"); 
	document.write("  <tr bgcolor='#FF0080' height='36'>");	
    document.write("    <td width='160'><div class='ProductColumnHeading'>Description</div></td>");
    document.write("    <td width='64'><div class='SmallProductColumnHeading'>Item Number</div></td>");			
    document.write("    <td width='70'><div class='ProductColumnHeading'>Size</div></td>");    
	document.write("    <td width='70'><div class='ProductColumnHeading'>Price</div></td>");
    document.write("    <td width='70'><div class='ProductColumnHeading'>Order</div></td>");	
	document.write("  </tr>");

	// display item information in the table
	for (i=0; item[i] != null;i++)
	  displayItemInfo(item[i]);

	document.write("</table>");
	
	// display the ViewCart, OrderForm and Home buttons
	document.write("    <table border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' id='AutoNumber3'>");
	document.write("      <tr>");
	document.write("        <td align='center'>");
	document.write("          <br clear='all'>");

	//document.write("          <a href='https://www.paypal.com/cart/display=1&business=adc1140%40charter.net'>");
	document.write("          <a href=javascript:PayPalViewCartPopUp();>");
	document.write("          <img src='../gifs/viewCart.gif' border = '0' alt='Click here to view your shopping cart contents.' align='middle' width='92' height='24'></a>");

	document.write("          <a href='http://www.AdorableDollClothes.com'>");
	document.write("          <img src='../gifs/home.gif'  border = '0' alt='Click here to return to AdorableDollClothes.com.' align='middle' width='92' height='24'></a></td>");

	document.write("      </tr>");
	document.write("    </table>");

	document.write("  </tr>");
	document.write("</table>");

	document.write("<br><br><br>"); 	/* seperate banner ads from our content */
	NumberOfItems = 0;
}


function displayItemInfo(item)
{
	document.write("<tr height='24'>");
    document.write("  <td width='160'><div class='ItemLeft'>"+item.title+"</div></td>");			 
    document.write("  <td width='63'><div class='ItemCenter'>"+item.number+"</div></td>");			 
    document.write("  <td width='70'><div class='ItemCenter'>"+item.size+"</div></td>");			 
    document.write("  <td width='70'><div class='ItemCenter'>"+item.price+"&nbsp;</div></td>");
	
	document.write("  <td ><p>");

	//the next line of code jumps to paypals url, not what you really wanna do.
	//document.write("            <a href='https://www.paypal.com/cart/add=1&business=adc1140%40charter.net&item_name="+item.description+" ("+item.size+")&item_number="+item.number+"&amount="+item.price+"'>");

	//if you pass a passed string you better escape() it!!!!!!!

	//if the item is sold out then set the item.number to zero and don't display an order button.
	if (item.inventory > 0) {
	    var currentURL = document.location.href;

	  document.write("              <a href=javascript:PayPalAddToCartPopUp('"+escape(item.title)+"','"+escape(item.size)+"','"+escape(item.number)+"','"+escape(item.price)+"');>");
	  document.write("              <img src='../gifs/addtocart.gif' border='0' alt='Click here to add this item to your shopping cart.' align='middle' width='92' height='24'></a>");
	}
	else
	{
	  document.write("<p align='center'><b><font color='#FF0080'>Sold Out</font></b></p>");
	}
	document.write("  </td>");
	document.write("</tr>");
}


function getInternetExplorerVersion()
{
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer')
    {       
        var ua = navigator.userAgent;        
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");        
        if (re.exec(ua) != null)           
        rv = parseFloat(RegExp.$1);    
    }    
    return rv;
}


//this function pops up the paypal Add to Cart window
//this function does not like single quotes included in the strings it is passed.
function PayPalAddToCartPopUp(description, size, number, price) 
{
//    var ver = getInternetExplorerVersion();

//    if (ver == 8) 
//    {
//        // for ie8 only
//        //instead of calling popitup and just passing the url, lets use the workaround
//        var w_itemDescription = description + " (" + size + ")";
//        var w_itemNumber = number;
//        var w_itemAmount = price;
//        WorkAroundForPaypalContinueShoppingIE8Bug(w_itemDescription, w_itemNumber, w_itemAmount);
//    }
//    else

    //the work around is worse than the problem.

    {
        // for all browsers other than ie8 

        // this worked before paypals upgrade - 8/23/09
        // var paypal = "https://www.paypal.com/cart/add=1";

        // change for paypal upgrade!
        var paypal = "https://www.paypal.com/cgi-bin/webscr?cmd=_cart&add=1";

        var adc = "&business=info%40adorabledollclothes.com";
        var itemDescription = "&item_name=" + description + " (" + size + ")";
        var itemNumber = "&item_number=" + number;
        var itemAmount = "&amount=" + price;
        popitup(paypal + adc + itemDescription + itemNumber + itemAmount, "cartwin", "toolbar=no, scrollbars=yes, resizable=yes");
    }
}


//this function pops up the paypal view cart window
function PayPalViewCartPopUp() 
{
    // this worked before paypals upgrade - 8/23/09 
    //	var paypal = "https://www.paypal.com/cart/display=1";

    // change for paypal upgrade!
    var paypal = "https://www.paypal.com/cgi-bin/webscr?cmd=_cart&display=1";
    
	var adc = "&business=info%40adorabledollclothes.com";
	popitup(paypal + adc, "cartwin", "toolbar=no, scrollbars=yes, resizable=yes");
}

function closepopup()
{
    if (my_window != null)
    {
         if ( my_window.closed == false ) 
         {
            my_window.close ();
         }
     }
 }


 function popitup(url) {
 
//    if ((my_window != null) && (my_window.closed == false) && (my_window.location)) {
//		my_window.location.href = url;
//	}
//	else {
//	    my_window=window.open(url,'name',"toolbar=no, scrollbars=yes, resizable=yes");
//		if (!my_window.opener) my_window.opener = self;
//	}
//	if (window.focus) {my_window.focus()}

// 7-29-11 Popups are being blocked so just goto the url
     location.href = url;

  return false;
}


// resolves "open a new browser window" issue continue shopping button on IE8
function WorkAroundForPaypalContinueShoppingIE8Bug(itemDescription, itemNumber, itemAmount) {
    document.write("<form name='frm' target='_self' action='https://www.paypal.com/cgi-bin/webscr' method='post>");
    document.write("<input type='image' src='' border='0' name='submit' alt='This is my paypal submit button'/>");
    document.write("<input type='hidden' name='add' value='1'/>");
    document.write("<input type='hidden' name='cmd' value='_cart'/>");
    document.write("<input type='hidden' name='shopping_url' value='http://www.adorabledollclothes.com'/>");
    document.write("<input type='hidden' name='business' value='info@adorabledollclothes.com'/>");
    document.write("<input type='hidden' name='item_name' value='" + itemDescription + "'/>");
    document.write("<input type='hidden' name='item_number' value='" + itemNumber + "'/>");
    document.write("<input type='hidden' name='amount' value='" + itemAmount + "'/>");
    document.write("</form>");
    document.frm.submit();
}


function setCookie(name,value)
{
	var expires = new Date();
	expires.setTime(expires.getTime()+24*60*60*30*1000);
	var expiryDate = expires.toGMTString();
	document.cookie= name + "=" + value + "; expires=" + expiryDate;
}

function getCookie(name)
{
	var cookieFound = false;
	var start=0;
	var end=0;
	var cookieString = document.cookie;

	var i=0;
	//scan for the cookie name
	while(i<=cookieString.length)
	{
	start=i;
	end=start + name.length;
	if (cookieString.substring(start,end) == name)
	{
	  cookieFound=true;
	  break;
	}
	i++;
	}
	if (cookieFound)
	{
	start = end+1;
	end = document.cookie.indexOf(";",start);
	if (end < start)
	  end = document.cookie.length;
	return document.cookie.substring(start,end);
	}
	return "";  
}

