// JavaScript Document
function search_products(type,value,page)
{

	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	} 
	
	// store the action data for history
	 var complexObject = new Object();
		 complexObject.action = 'search'
		 complexObject.type = type
         complexObject.value = value
		 complexObject.page = page
	
		
	 dhtmlHistory.add(""+num_count+"",complexObject);

	 num_count++

	var url="retailerSearch.php";
	url=url+"?type="+type;
	url=url+"&value="+value;
	url=url+"&page="+page;

	
	xmlHttp.onreadystatechange=show_products;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);


}

function search_history(type,value,page)
{
	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	} 
	
	var url="retailerSearch.php";
	url=url+"?type="+type;
	url=url+"&value="+value;
	url=url+"&page="+page;
	
	xmlHttp.onreadystatechange=show_products;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}





function show_enlarge_products(id,current,collection,extension,type,value)
{


	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	} 
	
		// store the action data for history
	 var complexObject = new Object();
		 complexObject.action = 'enlarge'
		 complexObject.id = id
		 complexObject.current = current
         complexObject.collection = collection
		 complexObject.extension = extension
		 complexObject.type = type
		 complexObject.value = value
		
	 dhtmlHistory.add(""+num_count+"",complexObject);

	
	num_count++
	
	document.getElementById("show_products").innerHTML="<table width='540' height='350'><tr align='center'><td><img src='images/loading_animate.gif' /></td></tr></table>"
	
	var url="enlarge_product.php";
	
	url=url+"?id="+id;
	url=url+"&current="+current;
	url=url+"&collection="+collection;
	url=url+"&extension="+extension;
	url=url+"&type="+type;
	url=url+"&value="+value;
	xmlHttp.onreadystatechange=show_products;	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}

function show_enlarge_history(id,current,collection,extension,type,value)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	} 	
	var url="enlarge_product.php";
	
	url=url+"?id="+id;
	url=url+"&current="+current;
	url=url+"&collection="+collection;
	url=url+"&extension="+extension;
	url=url+"&type="+type;
	url=url+"&value="+value;
	xmlHttp.onreadystatechange=show_products;	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


function update_portfolio_items(user_id,item_id)
{
	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	} 

	
	var url="update_portfolio.php";
	url=url+"?user_id="+user_id;
	url=url+"&item_id="+item_id;
	
	xmlHttp.onreadystatechange=show_update_portfolio; 
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}


function remove_item(id,current,type,value)
{
	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	} 
	
	var url="remove_item.php";
	url=url+"?id="+id;
	url=url+"&current="+current;
	url=url+"&type="+type;
	url=url+"&value="+value;
	
	xmlHttp.onreadystatechange=show_portfolio_update; 
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}


function clear_portfolio(clearType)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	} 
	
	var getConfirm= confirm("Are you sure you wish to delete all your previously selected items");
	
	if(getConfirm == true)
	{
	
		var url="clear_portfolio.php";
		url=url+"?type="+clearType;
		xmlHttp.onreadystatechange=show_portfolio; 
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}


function show_portfolio()
{
	
	if (xmlHttp.readyState==4)
	{
		document.getElementById("shopping_items").innerHTML = '(0) '
		document.getElementById("show_products").innerHTML = xmlHttp.responseText;
	}
}


function show()
{

	document.getElementById("show_products").innerHTML = ''
}

function zip_portfolio()
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	} 
	
	var url="zip_portfolio.php";
	xmlHttp.onreadystatechange=show_zip; 
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


function show_zip()
{
	
 	if (xmlHttp.readyState==4)
	{
 		var path = xmlHttp.responseText;
		document.getElementById("show_products").innerHTML='<table width="540"  height="390" border="0" cellpadding="3" cellspacing="0"><tr valign="top" align="center"><td><a href='+path+' onClick="congratuation()">Click here to download</a></td></tr></table>';
	}
	
}



