var gCategorieID = 0;
var vHttp = getHttpRequest();

/*
if (window.XMLHttpRequest) { 
	vHttp = new XMLHttpRequest();
}
else if (window.ActiveXObject){
	vHttp = new ActiveXObject("Microsoft.XMLHTTP");
}*/

function doGetLoginPassword(inObjectID, inTransaction){
	vRequest =  "getCommandCount.php?command=getLoginPassword&transactionid="+inTransaction;
	vHttp.open("GET", vRequest, false);
	vHttp.send(null);
	
	vElem = document.getElementById(inObjectID);
	var vProductObj = vHttp.responseText.split(";");
	var vHTML = "";
	if (vProductObj[0]=='END'){
		vHTML = "Thank you, we are currently handling your order.<br>You will receive an email with your purchased serials.<br>Please allow up to 72 hours to arrive... and don't forget to check your junk mail.";
	}else{
		vHTML = "<table><tr><td class='labelDisplay'>Login:</td><td class='valueDisplay'>";
		vHTML += vProductObj[0]+"</td></tr><tr><td class='labelDisplay'>Password:</td><td class='valueDisplay'>"+vProductObj[1]+"</td></tr></table>";
		vHTML +="<br>You can access your serial number by logging into your newly created account from the <a href='./perso/index.php'>'My Recipe'</a> area<br>";
	}
	vElem.innerHTML = vHTML; 	
}

function doGetSerialFromTransactionID(inObjectID, inTransaction){
	vElem = document.getElementById(inObjectID);	
	if (inTransaction == "BAD"){
		vElem.innerHTML = "<br>";
		vElem = document.getElementById('loginPassword');	
		vElem.innerHTML = "Operation Canceled<br>"; 	
		return false;
	}else{
	 vRequest =  "getCommandCount.php?command=getSerial&transactionid="+inTransaction;
	 vHttp.open("GET", vRequest, false);
	 vHttp.send(null);
	 
	 
	 if (vElem!=null){
		 var vHeight = 40;
		 var vTop = 0;
		 var vHttpReq = vHttp.responseText;
		 if (vHttpReq == "NotAllowed"){
				 
		 }else{
				 var vProductList = vHttpReq.split("|");
				 var vNbProduct = vProductList.length-1;
				 var vHTML = "";
				 if (false){
					 vHTML += "<div>";
					 for (var i=0; i<vNbProduct; i++)
					 {	 
						 var vProductObj = vProductList[i].split(";");
						 var vProductName = vProductObj[0];
						 var vProductSerial = vProductObj[1];
						 var vIdLogiciel = vProductObj[2];
						 if (vProductName!="END"){
							 vHTML += "<div style='position: absolute; top: "+vTop+"px; left: 0px;' >";
							  
							 vHTML +="<div style='position: absolute; top: 4px; left: 10px;' >";
							 vHTML +="<img width=30 src=./images/"+vIdLogiciel+"/"+vIdLogiciel+"_icone.jpg>";
							 vHTML +="</div>";
							 
							 vHTML +="<div style='position: absolute; top: 13px; left: 55px; width:70px; font-family:arial;font-size:10px;font-weight:800;' >";
							 vHTML += vProductName;
							 vHTML +="</div>";
							 
							 vHTML +="<div style='position: absolute; top: 13px; left: 125px; width:150px; font-family:arial;font-size:10px;font-weight:800;' >";
							 vHTML += vProductSerial;
							 vHTML +="</div>";
							  
							 vHTML +='</div>';
						 } 
						 vTop = vTop + vHeight;	 	
					 }
					 vHTML += "</div>";
				 }else{
					 vHTML += "<table>";
					 for (var i=0; i<vNbProduct; i++)
					 {
						 var vProductObj = vProductList[i].split(";");
						 var vProductName = vProductObj[0];
						 var vProductSerial = vProductObj[1];
						 var vIdLogiciel = vProductObj[2];
						 if (vProductName!="END"){
							 vHTML += "<tr>";
							  
								 vHTML +="<td rowspan=2 class='iconDisplay'>";
								 vHTML +="<img width=30 src=./images/"+vIdLogiciel+"/"+vIdLogiciel+"_icone.jpg>";
								 vHTML +="</td>";
								 
								 vHTML +="<td class='nameDisplay'>";
								 vHTML += vProductName;
								 vHTML +="<td>";
							 vHTML +='</tr>';
							 vHTML += "<tr>";
								 vHTML +="<td class='idDisplay'>";
								 vHTML += vProductSerial;
								 if ((vIdLogiciel == '114') || (vIdLogiciel == '116')){
								 	vHTML += "<br>-------------------------------- <br> You will need to go to our website and register the serial: <br> <a href='http://www.softpress.com/support/register.php'>http://www.softpress.com/support/register.php</a> <br>";
								 	vHTML += "And then download the latest update:<br> <a href='http://www.softpress.com/support/updates.php'>http://www.softpress.com/support/updates.php</a> <br> --------------------------------<br>";
		
								 }
								 vHTML +="</td>";
							 vHTML +='</tr>';
							 vHTML += "<tr style='height:20px'>";
							 vHTML +="<td></td><td></td>";
							 vHTML +='</tr>';
						 }	 
						 vTop = vTop + vHeight;	 	
					 }
					 vHTML += "</table>";
		 
				 }
		 }
		 vElem.innerHTML = vHTML; 
	 }
	 	return true;
	} 
}

