function sendPWD(){
	var str = prompt(window.sendPwd,"");
	if(str) window.location.replace('default.asp?cmd=sendPwd&cmdString='+escape(str));
}

function openPopUp(strURL,w,h,t,l,refName,e){
	e = e ||event;
	w = w+20;
	h = h+20;
	var obj;
	if(refName)
		obj = document.getElementById?document.getElementById(refName):document.layers?document.layers[refName]:document.all?document.all[refName]:null;
	if(t==null||l==null){
		if(e){
			//var l=e.clientX-150;
			//var t=e.clientY-34;
			l=e.screenX+20;
			t=e.screenY-222;
		}else if(obj){
			obj.css=document.layers?obj:obj.style;
			t=obj.css.top||obj.css.pixelTop||obj.offsetTop||100;
			l=obj.css.left||obj.css.pixelLeft||obj.offsetLeft||100;
		}else{t=100,l=100;}
	} else if(t>>0!=t&&l>>0!=l){
		l=l=="cs"?screen.width/2-w/2:100;
		t=t=="cs"?screen.height/2-h/2:100;
	}
	if(oPopUp&&!oPopUp.closed) oPopUp.close();

	oPopUp = window.open(strURL,"PopUp","width="+w+",height="+h+",top="+t+",left="+l+",scrollbars=1,resizable=1");
	oPopUp.focus();
}

function expandNode(id,obj){
	if(id){
		var obj = document.getElementById("lnk_"+id);
		// questo per dar si che se non voglio che venga moddato il testo non lo fa..
		switch(obj.innerHTML){
			case "+":
				obj.innerHTML = "-";
			break;
			case "-":
				obj.innerHTML = "+";
			break;
		}
		obj = document.getElementById("row_"+id);
	}
	if(obj)
		obj.style.display = obj.style.display=="none"?"":"none";
	return false;
}

function showAdvancedFields(frm,href){
	var fields = frm.getElementsByTagName("fieldset");
	for(var i=0;i<fields.length;i++)
		expandNode(null,fields[i]);
	if(fields[0].style.display=="none")
		href.style.backgroundPosition = "0 -16px" ;
	else
		href.style.backgroundPosition = "0 0" ;
	return false;
}

function checkOrderForm(frm){
	var courierChecked = false;
	var fldCourierToCheck;
	if(frm.courierID){
		if(frm.courierID.type=="select-one"){
			courierChecked = (frm.courierID.options[frm.paymentID.selectedIndex].value>>0) >0;
			fldCourierToCheck = frm.courierID;
		}else if( frm.courierID.type=="radio" || frm.courierID.length ) {
			// elenco di radio
			if(frm.courierID.length){
				for(var i=0,courierChecked=false;i<frm.courierID.length;i++)
					if(frm.courierID[i].checked){
						courierChecked = true;
						break;
					}
				fldCourierToCheck = frm.courierID[0];
			}
			// radio singolo
			else{
				courierChecked = frm.courierID.checked;
				fldCourierToCheck = frm.courierID;
			}
		}
	}
	
	if(!courierChecked){
		alert(window.courier);
		if(fldCourierToCheck)
			fldCourierToCheck.focus();
		return false;
	}
	
	if( !(frm.paymentID.options[frm.paymentID.selectedIndex].value>>0) ){
		alert(window.payment);
		frm.paymentID.focus();
		return false;
	}
	
	frm.action = "default.asp?cmd=sendOrder";
	return true;
}

function showHideCat(o){
	var list = o.parentNode.getElementsByTagName("ul");
	o.className = o.className=="open"?"folder":"open";
	for(var i=0;i<list.length;i++)
		if(o.parentNode==list[i].parentNode)
			list[i].style.display = list[i].style.display=="none"?"block":"none";
	return false;
}

function showVariant(customT1,customT2){
	customT1 = customT1>>0;
	customT2 = customT2>>0;
	var grid = document.getElementById("varaintsListBox");
	if(grid){
		var rowEls = grid.getElementsByTagName("TR");
		if(customT1) var rgx = new RegExp("_"+customT1+"_");
		else if(customT2) var rgx = new RegExp("_"+customT2+"$");
		// nascondo - mostro le righe
		for(var i=1,c=0;i<rowEls.length;i++){
			if(rgx&&rowEls[i].id.search(rgx)==-1)
				rowEls[i].style.display = "none";
			else{
				rowEls[i].style.display = "";
				rowEls[i].className = c%2==0?"bkg1":"bkg2";
				c++;
			}
		}
		// modifico l'action dei form carrello per mantenere selezionato il filtro
		var formList = grid.getElementsByTagName("FORM");
		for(var i=0;i<formList.length;i++)
			formList[i].action = (formList[i].action||"").replace(/\&filterID.*$/,"")+"&filterID="+(customT1||customT2);
	}
}

