var handleSuccess = function(o){
	if(o.responseText !== undefined){

		var response = o.responseText;
		var mySplitResult = response.split("\n");

		var myArray = new Array();
		myArray['id'] = "";
		myArray['count'] = "";
		myArray['name'] = "";
		myArray['price'] = "";
		

		var buybox = "<div class=\"buybox\">";

		for(i = 0; i < mySplitResult.length-1; i++){

			var myProperties = mySplitResult[i].split("|");
			for(j = 0; j < myProperties.length; j++){
				myPair = myProperties[j].split("=");
				myArray[myPair[0]] = myPair[1];

			}

			buybox = buybox+"<div class=\"instock\" style=\"border-top: 0px\">";
			buybox = buybox+"<div class=\"tag\"><br>"+myArray['name']+"</div>";
			buybox = buybox+"<div style=\"text-align:right;\"> ";
			buybox = buybox+"<form id=\"form_basket_"+myArray['id']+"\" method=\"post\" action=\"\"> ";

			if (myArray['count']>0)
			{
			buybox = buybox+""+myArray['price']+" грн. | <span style=\"color:#00FF00\";>в наличии</span> ";
			buybox = buybox+"<input id=\"icon_quantity_"+myArray['id']+"\" name=\"icon_quantity_"+myArray['id']+"\" type=\"text\" maxlength=\"3\" value=\"1\" style=\"width:20px; height:20px;\" class=\"icon_quantity\" />&nbsp;<input type=\"image\" src=\"include/images/button-add.gif\" alt=\"Add\" title=\"Add\" style=\"border:1px solid #000000; vertical-align:bottom; padding:0px; width:36px; height:20px\" onclick=\"ToggleDiv( 'buybox' ); ToggleDiv( 'click' ); hide_div( 'icon_"+o.argument[0]+"' ); document.getElementById( 'timer_"+o.argument[0]+"' ).style.display='inline'; addProduct( '"+myArray['id']+"','"+o.argument[0]+"','"+myArray['count']+"'); return false;\" /> ";
//			buybox = buybox+"<input id=\"icon_quantity_"+myArray['id']+"\" name=\"icon_quantity_"+myArray['id']+"\" type=\"text\" maxlength=\"3\" value=\"1\" style=\"width:20px; height:20px;\" class=\"icon_quantity\" />&nbsp;<input type=\"image\" src=\"include/images/button-add.jpg\" alt=\"Add\" title=\"Add\" style=\"border:1px solid #000000; vertical-align:bottom; padding:0px; width:36px; height:20px\" onclick=\"ToggleDiv( 'buybox' ); hide_div( 'icon_"+o.argument[0]+"' ); document.getElementById( 'timer_"+o.argument[0]+"' ).style.display='inline'; addProduct( '"+myArray['id']+"','"+o.argument[0]+"'); return false;\" /> ";
			}else
			{
			buybox = buybox+""+myArray['price']+" грн. | <span style=\"color:#fff33a\">нет в наличии</span> ";
			}
			buybox = buybox+"</form> ";
			buybox = buybox+"</div> ";
			buybox = buybox+"</div>	";

		}


		buybox = buybox+"</div>	";
	        document.getElementById('buybox').innerHTML = buybox;

		TogglePopup( 'buybox', 'button_add_'+PrID, 'left', 0 ); ToggleDiv( 'click' ); return false;
//		TogglePopup( 'buybox', 'button_add', 'left', 0 ); return false;
	}
}

var handleFailure = function(o){
	if(o.responseText !== undefined){
		alert(o.responseText);
	}
}


var abhandleSuccess = function(o){
	if(o.responseText !== undefined){

		var response = o.responseText;
		document.getElementById( 'basket_div' ).innerHTML = o.responseText;

		var obj = document.getElementById( 'timer_' + o.argument[0] );
		if (!( obj == null )) { document.getElementById( 'timer_' + o.argument[0] ).style.display = "none"; }

		var obj = document.getElementById( 'icon_' + o.argument[0] );
		if (!( obj == null )) {

		if (document.getElementById( 'label_quantity_' + o.argument[0] ).innerHTML != "") document.getElementById( 'icon_' + o.argument[0] ).style.display = "inline";
		}

//		if (document.getElementById( 'timer_' + o.argument[0] )) document.getElementById( 'timer_' + o.argument[0] ).style.display = "none";
//		if (document.getElementById( 'icon_' + o.argument[0] )) document.getElementById( 'icon_' + o.argument[0] ).style.display = "inline";

	}
}

var abhandleFailure = function(o){
	if(o.responseText !== undefined){
		alert(o.responseText);
	}
}


var adhandleSuccess = function(o){
	if(o.responseText !== undefined){


		var response = o.responseText;
		var mySplitResult = response.split("\n");

		var myArray = new Array();
		myArray['id'] = "";
		myArray['count'] = "";


		for(i = 0; i < mySplitResult.length-1; i++){

			var myProperties = mySplitResult[i].split("|");
			for(j = 0; j < myProperties.length; j++){

				myPair = myProperties[j].split("=");
				myArray[myPair[0]] = myPair[1];
			}
//alert(myArray['id']);
				var obj = document.getElementById( 'label_quantity_' + myArray['id'] );
				if (!( obj == null )) 
					{
					  if (myArray['count']>0){
						document.getElementById( 'label_quantity_' + myArray['id'] ).innerHTML = myArray['count'];
					  }else{
						document.getElementById( 'label_quantity_' + myArray['id'] ).innerHTML = "";
						document.getElementById( 'icon_' + myArray['id'] ).style.display = "none";
					  }
					}

				callback1 =
				{
				  success: abhandleSuccess,
				  failure: abhandleFailure,
			          argument: [myArray['id']]
				};


			       var guid = "";
			       for (var i = 0; i < 32; i++) guid += parseInt(Math.random() * 16).toString(16);

			       var sUrl = "include/get_basket.php?"+guid;
			       var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback1);
				

	       }


	}
}

