function GetXmlHttpObject()
{
	var xmlHttp = null;

	try
	{
		xmlHttp = new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	return xmlHttp;
}

function hi_lite(obj, color) 
{
	obj.style.backgroundColor = color;
}

function lo_lite(obj, color) 
{
	obj.style.backgroundColor = color;
}

var ElemC = null;

function element_on(ElemId,Color) 
{
	if(ElemC != null)
	{
		document.getElementById(ElemC).style.backgroundColor = "";
	
		if(ElemC == ElemId)
		{
			document.getElementById(ElemC).style.backgroundColor = Color;
		}
	}
	
	ElemC = ElemId;
	
	document.getElementById(ElemC).style.backgroundColor = Color;
}

function element_off(obj) 
{
	obj.style.backgroundColor = '';
}

function popUp(lnk, w, h, scroll, res, id) 
{
	lm 	= (screen.width - w) / 2;
	tm 	= (screen.height - h) / 2;
	vars 	= 'scrollbars='+scroll+', width='+w+', height='+h+', top='+tm+', left='+lm+', resizable='+res;

	var win = window.open(lnk+'&popUp=1', 'popUp'+id, vars);
	win.focus();
	return false;
}

function clearField(objField,strDefaultValue) 
{
	if(objField.value == strDefaultValue) objField.value = '';
}
	
function restoreField(objField,strDefaultValue) 
{
	if(objField.value == '') objField.value = strDefaultValue;
}

function confirmDelete(str, lnk) 
{
	if (confirm(str)) 
	{
		window.location = lnk
	}
}

function add_cart(lnk) 
{
	opener.location = lnk;
	window.close();
}

function lpos_on( id )
{
	var sl          = document.getElementById( 'obj_'+id );
	var display     = sl.style.display ? '' : 'none';

	sl.style.display = display;
}

function lpos_off( id )
{
	var sl = document.getElementById( 'obj_'+id );

	sl.style.display = 'none';
}

function LaterForward(Fid,str)
{
	if(confirm(str))
	{
		document.getElementById(Fid).submit();
	}
	else
	{
		return;
	}
}

function BankForward(Fid)
{
	document.getElementById(Fid).submit();
}

function numberFormat(no)
{
	no += '';
	x = no.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';

	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1))
		x1 = x1.replace(rgx, '$1' + ' ' + '$2');

	return x1 + x2;
}

function validatePaymentData()
{
	document.my_card_payment.first_name.value = document.my_card_payment.first_name.value.replace(/<\/?[^>]+(>|$)/g, "");
	document.my_card_payment.last_name.value = document.my_card_payment.last_name.value.replace(/<\/?[^>]+(>|$)/g, "");
	document.my_card_payment.personal_code.value = document.my_card_payment.personal_code.value.replace(/<\/?[^>]+(>|$)/g, "");
	document.my_card_payment.no_idpass_idcard.value = document.my_card_payment.no_idpass_idcard.value.replace(/<\/?[^>]+(>|$)/g, "");
	document.my_card_payment.residence_permit_duration.value = document.my_card_payment.residence_permit_duration.value.replace(/<\/?[^>]+(>|$)/g, "");
	document.my_card_payment.postal_address.value = document.my_card_payment.postal_address.value.replace(/<\/?[^>]+(>|$)/g, "");
	document.my_card_payment.user_phone.value = document.my_card_payment.user_phone.value.replace(/<\/?[^>]+(>|$)/g, "");
	document.my_card_payment.user_account.value = document.my_card_payment.user_account.value.replace(/<\/?[^>]+(>|$)/g, "");
	document.my_card_payment.user_workplace.value = document.my_card_payment.user_workplace.value.replace(/<\/?[^>]+(>|$)/g, "");
	document.my_card_payment.user_occupation.value = document.my_card_payment.user_occupation.value.replace(/<\/?[^>]+(>|$)/g, "");
	
	document.my_card_payment.first_name.style.backgroundColor = "";
	document.my_card_payment.last_name.style.backgroundColor = "";
	document.my_card_payment.personal_code.style.backgroundColor = "";
	document.my_card_payment.no_idpass_idcard.style.backgroundColor = "";
	document.my_card_payment.postal_address.style.backgroundColor = "";
	document.my_card_payment.user_phone.style.backgroundColor = "";
	document.my_card_payment.user_account.style.backgroundColor = "";
	document.my_card_payment.user_workplace.style.backgroundColor = "";
	document.my_card_payment.user_occupation.style.backgroundColor = "";

	if(document.my_card_payment.first_name.value.length == 0)
	{
		document.my_card_payment.first_name.focus();
		document.my_card_payment.first_name.style.backgroundColor = "#FF6600";
		
		return false;
	}
	else if(document.my_card_payment.last_name.value.length == 0)
	{
		document.my_card_payment.last_name.focus();
		document.my_card_payment.last_name.style.backgroundColor = "#FF6600";
		
		return false;
	}
	else if(document.my_card_payment.personal_code.value.length == 0 || isNaN(document.my_card_payment.personal_code.value))
	{
		document.my_card_payment.personal_code.value = "";
	
		document.my_card_payment.personal_code.focus();
		document.my_card_payment.personal_code.style.backgroundColor = "#FF6600";
		
		return false;
	}
	else if(document.my_card_payment.no_idpass_idcard.value.length == 0)
	{
		document.my_card_payment.no_idpass_idcard.focus();
		document.my_card_payment.no_idpass_idcard.style.backgroundColor = "#FF6600";
		
		return false;
	}
	else if(document.my_card_payment.postal_address.value.length == 0)
	{
		document.my_card_payment.postal_address.focus();
		document.my_card_payment.postal_address.style.backgroundColor = "#FF6600";
		
		return false;
	}
	else if(document.my_card_payment.user_phone.value.length == 0)
	{
		document.my_card_payment.user_phone.focus();
		document.my_card_payment.user_phone.style.backgroundColor = "#FF6600";
		
		return false;
	}
	else if(document.my_card_payment.user_account.value.length == 0 || isNaN(document.my_card_payment.user_account.value))
	{
		document.my_card_payment.user_account.value = "";
	
		document.my_card_payment.user_account.focus();
		document.my_card_payment.user_account.style.backgroundColor = "#FF6600";
		
		return false;
	}
	else if(document.my_card_payment.user_workplace.value.length == 0)
	{
		document.my_card_payment.user_workplace.focus();
		document.my_card_payment.user_workplace.style.backgroundColor = "#FF6600";
		
		return false;
	}
	else if(document.my_card_payment.user_occupation.value.length == 0)
	{
		document.my_card_payment.user_occupation.focus();
		document.my_card_payment.user_occupation.style.backgroundColor = "#FF6600";
		
		return false;
	}
	else
	{
		document.getElementById('my_card_payment').submit();
	}
}