function checkConfiguratore(frm){
	var list = frm.elements;
	for(var i=0,obbl;i<list.length;i++){
		if(list[i].type=="select-one"){
			obbl = (list[i].name).split("_")[0];
			if(obbl=="true" && list[i].selectedIndex==0 && list[i].options.length>1){
				alert(window.productObbl);
				list[i].focus();
				return false;
			}
		}
	}
	return true;
}
function renewSession(){
	var ob = document.images["renewSession"];
	if(ob)
		ob.src = "renewSession.asp?par="+Math.random();
}
function cookieAlert(){
	SetCookie("testSetCookie","True");
	//if(!navigator.cookieEnabled){
	if( GetCookie("testSetCookie")!="True" ){
		var w = 300;
		var h = 100;
		var left = (screen.width/2-w/2);
		var top = (screen.height/2-h/2);
		oTempTip = genTip('<h2>Attenzione!</h2><p>Il tuo browser ha i cookie disattivati<br />Per poter utilizzare correttamente i servizi offerti da questo sito e\' necessario attivare i cookie<br /><a href="#" onclick="removeTip();"><img src="ReadySkins/red/images/ico_close.gif" width="14" height="14" alt="" border="0" align="left" vspace="2" />&#160;chiudi</a></p>',w,h,left,top);
		document.body.appendChild(oTempTip);
		window.tempID = setTimeout('removeTip();',5000);
	}
}
function removeTip(){
	if(window.oTempTip)
		document.body.removeChild(window.oTempTip)
}
function genTip(msg,width,height,left,top){
	window.oTempTip = document.createElement("div");
	window.oTempTip.className = "alertBox";
	window.oTempTip.style.width = width+"px";
	window.oTempTip.style.height = height+"px";
	window.oTempTip.style.position = "absolute";
	window.oTempTip.style.left = left+"px";
	window.oTempTip.style.top = top+"px";
	window.oTempTip.innerHTML = msg;
	return oTempTip;
}


function GetCookie(cName) {
	var cookieValue = "";
	if (document.cookie.indexOf(cName) != -1){
		var cookieStart = document.cookie.indexOf(cName);
		var cookieValStart = (document.cookie.indexOf("=", cookieStart) + 1);
		var cookieValEnd = document.cookie.indexOf(";", cookieStart);
		if (cookieValEnd == -1){
			cookieValEnd = document.cookie.length;
		}
		cookieValue = document.cookie.substring(cookieValStart, cookieValEnd);
	}
	return cookieValue;
}

function SetCookie(name,value,expires,path,secure){
	document.cookie = name + "=" +escape(value) +
					( (expires) ? ";expires=" + expires.toGMTString() + 1000*60*20 : "") +
					( (path) ? ";path=" + path : "") +
					( (secure) ? ";secure" : "");
	return true;
}

function goBack(){
	if(window.history.length)
		window.history.go(-1);
	else
		window.location.href = "about:blank";
	return false;
}

function addLoadEvent(func){
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		}
	}

}
function addUnloadEvent(func) {
	var oldOnunload = window.onunload;
	if (typeof window.onunload != 'function') {
		window.onunload = func;
	}
	else {
		window.onunload = function() {
			oldOnunload();
			func();
		}
	}
}

function startPersistentSession(){
	window.setInterval("renewSession();",1200000);
}
function closePopUp(){
	if( oPopUp && !oPopUp.closed )
		oPopUp.close();
}

// Response per chiamata xmlHTTP
function requestCallBack(sData,sStatus){
	var responseText; // vedere come gestire il messaggio di errore
	switch(sStatus){
		case "success":
			var oResponse = $( document.createElement("div") ).append(sData);
			oResponse.children().each( function(){
				if(this.id){
					var o = $("#"+this.id);
					if(o)
						o.replaceWith( $(this) );
					// faccio il replace degli oggetti corrispondenti in pagina
				}
			});
			
			var oResponseText = oResponse.children("#responseText")[0];
			if(oResponseText)
				responseText = oResponseText.innerHTML;
		break;
		case "error":
			responseText = "CLIENT ERROR: "+sData.responseText;
		break;
	}
	return responseText;
}

// Post di un form in Ajax
function sendData(frm,bLightBox){
	var data = $("#"+frm.name).serialize();
	bLightBox = true;//!!bLightBox;
	var qs = frm.action.split("?");
	if(qs.length>1)
		qs = "?"+qs[1];
	else 
		qs = "";
	
	if(bLightBox)
		$.facebox(function(){ ajaxCall(qs,data); });
	else
		ajaxCall(qs,data);
	return false;
}

function sendGetData(qs){
	$.facebox(function(){ ajaxCall("?"+qs); });
	return false;
}

function ajaxCall(qs,data){
	data = data||"";
	$.ajax({
		async: true,
		error: function(XMLHttpRequest, textStatus) {
					showMessageInLightBox( requestCallBack(XMLHttpRequest, textStatus) );
				},//func(XMLHttpRequest, textStatus);
		success: function(data, textStatus) { 
					showMessageInLightBox( requestCallBack(data, textStatus) );
				},//func(data, textStatus),
		data: data,
		type: "POST",
		url: "ajaxRequests.asp"+qs,
		scriptCharset:"iso-8859-1"
	});
}

function showMessageInLightBox(text){
	// questa funzione serve per tutte le volte che voglio sovrascrivere la gif loading.. con un messaggio
	// dopo che ho finito l'operazione in corso
	var bLoading = $('#facebox .loading').length == 1;
	text = text||"";
	if(text.length){
		if( bLoading )
			$.facebox.reveal(text);
		else
			$.facebox(text);
	}else if(bLoading)
		$.facebox.close();
}

function showHiddenElement(id){
	var el = $("#"+id)[0]
	if(el)
		$.facebox(el.innerHTML);
	return false
}

// questo perche' ci possono essere degli script che vanno in conflitto per il metodo onload
var oPopUp;
addLoadEvent(startPersistentSession);
addLoadEvent(cookieAlert);
addUnloadEvent(closePopUp);