function doGetCommandsCount(){
			
	vRequest =  "getCommandCount.php?command=getCommandCount";
	
	vHttp.open("GET", vRequest, false);
	vHttp.send(null);
	
	vElem = document.getElementById("commandCounter");
	if (vElem!=null){
		vElem.innerHTML = vHttp.responseText; 
	}
	
}

function doGetIngredientList(inQueryID){
	
	/*var vDevise = CURRENCY;//getCurrency();
	if (vDevise=="USD")
		vDevise=2;
	else
		vDevise=1;
	vRequest =  "getCommandCount.php?command=getIngredientList&query="+inQueryID+"&devise="+vDevise;
	
	vHttp.open("GET", vRequest, false);
	vHttp.send(null);
	
	vElem = document.getElementById("div2");
	if ( (vElem!=null) && (vHttp.responseText.indexOf("[VIDE]",0)==-1) ){
		vElem.innerHTML = vHttp.responseText; 
		return true;
	}else
		return false;*/
		
	return false;
}



function updateProductCounter(){
	var gNbProducts = document.getElementById('nbProducts'); 
	var vNbProducts = 0;
	if (gNbProducts != null)
		vNbProducts = gNbProducts.value; 
	return vNbProducts;
	return 0;
}

function getElementsByClass(searchClass,node,tag) {

	var classElements = new Array();
	if (node == null)
		node = document;
	if (tag == null)
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	var j = 0;
	for (i = 0; i < elsLen; i++) {
		if (pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function getCurrency(){
	var vObj = document.getElementById("currency_code");
	if (vObj){
		return vObj.value;
	}else
		return "";
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

/*
function formatCurrency(num, inCurrency, inUnity) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));

	if (inCurrency == ""){
		return (((sign)?'':'-') + num + '.' + cents);
	}else
	if (inCurrency == "EUR")
		return (((sign)?'':'-') + num + '.' + cents + ' '+inUnity);
	else
		return (((sign)?'':'-') + inUnity + num + '.' + cents);
}
*/

function view_list_software_caller(inCatID){
	if (!doGetIngredientList(inCatID))
		viewListSoftware(inCatID);
		
	var vProductList = getElementsByClass("product", null, null);
	var vNbElements = vProductList.length;
	for ( i=0;i<vNbElements; i++ ){	
		showHideAddButton(vProductList[i].innerHTML);
	}
}

/*
function updateAllPrice(inNbProducts){

	var vProductCurPriceList = getElementsByClass("current_prix", null, null);
	var vProductPriceList = getElementsByClass("prix", null, null);
	var vNbElements = vProductPriceList.length;
	if (inNbProducts < 3){
		view_list_software_caller(gCategorieID);
		return;
	}
	
	for (i=0;i<vNbElements; i++){	
		var vCurPriceStr = "";
		var vPriceStr = "";
		
		if (getCurrency()=="EUR"){
			vCurPriceStr = vProductCurPriceList[i].innerHTML.split(" ");
			vPriceStr = vProductPriceList[i].innerHTML.split(" ");
			vIndice = 0;
		}else{
			vCurPriceStr = vProductCurPriceList[i].innerHTML.split("$");
			vPriceStr = vProductPriceList[i].innerHTML.split("$");
			vIndice = 1;
		}	
		
		var vValue = vCurPriceStr[vIndice];
		var vOldValue = vValue;
		vValue++;
		vValue--;
		vColor = "#000000";
		if (inNbProducts>9){
			vValue = vValue - (vValue*70/100);
			vColor = "#AA5555";
		}else
		if (inNbProducts>6){
			vValue = vValue - (vValue*50/100);
			vColor = "#AA5555";
		}else
		if (inNbProducts>4){
			vValue = vValue - (vValue*40/100);
			vColor = "#AA5555";
		}else
		if (inNbProducts>2){
			vValue = vValue - (vValue*30/100);
			vColor = "#AA5555";
		}
		
		if (vValue != vOldValue){	
			
			//vValue = roundNumber(vValue, 2);
			
			if (getCurrency()=="EUR"){
				vProductPriceList[i].innerHTML = formatCurrency(vValue, getCurrency(), '&euro;');
			}else{
				vProductPriceList[i].innerHTML = formatCurrency(vValue, getCurrency(), '$');
			}		
			
			vProductPriceList[i].style.top = "15px";
			vProductCurPriceList[i].style.fontSize = "10px";
			vProductCurPriceList[i].style.color = vColor;
			vProductCurPriceList[i].style.textDecoration = "line-through";
			vProductCurPriceList[i].style.top = "5px";
		}else{
			view_list_software_caller(gCategorieID); 
			i=vNbElements;
		}
	}
	gOldCurrency = getCurrency();
}*/

function setCategory(inNewCat){
	gCategorieID = inNewCat;
	view_list_software_caller(inNewCat);
	//updateAllPrice(updateProductCounter());
}

function showHideAddButton(inCode){
//	alert( "show/hide");
	var vItem = document.getElementById("qty_"+inCode);
	var vAddBt = document.getElementById('bAddProd'+inCode);
	if( vItem ) {
		if( vAddBt ) {
			vAddBt.style.visibility='hidden';
			vAddBt.parentNode.style.color = "#999";
		}
	} else {
		if( vAddBt ) {
			vAddBt.style.visibility='visible';
			vAddBt.parentNode.style.color = "";
		}
	}
	
	
	
	/*if (vItem==null){
		vItem = document.getElementsByName("qty_"+inCode);
		vItem = vItem[0];
	}*/
		
	
	
/*	if (vAddBt==null){
		vAddBt = document.getElementsByName("bAddProd"+inCode);
		vAddBt = vAddBt[0];
	}*/
	
	/*
	
	var vNbProductInCart=0;
	if (vItem){
		vNbProductInCart = Number( vItem.contentText );
		//vNbProductInCart++; vNbProductInCart--;
	}
	
	if (vAddBt!=null){
		if ( vNbProductInCart > 0 ){
			vAddBt.style.visibility='hidden';
		}else{
			vAddBt.style.visibility='visible';
		}
	}
	return vNbProductInCart; */
	return 0;
}

function hideAddButton(inCode) {
	var vAddBt = document.getElementById('bAddProd'+inCode);
	if( vAddBt ) {
		vAddBt.style.visibility = "hidden";
		vAddBt.parentNode.style.color = "#999";
	}
}

function add_product(inCodeProduct){
	var vNbProducts = updateProductCounter();
	var vNbProductInCart = showHideAddButton(inCodeProduct);
//	if ( vNbProductInCart<0 ) { // security test;
		//xajax_buy_software(inCodeProduct);
		
		addToCart(inCodeProduct);
		
		
		vNbProducts++;
		//updateAllPrice(vNbProducts);
	//}
	vNbProductInCart=showHideAddButton(inCodeProduct);
}

function remove_product(inCodeProduct){
	var vNbProducts = updateProductCounter();
	var vNbProductInCart=showHideAddButton(inCodeProduct);
	if (vNbProductInCart>=0){
		removeToCart(inCodeProduct);
		vNbProducts--;
		//updateAllPrice(vNbProducts);
		vNbProductInCart=showHideAddButton(inCodeProduct);
	} 
}

function setOpacity(inObj, inOpacity) {
	inObj.style.opacity = (inOpacity / 100);
	inObj.style.MozOpacity = (inOpacity / 100);
	inObj.style.KhtmlOpacity = (inOpacity / 100);
	inObj.style.filter = 'alpha(opacity=' + inOpacity + ')';
}

function fadeImage(inImage, inOpacity, inSpeed, inPause, inCaption) {
	setOpacity(inImage,inOpacity);
	continueFadeImage(inImage, inOpacity, inSpeed, inPause, inCaption);
}

function startBlending(inImage, inSpeed, inPause, inCaption) {
	inImage.style.display = 'block';
	inImage.style.top = '-300px';
	
	continueFadeImage(inImage, 0, inSpeed, inPause, inCaption);
}

function continueFadeImage(inImage, ioOpacity, inSpeed, inPause, inCaption) {
	ioOpacity = ioOpacity + 3;
	if (ioOpacity < 103) {
		setTimeout(function() {fadeImage(inImage, ioOpacity, inSpeed, inPause, inCaption)}, inSpeed);
	} else {
	}
}

function opacity(id, opacStart, opacEnd, millisec) { 
	//speed for each frame 
	var speed = Math.round(millisec / 100); 
	var timer = 0; 

	//determine the direction for the blending, if start and end are the same nothing happens 
	if(opacStart > opacEnd) { 
		for(i = opacStart; i >= opacEnd; i--) { 
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
			timer++; 
		} 
	} else if(opacStart < opacEnd) { 
		for(i = opacStart; i <= opacEnd; i++) 
			{ 
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
			timer++; 
		} 
	} 
} 

//change the opacity for different browsers 
function changeOpac(opacity, id) { 
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100); 
	object.MozOpacity = (opacity / 100); 
	object.KhtmlOpacity = (opacity / 100); 
	object.filter = "alpha(opacity=" + opacity + ")"; 
}

function shiftOpacity(id, millisec) { 
	//if an element is invisible, make it visible, else make it ivisible 
	vObj = document.getElementById(id);
	if (vObj){
	   if(vObj.style.opacity == 0) { 
			vObj.parentNode.style.display = 'block';
			vObj.parentNode.style.position = 'relative';
			vObj.parentNode.style.top = '-1000px';
		   opacity(id, 0, 100, millisec); 
		   document.getElementById('productList').style.overflow='hidden';
	   } else { 
		   opacity(id, 100, 0, millisec); 
		   document.getElementById('productList').style.overflow='auto';
		   vObj.parentNode.style.display = 'none';
		   vObj.parentNode.style.top = '0px';
	   } 
	}
}

function shiftOpacity_cmForm(millisec) {
	vObj = document.getElementById('cybermutForm');
	if (vObj) {
	   if(vObj.style.opacity == 0) { opacity('cybermutForm', 0, 100, millisec); } 
	   else { opacity('cybermutForm', 100, 0, millisec); }
	}
}

function moveContent(id){
	vObj = document.getElementById(id);
	if (vObj){
		if(vObj.style.opacity != 0) { 
		  vObj.parentNode.style.display = 'block';
		  //vObj.parentNode.style.position = 'relative';
		  vObj.parentNode.style.top = '-700px';
		}
	}
}
	
function cook(){
	//shiftOpacity('freeBonus', 1000);
	
	var vNote = document.getElementById('note');
	if (vNote){
		vNote.submit();
	}
}

function doCooking(inAddColadia){
	document.getElementById('addColadia').value=inAddColadia;
	var vNote = document.getElementById('note');
	if (vNote){
		vNote.submit();
	}
}

function showHideObjectsByName(inNameToHide, inNameToShow){
	var vObjListToHide = document.getElementsByName(inNameToHide);
	var vObjListToShow = document.getElementsByName(inNameToShow);
	var vCount = vObjListToHide.length;
	for (i=0;i<vCount;i++){
		vObjListToHide[i].style.visibility = "hidden";
	}
	var vCount = vObjListToShow.length;
	for (i=0;i<vCount;i++){
		vObjListToShow[i].style.visibility = "visible";
		
		var vId = i+1;
		var vObjDest = document.getElementById('amount_'+vId);
		
		if (inNameToShow.indexOf('Euro')!=-1)
			vObjSrc = document.getElementById('prix_euro_'+vId);
		else
			vObjSrc = document.getElementById('prix_dollar_'+vId);

	//	if (vObjSrc && vObjDest)
		//	vObjDest.value = vObjSrc.innerHTML;
			
	}
}

function updateProductListState() {
	var cart = document.getElementById( "subCartHome" );
	var td = cart.getElementsByTagName( "td" );
	for( var i=0;i<td.length;i++ ) {

		if( td[i].id && td[i].id.indexOf("qty_") != -1 ) {
			var id = td[i].id.replace("qty_", "");
			hideAddButton( id );
		}
	}

}

function setCurrency(inCurrency, inIndexPage, inchemin1, inchemin2){

//	MM_swapImage('dollar','',inchemin1,1);
//	MM_swapImage('euro','',inchemin2,1);
	var currencyChooser = document.getElementById('currencyChooser');
	if (inCurrency == 'dollar'){	
		updateCurrency( "USD" );
		getCart();
		setCategory(CURRENT_CATEGORY);
	} else {		
		updateCurrency( "EUR" );
		getCart();
		setCategory(CURRENT_CATEGORY);
	}			
}


function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; 
  document.MM_sr=new Array;
  for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){
   		document.MM_sr[j++]=x;
   		if(!x.oSrc)
   			x.oSrc=x.src;
   		x.src=a[i+2];
   	}
}

function MM_swapImage_categories(nom_categorie) { //v3.0
	
	var navArray = new Array("all","multimedia","web","productivity","others","top_10_softs","top_5_bundles");

	for (var i=0; i < navArray.length; i++) {
		var nom_categorie_off="images/categories_"+navArray[i]+".jpg";	
	
		document.getElementById(navArray[i]).src=nom_categorie_off;
		// alert("ok");					
	}
	

	
	// alert(nom_categorie);
	
	// document.getElementById(nom_categorie).style.display='none';
	var nom_categorie_on="images/categories_"+nom_categorie+"_on.jpg";
	
	// alert(nom_categorie_on);
	
	document.getElementById(nom_categorie).src=nom_categorie_on;
	
	// document.images[nom_categorie].src='images/home.jpg';
}

/* UTILS */
// send email:
function showEmailForm() {
	var vElem = document.getElementById('information');
	var vHTML = '<div id="emailForm"><form name= "emailtofriend" action="../mail.php" method="post" ><p>Tell a friend about it !</p><p>Your name:* <br><input type="text" size="20" maxlength="100" name="name" value="" /><br>Your email:*<br><input type="text" size="20" maxlength="100" name="from_addy" value="" /><br>Destination Name:*<br><input type="text" size="20" name="to_name" value="" /><br>Destination email(s):*<br><input type="text" size="20" name="to_addy" value="" /></p><p><strong><div id="submit"><a href="javascript:void(0)" onclick="emailtofriend.submit()">Submit</a></div><strong></p></form></div>';
	vElem.innerHTML = vHTML;
}
