
function changeImage(filename)
{
  item_image.src = filename;
}

function draw_pic_thumbnail(sPic, sVerital)
{
	document.write("<img src='images/"+sPic+"' width='50'  ");
	document.write(" onClick=\"javascript:changeImage('images/"+sPic+"')\"  ");
	document.write(" onMouseOver=\"this.style.cursor = 'hand';\" onMouseOut=\"this.style.cursor = 'auto';\" >");
	if (sVerital=="Y") {
		document.write("<br><img src='images/empty.gif' height='4' border='0'><br>");
	} else {
		document.write("&nbsp;");
	}
}


//------------------------------------------------------------------------------------------------------
// var sform_url="https://www.sandbox.paypal.com/cgi-bin/webscr";
var sform_url="https://www.paypal.com/cgi-bin/webscr";
// var sbusiness_id="withde_1193184913_biz@yahoo.com";
var sbusiness_id="sonakius@yahoo.com";
var sType_ = new Array();
var sItemID_ = new Array();
var sItemTitle_ = new Array();
var sItemPrice_ = new Array();
var updateItemPrice_ = new Array();
var sPic_ = new Array();
var sLink_ = new Array();
var sDescription_ = new Array();

function draw_item_section(sType, sItemID, sItemTitle, sItemPrice, sPic, sLink, sDescription, productID, updatePrice, promotion_code)
{
	var sItemIDUpper=sItemID.toUpperCase();
	var sItemIDLower=sItemID.toLowerCase();

	sType_[productID] = sType;
	sItemID_[productID] = sItemID;
	sItemTitle_[productID] = sItemTitle;
	sItemPrice_[productID] = sItemPrice;
	var price = sItemPrice;
	var disPrice = "$" + sItemPrice;
	if(updatePrice != null && updatePrice != ""){
		price = updatePrice;
		disPrice = disPrice + " => " + "$" + price; 
	}
	sPic_[productID] = sPic;
	sLink_[productID] = sLink;
	sDescription_[productID] = sDescription;

	var sHtml = "";
	if(sType=="LINE") {
		sHtml = sHtml + "<table border='0' width='700'><tr>";
		sHtml = sHtml + "<td width='150'  align='center'>";
 	   	sHtml = sHtml + "<img src='../shop/images/"+sPic+"'>";
   		sHtml = sHtml + "</td><td width='350'  align='left'>";
   	 	sHtml = sHtml + "<font size='1' >"+sItemID+"</font><br><font size='4' color='#327fba' >"+sItemTitle+"</font>";
    		if (sDescription != "") {
    			sHtml = sHtml + "<br><font size='2' >"+sDescription+"</font>";
    		}
   		sHtml = sHtml + "</td><td width='200'>";
  	} else {
  		sHtml = sHtml + "<table border='0'  ><tr><td align='left'>";
   		sHtml = sHtml + "<font size='1' >"+sItemID+"</font><br><font size='4' color='#327fba' >"+sItemTitle+"</font>";
    		if (sDescription != "") {
    			sHtml = sHtml + "<font size='2' >"+sDescription+"</font>";
    		}
  		sHtml = sHtml + "</td></tr></table>";
  	}
   	
    sHtml = sHtml + "<table border='0' cellpadding='0' cellspacing='0'>";
   	sHtml = sHtml + "<tr>";
	if(sType=="LINE") {
		sHtml = sHtml + "<td width='200' align='right'>";
  	} else {
		sHtml = sHtml + "<td width='300' align='right'>";
	}
	sHtml = sHtml + "<form xtarget='paypal' action='"+sform_url+"' method='post'>";
	sHtml = sHtml + "<input type='image' src='images/viewcart_txt.jpg' border='0' name='submit' alt='Make payments with PayPal - it is fast, free and secure!'>";
	sHtml = sHtml + "<input type='hidden' name='cmd' value='_cart'>";
	sHtml = sHtml + "<input type='hidden' name='shopping_url' value='http://www.vitaminshower.net/shop/'>";
	sHtml = sHtml + "<input type='hidden' name='business' value='"+sbusiness_id+"'>";
	sHtml = sHtml + "<input type='hidden' name='display' value='1'>"; 	 			
	sHtml = sHtml + "</form>";
	sHtml = sHtml + "</td>";
	sHtml = sHtml + "</tr>";
	sHtml = sHtml + "</table>";

	if ((navigator.appName).indexOf('Microsoft')!=-1) {
	    sHtml = sHtml + "<table border='0' cellpadding='0' cellspacing='0'>";
		sHtml = sHtml + "<form xtarget='paypal' name='"+sItemID+"' action='"+sform_url+"' method='post'>";
	} else {
		sHtml = sHtml + "<form xtarget='paypal' name='"+sItemID+"' action='"+sform_url+"' method='post'>";
	    sHtml = sHtml + "<table border='0' cellpadding='0' cellspacing='0'>";
	}

    sHtml = sHtml + "<tr><td colspan='3' align='left'><font color='red' size='3'><b>"+disPrice+"</b></font></td></tr>";
	sHtml = sHtml + "<tr>";
    sHtml = sHtml + "<td align='left'>Quantity : </td><td align='left'> <input type='text'  size='2' name='quantity' value='1' ></td>";
 	sHtml = sHtml + "<td><input type='image' src='https://www.paypal.com/en_US/i/btn/btn_cart_SM.gif' border='0' name='submit' alt='Make payments with PayPal - it is fast, free and secure!' ></td>";
	sHtml = sHtml + "<input type='hidden' name='add' value='1'>";
   	sHtml = sHtml + "<input type='hidden' name='cmd' value='_cart'>";
   	sHtml = sHtml + "<input type='hidden' name='business'       value='"+sbusiness_id+"'>";
   	sHtml = sHtml + "<input type='hidden' name='item_name'    value='"+sItemTitle+"'>";
   	sHtml = sHtml + "<input type='hidden' name='item_number'    value='"+sItemID+"'>";
   	sHtml = sHtml + "<input type='hidden' name='amount' value='"+price+"'>";
   	sHtml = sHtml + "<input type='hidden' name='undefined_quantity' value='1'>";
   	sHtml = sHtml + "<input type='hidden' name='buyer_credit_promo_code' value=''>";
   	sHtml = sHtml + "<input type='hidden' name='buyer_credit_product_category' value=''>";
   	sHtml = sHtml + "<input type='hidden' name='buyer_credit_shipping_method' value=''>";
   	sHtml = sHtml + "<input type='hidden' name='buyer_credit_user_address_change' value=''>";
	sHtml = sHtml + "<input type='hidden' name='shopping_url' value='http://www.vitaminshower.net/shop/'>";
	sHtml = sHtml + "<input type='hidden' name='return' value='http://www.vitaminshower.net/shop/'>";
	sHtml = sHtml + "<input type='hidden' name='cancel_return' value='http://www.vitaminshower.net/shop/'>";
	sHtml = sHtml + "<input type='hidden' name='no_shipping' value='0'>";
	sHtml = sHtml + "<input type='hidden' name='no_note' value='1'>";
	sHtml = sHtml + "<input type='hidden' name='currency_code' value='USD'>";
	sHtml = sHtml + "<input type='hidden' name='tax' value='0'>";
	sHtml = sHtml + "<input type='hidden' name='bn' value='PP-ShopCartBF'>";
 	sHtml = sHtml + "</tr>";
	sHtml = sHtml + "</form>";
	sHtml = sHtml + "</table>";
			   		 	 		
    sHtml = sHtml + "<table border='0' cellpadding='0' cellspacing='0'>";
   	sHtml = sHtml + "<tr><td>";
	if(sType=="LINE") {
	    sHtml = sHtml + "Promotional Code : <input type='text' name='promo_code' id= promo_code"+ productID +" size='7'> ";
		sHtml = sHtml + "</td></tr><tr><td align='right' >";
  	} else {
	    sHtml = sHtml + "Promotional Code : <input type='text' name='promo_code' id= promo_code"+ productID +" size='9'> ";
		sHtml = sHtml + "</td><td>";
	}
    sHtml = sHtml + "<input type='button' value='Update Price' onclick='update_price("+ productID + ")'>";
	sHtml = sHtml + "</td>";
	sHtml = sHtml + "</tr>";
	sHtml = sHtml + "</table>";


   	if(sType=="LINE") {
		sHtml = sHtml + "</td>";
  		sHtml = sHtml + "</tr>";
		sHtml = sHtml + "</table>";
  	}

	document.getElementById("product"+ productID).innerHTML = sHtml;

	if(promotion_code != null){
		document.getElementById("promo_code"+ productID).value = promotion_code;
	}
}

