function updateReq(ContDiv, url, Method, postFields, noCheckFields){
	if (Method == '' || Method == undefined){
		Method = 'GET';
	}
	// setTimeout(function(){ahahJs(ContDiv, url, Method, postFields)}, 10)
	ahahJs(ContDiv, url, Method, postFields, noCheckFields);
}

function ahahJs(ContDiv, url, Method, postFields, noCheckFields){
	GestDivDisplay(ContDiv, '');
	var postData = "";
	var req = "";

	// document.getElementById(ContDiv).innerHTML = 'caricamento dati in corso...';
	document.getElementById(ContDiv).innerHTML = ' ';

	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}

	if (req) {
		req.onreadystatechange = function() {
			if (req.readyState == 4) { 
				if (req.status == 200) { 
					document.getElementById(ContDiv).innerHTML = req.responseText;
					ahahEvalJS(ContDiv);
				} else {
					document.getElementById(ContDiv).innerHTML="ahah error:\n"+req.statusText;
				}
			}
		};

		// req.open('GET', url + document.getElementById("chars").value, true);
		if (Method == "GET"){
			if (url.indexOf("?")!= -1){
				req.open(Method, url+"&rand="+escape(Math.random()), true);
			}else{
				req.open(Method, url+"?rand="+escape(Math.random()), true);
			}
		}else{
			req.open(Method, url, true);
		}

		if (Method == 'POST'){			

			if (noCheckFields != 1){
				pFields = postFields.split("|");
				for (i=0; i<pFields.length; i++){
					if (i==0){
						postData += pFields[i]+"="+escape(encodeURI(document.getElementById(pFields[i]).value));
					}else{
						postData += "&"+pFields[i]+"="+escape(encodeURI(document.getElementById(pFields[i]).value));
					}
				}
			
			}else{
				postData = postFields;
			}

			req.setRequestHeader("Method", "POST "+url+"? HTTP/1.1");
			req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			req.setRequestHeader("Content-length", postData.length);
			req.setRequestHeader("Connection", "close"); 
		}

		req.send(postData);
	}
}

ahahEvalJS = function(elementId) {
	var scripts = document.getElementById(elementId).getElementsByTagName('script');
	var code;
	for (var i = 0; i < scripts.length; i++) {
		code =	scripts[i].innerHTML ? scripts[i].innerHTML : 
			scripts[i].text ? scripts[i].text : 
			scripts[i].textContent;
		try {
			eval(code);
		} catch(e) {
			alert(e);
		}
	}
}


evalDivJs = function(elementId) {
	var scripts = elementId.getElementsByTagName('script');
	var code;
	for (var i = 0; i < scripts.length; i++) {
		code =	scripts[i].innerHTML ? scripts[i].innerHTML : 
			scripts[i].text ? scripts[i].text : 
			scripts[i].textContent;
		try {
			eval(code);
		} catch(e) {
			alert(e);
		}
	}
}


function ShowHiddenDiv(DivName) {
	if (document.getElementById(DivName).style.display=='none'){
		document.getElementById(DivName).style.display="";
	} else {
		document.getElementById(DivName).style.display="none";
	}
}


function OpenWin(PageName, W, H){
	window.open (PageName, "mywindow","location=0,status=0,scrollbars=1, width="+W+",height="+H+"");
}


function GestDivDisplay(DivName, Status, SetW) {

	if (SetW != '' && SetW != undefined){
		document.getElementById(DivName).style.width = parseInt(SetW)+"px";
	}

	document.getElementById(DivName).style.display=Status;
}


function ChangeImg(ImageName,ImageFile){
	document.getElementById(ImageName).src = ImageFile;
}


function SetFieldValue(FieldName, sValue){
	document.getElementById(FieldName).value = sValue;
}


function UpdateShipCosts(PriceValue, WeightValue){

	PayMethod = document.getElementById('PayMethodField').value;
	ShipAddr = document.getElementById('ShipAddrField').value;

	updateReq('ship_cost', 'inc_store_shop_checkout_shipcost.php?price='+PriceValue+'&weight='+WeightValue+'&paymethod='+PayMethod+'&shipaddr='+ShipAddr+'');
}


function ChangeFormAction(formname, actionURL){
	formname.action = actionURL;
}


function SetEndDis(FieldName, sValue){
	document.getElementById(FieldName).disabled = sValue;
}


function setErrorToField(fieldId, status, txt){
	if (document.getElementById('err_'+fieldId) != undefined){
		document.getElementById('err_'+fieldId).innerHTML = "<span class='error'>"+txt+"</span>";
		document.getElementById('err_'+fieldId).style.display = status;

		if (document.getElementById(fieldId) != undefined && status != 'none'){
			document.getElementById(fieldId).className = 'input_error';
		}
	}
}


function hideErrorField(fieldId, vClass){
	if (document.getElementById('err_'+fieldId) != undefined){
		document.getElementById('err_'+fieldId).style.display = 'none';

		if (document.getElementById(fieldId) != undefined){

			if (vClass != undefined){
				setClassName = vClass;
			
			}else{
				setClassName = '';
			}

			document.getElementById(fieldId).className = setClassName;
		}
	}
}


function viewDivAlert(txt, closeTxt, status){
	var fieldId = 'msg_div';
	var output = '';

	output = txt;
	output += '<br><br><a href="javascript:void(0);" onClick="javascript:viewDivAlert(\'\'\, \'\'\, \'none\');">'+closeTxt+'</a>';

	document.getElementById(fieldId).innerHTML = output;
	document.getElementById(fieldId).style.display = status;
}


function setDivContent(fieldId, cont){
	document.getElementById(fieldId).innerHTML = cont;
}


function redir(vLocation){
	// parent.location.href=vLocation;

	secondi=3;
	setTimeout(function(){parent.location.href=vLocation}, secondi*1000)
}


