var is_ie = (document.all) ? true : false;

function init_ajax()
{
    if (is_ie){
	try{
	  objHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}catch(e){
	  return false; 
	}
    }else{
    	try{
	  objHttp = new XMLHttpRequest();
	}catch(e) {
	  return false;
	}
    }
    return objHttp;	
}

function ajax_request(objHttp,query,post_data)
{
	objHttp.open("POST", query, true);
	objHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	objHttp.send(post_data);
}

function add_to_cart(link,id,s)
{
	objHttp = init_ajax();
	objHttp.onreadystatechange = function()
	{
		if (objHttp.readyState == 4){
			if (objHttp.responseText==1){
				if (document.getElementById('search_cart_'+id)!=undefined) document.getElementById('search_cart_'+id).innerHTML = 'In Shopping Cart';
				inc_cart_count(1);
			}else{
				if (document.getElementById('search_cart_'+id)!=undefined) document.getElementById('search_cart_'+id).innerHTML = 'Something wrong';
			}
		}
	}
	ajax_request(objHttp,link,'id='+id+'&s='+s);
}

function delete_from_cart_by_id(link,id)
{
	objHttp = init_ajax();
	objHttp.onreadystatechange = function()
	{
		if (objHttp.readyState == 4){
			/*if (objHttp.responseText==1){
				if (document.getElementById('search_cart_'+id)!=undefined) document.getElementById('search_cart_'+id).innerHTML = 'In Shopping Cart';
				inc_cart_count(1);
			}else{
				if (document.getElementById('search_cart_'+id)!=undefined) document.getElementById('search_cart_'+id).innerHTML = 'Something wrong';
			}*/
		}
	}
	ajax_request(objHttp,link,'id='+id);
}

function request_link(link)
{
	objHttp = init_ajax();
	objHttp.onreadystatechange = function()
	{
		if (objHttp.readyState == 4){
			
		}
	}
	ajax_request(objHttp,link,'');
}


function inc_cart_count(cnt)
{
	if (!cnt) cnt = 1;
	count = document.getElementById('cart_items_count').innerHTML;
	document.getElementById('cart_items_count').innerHTML = parseInt(count)+cnt;
}

function dec_cart_count(cnt)
{
	if (!cnt) cnt = 1;
	count = document.getElementById('cart_items_count').innerHTML;
	document.getElementById('cart_items_count').innerHTML = parseInt(count)-cnt;
}

var loaded_urls = new Array();
loaded_urls[0] = new Array();
loaded_urls[1] = new Array();
loaded_urls[2] = new Array();
loaded_urls[3] = new Array();

function load_url_to_element(link,key,container,kj)
{
	if (!kj) kj = 0;
	if (key!='' && loaded_urls[kj][key]!=undefined && loaded_urls[kj][key]!='') document.getElementById(container).innerHTML = loaded_urls[kj][key];
	var objHttp = new Array();
	objHttp[kj] = init_ajax();
	objHttp[kj].onreadystatechange = function()
	{
		if (objHttp[kj].readyState == 4){
			document.getElementById(container).innerHTML = objHttp[kj].responseText;
			if (key!='') loaded_urls[kj][key] = objHttp[kj].responseText;
		}
	}
	ajax_request(objHttp[kj],link,'');
}

function buyer_root_add(link,id)
{
	objHttp = init_ajax();
	objHttp.onreadystatechange = function()
	{
		if (objHttp.readyState == 4){
			/*if (objHttp.responseText==1){
				if (document.getElementById('search_cart_'+id)!=undefined) document.getElementById('search_cart_'+id).innerHTML = 'In Shopping Cart';
				inc_cart_count(1);
			}else{
				if (document.getElementById('search_cart_'+id)!=undefined) document.getElementById('search_cart_'+id).innerHTML = 'Something wrong';
			}*/
		}
	}
	ajax_request(objHttp,link,'id='+id);
}

function buyer_lightbox_add(link,id,folder)
{
	objHttp = init_ajax();
	objHttp.onreadystatechange = function()
	{
		if (objHttp.readyState == 4){
			/*if (objHttp.responseText==1){
				if (document.getElementById('search_cart_'+id)!=undefined) document.getElementById('search_cart_'+id).innerHTML = 'In Shopping Cart';
				inc_cart_count(1);
			}else{
				if (document.getElementById('search_cart_'+id)!=undefined) document.getElementById('search_cart_'+id).innerHTML = 'Something wrong';
			}*/
		}
	}
	ajax_request(objHttp,link,'id='+id+'&f='+folder);
}

function lightbox_to_cart(link,id)
{
	objHttp = init_ajax();
	objHttp.onreadystatechange = function()
	{
		if (objHttp.readyState == 4){
			if (objHttp.responseText==1){
				if (document.getElementById('lsc_a_'+id)!=undefined) document.getElementById('lsc_a_'+id).style.display = 'none';
				if (document.getElementById('lsc_r_'+id)!=undefined) document.getElementById('lsc_r_'+id).style.display = '';
			}else{
				if (document.getElementById('lsc_s_'+id)!=undefined) document.getElementById('lsc_s_'+id).innerHTML = 'Something wrong';
			}
		}
	}
	ajax_request(objHttp,link,'id='+id);
}

function lightbox_remove_from_cart(link,id)
{
	objHttp = init_ajax();
	objHttp.onreadystatechange = function()
	{
		if (objHttp.readyState == 4){
			if (objHttp.responseText==1 || objHttp.responseText==''){
				if (document.getElementById('lsc_r_'+id)!=undefined) document.getElementById('lsc_r_'+id).style.display = 'none';
				if (document.getElementById('lsc_a_'+id)!=undefined) document.getElementById('lsc_a_'+id).style.display = '';
			}else{
				if (document.getElementById('lsc_s_'+id)!=undefined) document.getElementById('lsc_s_'+id).innerHTML = 'Something wrong';
			}
		}
	}
	ajax_request(objHttp,link,'id='+id);
}

function move_lightbox_to_lightbox(link,from,to)
{
	objHttp = init_ajax();
	objHttp.onreadystatechange = function()
	{
		if (objHttp.readyState == 4){
			/*if (objHttp.responseText==1){
				if (document.getElementById('search_cart_'+id)!=undefined) document.getElementById('search_cart_'+id).innerHTML = 'In Shopping Cart';
				inc_cart_count(1);
			}else{
				if (document.getElementById('search_cart_'+id)!=undefined) document.getElementById('search_cart_'+id).innerHTML = 'Something wrong';
			}*/
		}
	}
	ajax_request(objHttp,link,'id1='+from+'&id2='+to);	
}

function move_deposit_item_to_lightbox(link,item_id,to)
{
	objHttp = init_ajax();
	objHttp.onreadystatechange = function()
	{
		if (objHttp.readyState == 4){
			/*if (objHttp.responseText==1){
				if (document.getElementById('search_cart_'+id)!=undefined) document.getElementById('search_cart_'+id).innerHTML = 'In Shopping Cart';
				inc_cart_count(1);
			}else{
				if (document.getElementById('search_cart_'+id)!=undefined) document.getElementById('search_cart_'+id).innerHTML = 'Something wrong';
			}*/
		}
	}
	ajax_request(objHttp,link,'item_id='+item_id+'&id='+to);	
}