function update_price(productID)
{
	var promo_code_obj = document.getElementById("promo_code"+ productID);
	var promo_rate = 0;
	updateItemPrice_[productID] = "";

	if(promo_code_obj.value != '' && promo_code_obj.value != null){
		var errflag = true;
		for(i=0; i<promoCodeList.length; i++){
			if(promoCodeList[i] == promo_code_obj.value){
				promo_rate = promoCodeRate[i];
				errflag = false;
				break;
			}
		}
		if(errflag == true){
			alert("Promotion Code is not correct.");
			return;
		}


		var promoPrice = sItemPrice_[productID] * (promo_rate * 0.01);
		updateItemPrice_[productID] = leadingZeros(sItemPrice_[productID] - promoPrice, 2, false) ;

		var time = getTimeStamps();
		var sFileName = "../log/" + promo_code_obj.value + "_" + sItemID_[productID] + "_" +time + ".csv";
		var sFileData = 
			sItemID_[productID] + "," + 
			sItemTitle_[productID] + "," + 
			sItemPrice_[productID] + "," +
			updateItemPrice_[productID] + "," +
			promo_code_obj.value + "," +
			promo_rate + "," +
			time;

		document.getElementById("dynamic").src ="outputLog.php?fileName="+sFileName+"&fileData="+sFileData;
	}

	draw_item_section( 
		sType_[productID], 
		sItemID_[productID], 
		sItemTitle_[productID], 
		sItemPrice_[productID], 
		sPic_[productID], 
		sLink_[productID], 
		sDescription_[productID],
		productID, 
		updateItemPrice_[productID],
		promo_code_obj.value);
	
}