var divC = null;

function layer_sw(divI)
{
	if(divC != null)
	{
		document.getElementById(divC).style.width = "auto";
		document.getElementById(divC).style.display = "none";
	
		if(divC == divI)
		{
			divC = null;
			return;
		}
	}
	
	divC = divI;
	
	document.getElementById(divC).style.width = "200px";
	document.getElementById(divC).style.display = "";
}

function closeLayer()
{
	document.getElementById('erro').style.display = 'none';
}

function drawLoading(site)
{
	document.getElementById('erro').style.display = 'block';
	
	if(browser.isIE)
	{
		document.getElementById('erro').style.position = 'absolute';
	}
	else
	{
		document.getElementById('erro').style.position = 'fixed';
	}
	
	document.getElementById('erro').style.top = "40%";
	document.getElementById('erro').style.left = "40%";
	document.getElementById('erro').style.width = "200px";
	document.getElementById('erro').style.height = "auto";
	document.getElementById('erro').innerHTML = '<img src="' + site + '/img/big_loading.gif" class="image" \/>';
}

// -- count basket
function countBasket(site,action)
{
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	
	var url = site + "/ajax/work.php";
	
	url = url + "?sid=" + Math.random();
	
	var params = "action=" + action;
	
	xmlHttp.open("POST",url,true);
	
	xmlHttp.onreadystatechange = function() {stateChangedCountBasket();};
	
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlHttp.send(params);
}

function stateChangedCountBasket()
{
	if (xmlHttp.readyState==4)
	{
		if(xmlHttp.status == 200)
		{
			var output = xmlHttp.responseText;

			document.getElementById("basket_total").innerHTML = output;
			eval("closeLayer()");
		}
	}
}

// -- process: add product|offer to Basket
function addBasket(site,action,product,mess,userId,erroUserId)
{
	var r = confirm(mess);

	if(r == false)
	{
		return;
	}
	
	if(userId == null || userId == 0)
	{
		alert(erroUserId);
		return;
	}
	
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	
	var url = site + "/ajax/work.php";
	
	url = url + "?sid=" + Math.random();
	
	var params = "action=" + action;
	params = params + "&m_product=" + product;
	
	xmlHttp.open("POST",url,true);
	
	xmlHttp.onreadystatechange = function() {stateChangedAddBasket(site);};
	
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlHttp.send(params);
}

// -- handle response: addBasket
function stateChangedAddBasket(site)
{
	document.getElementById("basket_total").innerHTML = '<img src="' + site + '/img/ico_loading.gif" class="image" \/>';

	if (xmlHttp.readyState==4)
	{
		if(xmlHttp.status == 200)
		{
			drawLoading(site);
			eval("countBasket(site,'count_basket')");
		}
	}
}