function show_update_portfolio()
{
	
	if (xmlHttp.readyState==4)
 	{
		var message = xmlHttp.responseText
		
		var message_array = message.split(".")
	
		//alert("The message is "+message_array[0])
	
		if(message_array[0] =='done')
		{
			var toShow = 'portfolio_edit_'+message_array[2]
			
			document.getElementById("shopping_items").innerHTML ='('+message_array[1]+')'; 
			document.getElementById(toShow).innerHTML = '<span class="VrdRdSm"><strong> *Added </strong></span>'
		
			//document.getElementById("show_products").innerHTML = message_array[0];
		}
		else if(message_array[0] =='duplicated')
		{
			alert("The item is already in your Items To Download list");
			//alert(message_array[1]);
		}
		else if(message_array[0] =='')
		{
			//alert(message_array[1])	
		}
		else
		{
			alert("We're sorry, the maximum of downloads at one time is 10.\nPlease either download your selections (click 'Download Items' in top right) or click Empty Download to continue.");
			//alert(message_array[1]);
		}
	}
}


function download_portfolio()
{
	var check =document.getElementById("agree_terms").checked;
	var msg = '**********************************************<br />Please agree to our terms & conditions to Proceed<br /> **********************************************<br />'  
	 
	 if(check)
	  {
			
			xmlHttp=GetXmlHttpObject()
			if (xmlHttp==null)
  			{
  				alert ("Your browser does not support AJAX!");
  				return;
  			} 
	
			var url="show_portfolio.php";
			xmlHttp.onreadystatechange=show_products; 
			xmlHttp.open("GET",url,true);
			xmlHttp.send(null);  
		
  }
	  else
	  document.getElementById("showMessage").innerHTML = msg
			
			
}


function call_search()
{
	var type='search'
	var value = document.getElementById('search_keyword').value
	search_products(type,value,0)
	
}


function empty_portfolio()
{
     xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	} 
	
	
		
	var getConfirm= confirm("Are you sure you wish to delete all your previously selected items");
	
	if(getConfirm == true)
	{
		var url="empty_portfolio.php";
		xmlHttp.onreadystatechange=show_empty_portfolio;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
	
}


function show_empty_portfolio()
{
	if (xmlHttp.readyState==4)
 	document.getElementById("shopping_items").innerHTML = '(0)';
	
	if(document.getElementById('display_shopping_items'))
	document.getElementById("display_shopping_items").innerHTML = '(0) Items'
	
}


function congratuation()
{

	var message = '<span class="h1">Congratulations  </span><table width="540" height="400" border="0" cellpadding="3" cellspacing="0" bgcolor="#CCCCCC"><tr><td colspan="3" valign="top" bgcolor="#FFFFFF"><p class="VrdBlkBld">Your download has been successfully downloaded.<br /><br /> <img src="images/choose_bar.gif" width="500" height="32" /><br /><br /><img src="images/1.gif" width="50" height="30" align="absbottom" /><span class="VrdBlkBld"><a href="clear_logout.php">Clear Download and Sign Out</a></span> <br /><img src="images/2.gif" width="50" height="30" align="absbottom" /><span class="VrdBlkBld"><a href="javascript: clear_portfolio(\'in_download_portfolio\')">Clear Download and Continue to Browse</a></span> <br /><img src="images/3.gif" width="50" height="30" align="absbottom" /><span class="VrdBlkBld"><a href="logout.php">Sign Out</a></span> <br /><br /><td bgcolor="#FFFFFF"><td bgcolor="#FFFFFF"></td><td bgcolor="#FFFFFF"></tr> </table>';


	document.getElementById("show_products").innerHTML=message;

}


function show_update()
{
	if (xmlHttp.readyState==4)
 	{	
			document.getElementById("shopping_items").innerHTML = '(0)';
		document.getElementById("show_products").innerHTML = xmlHttp.responseText;
	}
}


function show_products()
{
	if (xmlHttp.readyState==4)
	{
 		document.getElementById("show_products").innerHTML=xmlHttp.responseText;

	}
}


/* used for showing the result after updating  portfolio */

function show_portfolio_update()
{
	if (xmlHttp.readyState==4)
	{
		var str = xmlHttp.responseText;
		var num = str.indexOf('.');

		var item_num = str.substr(0,num);
		var msg = str.substr(num+1);

		document.getElementById("shopping_items").innerHTML ='('+item_num+') '; 
		document.getElementById("show_products").innerHTML =msg ;
	}
}


/**
		record Record
*/

function recordDownload(imgID)
{	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	} 	
	
	var url="itemTracker.php?item_id="+imgID;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
		
		
		
}




function GetXmlHttpObject()
{
	var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
	return xmlHttp;
}