function redirNow(vLocation){
	parent.location.href=vLocation;
}


function gestObjVisibility(fielId, Status) {
	document.getElementById(fielId).style.visibility=Status;
}


function centerCont(){
	var totW = document.getElementById('contenuto_sisposta').offsetWidth;
	var totWHeader = document.getElementById('header').offsetWidth;
	var totWFooter = document.getElementById('footer').offsetWidth;

	if (document.all){ // Explorer
		pageW = document.body.clientWidth;
	}else{
		pageW = document.body.clientWidth;
	}

	// var IpopTop = (document.body.clientHeight - document.getElementById(divName).offsetHeight) / 2;
	var IpopLeft = (pageW - totW) / 2;
	var IpopLeftHeader = (pageW - totWHeader) / 2;
	var IpopLeftFooter = (pageW - totWFooter) / 2;

	// document.getElementById('main').style.marginLeft = IpopLeft+"px";
	// document.getElementById(divid).style.top=IpopTop;

	document.getElementById('contenuto_sisposta').style.marginLeft = IpopLeft+"px";
	document.getElementById('header').style.marginLeft = IpopLeftHeader+"px";
	document.getElementById('footer').style.marginLeft = IpopLeftFooter+"px";
}



var headerIntervalId = new Array();

function moveHeader(divName, yPos){

	var myObj = document.getElementById(divName);
	var endH = parseInt(myObj.offsetHeight) + yPos;
	
	cmd = "moveHeaderStep('" +  divName + "','" + endH + "')";
	headerIntervalId[divName] = setInterval(cmd, 10);
}


function moveHeaderStep(divName, yPos){

	var myObj = document.getElementById(divName);

	if (myObj.style.height == "" || myObj.style.height == undefined){
		myObjH = myObj.offsetHeight;

	}else{
		myObjH = myObj.style.height;
	}

	myObj.style.height = (parseInt(myObjH) + 10) + "px";

	if (parseInt(myObj.style.height) >= yPos){
		clearInterval(headerIntervalId[divName]);
		headerIntervalId[divName] = undefined;
	}

}


function showHideAstProv(sData, fieldName){
	if (document.getElementById(fieldName) != undefined){
		if (sData != "" && sData != undefined){
			document.getElementById(fieldName).style.visibility = "";
		
		}else{
			document.getElementById(fieldName).style.visibility = "hidden";
		}
	}
}


var onlineIntervalId = new Array();
var onlineSpost = 0;
var onlineIsOpen = 0;

function moveOnline(){

	var divName = "online_list";

	if (onlineIsOpen == 0){
		var yPos = 26;
	}else{
		var yPos = 0;
	}
	
	cmd = "moveOnlineStep('" +  divName + "','" + yPos + "')";
	onlineIntervalId[divName] = setInterval(cmd, 10);
}


function moveOnlineStep(divName, yPos){

	var myObj = document.getElementById(divName);
	var myContentDiv = document.getElementById('content');
	var myHeaderDiv = document.getElementById('cont_header');
	var myFooterDiv = document.getElementById('footer');
	var myContFirmaDiv = document.getElementById('content_firma');
	var myContFooterDiv = document.getElementById('content_footer');

	if (onlineIsOpen == 0){
		var movOffset = 4;
	}else{
		var movOffset = -4;
	}

	if (myObj.style.marginTop == "" || myObj.style.marginTop == undefined){
		myObjTop = 28;

	}else{
		myObjTop = myObj.style.marginTop;
	}

	if (myHeaderDiv.style.marginTop == "" || myHeaderDiv.style.marginTop == undefined){
		myHeaderDivTop = 0;

	}else{
		myHeaderDivTop = myHeaderDiv.style.marginTop;
	}

	if (myContentDiv.style.marginTop == "" || myContentDiv.style.marginTop == undefined){
		myContentDivTop = 0;

	}else{
		myContentDivTop = myContentDiv.style.marginTop;
	}

	if (myContFooterDiv.style.marginTop == "" || myContFooterDiv.style.marginTop == undefined){
		myContFooterDivTop = -4;

	}else{
		myContFooterDivTop = myContFooterDiv.style.marginTop;
	}

	if (myContFirmaDiv.style.marginTop == "" || myContFirmaDiv.style.marginTop == undefined){
		myContFirmaDivTop = 0;

	}else{
		myContFirmaDivTop = myContFirmaDiv.style.marginTop;
	}

	if (myFooterDiv.style.marginTop == "" || myFooterDiv.style.marginTop == undefined){
		myFooterDivTop = 0;

	}else{
		myFooterDivTop = myFooterDiv.style.marginTop;
	}

	myHeaderDiv.style.marginTop = (parseInt(myHeaderDivTop) - movOffset) + "px";
	myObj.style.marginTop = (parseInt(myObjTop) - movOffset) + "px";
	// myContentDiv.style.marginTop = (parseInt(myContentDivTop) - movOffset) + "px";
	myFooterDiv.style.marginTop = (parseInt(myFooterDivTop) + movOffset) + "px";
	myContFooterDiv.style.marginTop = (parseInt(myContFooterDivTop) + movOffset) + "px";
	myContFirmaDiv.style.marginTop = (parseInt(myContFirmaDivTop) - movOffset) + "px";

	onlineSpost = onlineSpost + movOffset;

	if (onlineIsOpen == 0){
		if (onlineSpost >= yPos){
			clearInterval(onlineIntervalId[divName]);
			onlineIntervalId[divName] = undefined;
			onlineIsOpen = 1;
		}

	}else{
		if (onlineSpost <= yPos){
			clearInterval(onlineIntervalId[divName]);
			onlineIntervalId[divName] = undefined;
			onlineIsOpen = 0;
		}
	}
}