var adhandleFailure = function(o){
	if(o.responseText !== undefined){
		alert(o.responseText);
	}
}



function getProperties(ID)
{
var guid = "";
for (var i = 0; i < 32; i++) guid += parseInt(Math.random() * 16).toString(16);

PrID = ID;

callback =
{
  success:handleSuccess,
  failure: handleFailure,
  argument: [PrID]
};

var sUrl = "include/get_product_properties.php?"+guid;
var postData = "ProductID="+PrID;
var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
return false;
}

function addProduct(ID, productID, Total)
{
var count = document.getElementById('icon_quantity_'+ID).value;

var itemsordered = document.getElementById( 'label_quantity_' +ID )
var itemsordered_count = 0;
if (!( itemsordered == null )) { itemsordered_count = document.getElementById( 'label_quantity_' + ID ).innerHTML; }

 if (count <= (Total - itemsordered_count))
// if (count <= Total)
 {
	var guid = "";
	for (var i = 0; i < 32; i++) guid += parseInt(Math.random() * 16).toString(16);

	WarehouseID = ID;
	callback =
	{
	  success: adhandleSuccess,
	  failure: adhandleFailure,
	  argument: [WarehouseID]
	};


	var sUrl = "include/insert_product_to_basket.php?"+guid;
	var postData = "WarehouseID="+WarehouseID;
	var postData = postData + "&quantity="+count+"&productid="+productID;
	var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
 }
 else
 {
  	alert("Максимальное количество на складе: "+Total+". Жаль, но вы не можете заказать больше.");

	var obj = document.getElementById( 'timer_' + o.argument[0] );
	if (!( obj == null )) { document.getElementById( 'timer_' + o.argument[0] ).style.display = "none"; }

	var obj = document.getElementById( 'icon_' + o.argument[0] );
	if (!( obj == null )) {
	if (document.getElementById( 'label_quantity_' + o.argument[0] ).innerHTML != "") document.getElementById( 'icon_' + o.argument[0] ).style.display = "inline";
	}

	return false;
 }
return false;
}


function removeItem(ID, productID)
{
var guid = "";
for (var i = 0; i < 32; i++) guid += parseInt(Math.random() * 16).toString(16);

WarehouseID = ID;

callback =
{
  success: adhandleSuccess,
  failure: adhandleFailure,
  argument: [WarehouseID]
};


var sUrl = "include/delete_product_from_basket.php?"+guid;
var postData = "WarehouseID="+WarehouseID;
var postData = postData + "&productid="+productID;
var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);

return false;
}














var g_load_flags = 1;
var g_flags = new Array();

function getY( oElement )
{
var iReturnValue = 0;

while( oElement != null ) {
iReturnValue += oElement.offsetTop;
oElement = oElement.offsetParent;
}
return iReturnValue;
}

function getX( oElement )
{
var iReturnValue = 0;
while( oElement != null ) {
iReturnValue += oElement.offsetLeft;
oElement = oElement.offsetParent;
}
return iReturnValue;
}

function TogglePopup( id, parent_object, position, border )
{
	if( position == null ) position = "left";
	if( border == null ) border = 0;
	var obj = document.getElementById( parent_object );
	var width = obj.width;
	var height = obj.height;
	var x = getX( obj );
	var y = getY( obj );
	
	
	if( position == "right" ) 
		x += width + border * 2;
	y += height + border * 2;
	
	obj = document.getElementById( id );
	obj.style.display = "block";
	
	width = 0;
	if( position == "right" )
		width = obj.offsetWidth;
	obj.style.left = ( x - width ) + 'px';
	obj.style.top = y + 'px';
}


function ToggleField( id )
{
	var obj = document.getElementById( id );
	if( obj.disabled == false )
		obj.disabled = true;
	else
		obj.disabled = false;
}

function ToggleDiv( id )
{
	
	var obj = document.getElementById( id );
	if( obj.style.display == "none" )
		obj.style.display = "block";
	else
		obj.style.display = "none";
}

function toggle_div( id )
{	
	var obj = document.getElementById( id );
	if( obj.style.display == "none" )
		obj.style.display = "block";
	else
		obj.style.display = "none";
}

function ShowDivs( className, display )
{
	var divs = document.getElementsByTagName( "div" );
	for( var i = 0; i < divs.length; i++ )
	{
		var currentDiv = divs[ i ];
		if( currentDiv.className == className )
			currentDiv.style.display = display;

	}		
}

function ShowDiv( id )
{
	var obj = document.getElementById( id );
	obj.style.display = "block";
}

function show_div( id )
{
	var obj = document.getElementById( id );
	obj.style.display = "block";
}

function hide_div( id )
{
	var obj = document.getElementById( id );
	obj.style.display = "none";
}

function HidePopups( class_name )
{
	var divs = document.getElementsByTagName( "div" );
	for( var i = 0; i < divs.length; i++ )
	{
		var currentDiv = divs[ i ];
		if( currentDiv.className == class_name )
			currentDiv.style.display = "none";
	}		
}

function load_flags()
{
	
	if( g_load_flags == 1 )
	{
		var divs = document.getElementsByTagName( "img" );
		
		outs = 0;
		
		for( var i = 0; i < divs.length; i++ )
		{
			var currentDiv = divs[ i ];
			if( currentDiv.className == "flag_img" )
			{
				currentDiv.src = currentDiv.title;
				outs = currentDiv.title; 
			}
		}
		
		g_load_flags  = 0;
	}
}

function redirect_form(form_id, url)
{
	var obj = document.getElementById( form_id );
	obj.action = url;
	obj.submit();
}