function getTimeStamps(){
	var d = new Date();
	var system_time = 
	leadingZeros(d.getFullYear(), 4, true) + "-" +
	leadingZeros(d.getMonth() + 1, 2, true) + "-" + 
	leadingZeros(d.getDate(), 2, true) + "-" +
	leadingZeros(d.getHours(), 2, true) + "-" + 
	leadingZeros(d.getMinutes(), 2, true) + "-" + 
	leadingZeros(d.getSeconds(), 2, true);

	return system_time;
}

function leadingZeros(n, digits, isTime){
	var zero = '';
	n = n.toString();

	if(isTime){
		if(n.length < digits){
			for(i = 0; i < digits - n.length; i++){
				zero += '0';
			}
		}
	}else{
		var nArray = n.split(".");
		if(nArray.length == 1){
			n = n + ".00";
		}else{
			if(nArray[1].length == 1){
				n = n + "0";
			}
		}
	}

	return zero + n;
}

function draw_cart_view(sWithTD)
{
 		document.write("<form xtarget='paypal' action='"+sform_url+"' method='post'>");
 		if (sWithTD!="") document.write("<td align='right' >");
 		document.write("<input type='hidden' name='cmd' value='_cart'>");
 		document.write("<input type='hidden' name='shopping_url' value='http://www.vitaminshower.net/shop/'>");
 		document.write("<input type='hidden' name='business' value='"+sbusiness_id+"'>");
 		document.write("<input type='image' src='images/view_cart.jpg' border='0' name='submit' alt='Make payments with PayPal - it is fast, free and secure!'>");
 		document.write("<input type='hidden' name='display' value='1'>");
 		if (sWithTD!="") document.write("</td>");
 		document.write("</form>");
}

//------------------------------------------------------------------------------------------------------