// JavaScript Document

/* Begin Ajax */
var xmlHttp //doi tuong thuc thi ajax
var aseachContent = "";

function stateChanged1() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		if(xmlHttp.responseText)
		{
			document.getElementById("listProduct").innerHTML=xmlHttp.responseText;						
			document.getElementById('tdShowSearch').innerHTML = aseachContent;			
		}
	}
}



function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		if(xmlHttp.responseText)
		{
			
			document.getElementById("tdShowSearch").innerHTML=xmlHttp.responseText;			
			loadAjax = 1;			
		}
	}
}

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;
}
/* End Ajax */

function showSearch()
{
	
	if(document.getElementById('trShowSearch').style.display=='')
	{		
		document.getElementById('trShowSearch').style.display='none';
		document.getElementById('trHideSearch').style.display='';	
	}
	else
	{
			
		if (loadAjax == 0)
		{
			xmlHttp=GetXmlHttpObject();
			
			if (xmlHttp==null)
			{
				  alert ("Your browser does not support AJAX!");
				  return;
			} 
			else
			{
				
				xmlHttp.onreadystatechange=stateChanged;
				xmlHttp.open("GET",asearch_url,true);
				xmlHttp.send(null);	
	
				document.getElementById('trShowSearch').style.display='';
				document.getElementById('trHideSearch').style.display='none';
			}
		}
		else
		{
				document.getElementById('trShowSearch').style.display='';
				document.getElementById('trHideSearch').style.display='none';
	
		}
	
	}

}

function beginASearch()
{
	//if(document.getElementById('tdShowSearch').innerHTML!=aseachContent)	
	{
		var ssize=0;
		if (document.getElementById('ssize'))
			ssize = document.getElementById('ssize').value;

		var scolor=0;
		if (document.getElementById('scolor'))
			scolor = document.getElementById('scolor').value;

		var sprice=0;
		if (document.getElementById('sprice'))
			sprice = document.getElementById('sprice').value;

		aseachContent = document.getElementById('tdShowSearch').innerHTML;
		document.getElementById('tdShowSearch').innerHTML = '<img src="images/Image/layout/wait.gif" title="Loading data ...!" />';
		
		xmlHttp=GetXmlHttpObject();
		
		if (xmlHttp==null)
		{
			 alert ("Your browser does not support AJAX!");
			 document.getElementById('tdShowSearch').innerHTML = aseachContent;
			 return;
		} 
		else
		{

			xmlHttp.onreadystatechange=stateChanged1;			
			xmlHttp.open("GET", (asearch_url1+"&ssize="+ssize+"&scolor="+scolor+"&sprice="+sprice), true);
			xmlHttp.send(null);	

		}
	}

}

function beginASearch1(url)
{
	//if(document.getElementById('tdShowSearch').innerHTML!=aseachContent)	
		
	{
		
		aseachContent = document.getElementById('tdShowSearch').innerHTML;
		document.getElementById('tdShowSearch').innerHTML = '<img src="images/Image/layout/wait.gif" title="Loading data ...!" />';
		document.getElementById("listProduct").innerHTML= '<img src="images/Image/layout/wait.gif" title="Loading data ...!" />';
		
		xmlHttp=GetXmlHttpObject();
		
		if (xmlHttp==null)
		{
			 alert ("Your browser does not support AJAX!");
			 document.getElementById('tdShowSearch').innerHTML = aseachContent;
			 return;
		} 
		else
		{

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

		}
	}

}

function mypopup(url,width,height)
{
   mywindow = window.open (url,"mywindow","scrollbars = yes, width="+width+",height="+height);
   if (mywindow)
   		mywindow.moveTo(((screen.width/2)-(width/2)),((screen.height/2)-(height/2)));
   else	
   		alert('Your popup is blocked!');
}

function mypopup1(url,width,height)
{
   mywindow = window.open (url,"mywindow","width="+width+",height="+height);
   if (mywindow)
   {
   		//mywindow.moveTo(((screen.width/2)-(width/2)),((screen.height/2)-(height/2)));
   }
   else	
   		alert('Your popup is blocked!');
}


/*-------------------------------- Product Detail -------------------------------------------*/
function loadQuantity()
{
	if (!document.getElementById('size_id')||!document.getElementById('size_quantity_hidden')||!document.getElementById('size_quantity'))
		return false;
	
	if(!document.getElementById('size_id').options.length) 
	{
		document.getElementById('imgAddCart').src 			=	'images/Image/layout/btn-addtobag-blur.jpg';	
		return false;
	}
	
	size_index    	= document.getElementById('size_id').selectedIndex;
	quantity 		= document.getElementById('size_quantity_hidden').options[size_index].value;
	size_status		= document.getElementById('size_quantity_hidden').options[size_index].text;
	
	document.getElementById('size_quantity').options.length = 	0;
	document.getElementById('imgAddCart').title 			= 	'Add this product to your bag!';
	document.getElementById('imgAddCart').src 				=	'images/Image/layout/btn-addtobag.gif';
	document.getElementById('msgSize_status').innerHTML		=	'';
	document.getElementById('size_name_selected').innerHTML	=   document.getElementById('size_id').options[size_index].text ;
	document.getElementById('size_name_selected_1').innerHTML	=   document.getElementById('size_id').options[size_index].text ;
	
	if(quantity==0) 
	{
		document.getElementById('size_quantity').options[0]  = 	new Option('--',0);	
		document.getElementById('imgAddCart').title 		 = 	'Select another size please!';
		document.getElementById('imgAddCart').src 			 =	'images/Image/layout/btn-addtobag-blur.jpg';
		document.getElementById('msgSize_status').innerHTML  =	size_status;
	}
	
	while (quantity>0){
		document.getElementById('size_quantity').options[(quantity-1)] = new Option(quantity,quantity);	
		quantity--;
	}

	
	document.getElementById('size_quantity').selectedIndex = 0;
}


function func_addTobag()
{
	if (!document.getElementById('size_id')||!document.getElementById('size_quantity'))
	{
		alert('Quatity is updating! \nYou can choose another size or another product please!');
		//return false;
	}	
	else if (document.getElementById('size_quantity').length==0 || document.getElementById('size_quantity').value==0)
	{
		alert('Quatity is updating! \nYou can choose another size or another product please!');
		//return false;
		
	}
	else
	{
		document.frm_addToBag.action = 	document.frm_addToBag.action+"&ssize="+document.getElementById('size_id').options[document.getElementById('size_id').selectedIndex].value+"&squantity="+document.getElementById('size_quantity').options[document.getElementById('size_quantity').selectedIndex].value;
		document.frm_addToBag.submit();
	}
}

