function isEmpty(str){str = str||""; return (str.replace(/\s/g,"").length==0)}
function isMail(str){str = str||""; return str.search(/^([\w\.\-])+@(([\w\-]{2,})+\.)+([a-z]{2,})+$/i)!=-1}

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(e,id,obj){
	if(e){ // serve nel caso in cui ci siano elementi con lo stesso metodo (es: documenti)
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
	}
	if(id){
		// trasformo il contenuo del bottone
		var obj = document.getElementById("lnk_"+id);
		if(obj){
			switch(obj.innerHTML){
				case "+":
					obj.innerHTML = "-";
				break;
				case "-":
					obj.innerHTML = "+";
				break;
			}
		}
		obj = document.getElementById("row_"+id);
	}
	if(obj){
		// questo perche' gli elementi di tipo blocco in molti casi deveno mantenere il "block"
		var block = "";
		if((obj.nodeName).toUpperCase()=="FIELDSET")
			block = "block";
		obj.style.display = obj.style.display=="none"?block:"none";
	}
	return false;
}

function showAdvancedFields(frm,href){
	var fields = frm.getElementsByTagName("fieldset");
	for(var i=0;i<fields.length;i++)
		expandNode(null,null,fields[i]);
	if(fields[0].style.display=="none")
		href.style.backgroundPosition = "0 -16px" ;
	else
		href.style.backgroundPosition = "0 0" ;
	return false;
}

function enableMainFormQuickResults(){
	var mainFormSearch = document.getElementById('mainSearchForm_cmdString');
	if(mainFormSearch)
		mainFormSearch.onkeyup = startQuickSearch;
}

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();",60000);
}
function closePopUp(){
	if( oPopUp && !oPopUp.closed )
		oPopUp.close();
}

function getStyle(el,styleProp){
	if (el.currentStyle)
		var y = el.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
	return y;
}

function fieldChecked(fld,bWrong){
	bWrong = !!bWrong;
	var checkOK = template_var_skinFolder+"ico_ball_green2.gif";
	var checkKO = template_var_skinFolder+"ico_ball_red2.gif";
	var icoID = fld.name+"_check";
	var icoCheck = document.getElementById(icoID);
	// metto a verde solo i campi che erano stati segnalati
	if(!bWrong && icoCheck){
		fld.className = "correctValue";
		icoCheck.src = checkOK;
	}
	if(bWrong){
		if(!icoCheck){
			icoCheck = new Image();
			icoCheck.setAttribute("id",icoID);
			icoCheck.setAttribute("hspace",5);
			insertAfter(icoCheck,fld);
		}
		fld.className = "wrongValue";
		icoCheck.src = checkKO;
	}
}
function insertAfter(new_node, existing_node) {
	// if the existing node has a following sibling, insert the current
	// node before it. otherwise appending it to the parent node
	// will correctly place it just after the existing node.
	
	if (existing_node.nextSibling) {
	// there is a next sibling. insert before it using the mutual
	// parent's insertBefore() method.
		existing_node.parentNode.insertBefore(new_node, existing_node.nextSibling);
	} else {
	// there is no next sibling. append to the end of the parent's
	// node list.
		existing_node.parentNode.appendChild(new_node);
	}
}

function cryptString(s,bDecrypt){
	s = s||"";
	var newString = "";
	bDecrypt = !!bDecrypt;
	var cryptPrefix = "$";
	var krypt1 = "23BuvC_67X5Zab-fg8mYD4pRdhi.M9AEQPlNjk01/FTGJwxHIyK LceOqrstSVUWzno";
	var krypt2 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-. /";
	if(bDecrypt){
		if( s.indexOf(cryptPrefix)!=0 )
			return s;
		var temp = krypt1;
		krypt1 = krypt2;
		krypt2 = temp;
		s = s.substr(1);
	}else
		newString += cryptPrefix;
	for(var i=0,reg,index;i<s.length;i++){
		reg = new RegExp(s.charAt(i));
		index = krypt2.search(reg);
		newString += krypt1.charAt(index);
	}
	return newString;
}
function decryptString(s){
	return cryptString(s,true);
}

function resizeIframe(objID){
	var height;
	var obj = document.getElementById(objID);
	if( obj ){
		height = obj.contentWindow.document.body.scrollHeight;
		obj.style.height = height+"px";
	}
	return height;
}

function checkCustomTablesList(frm){
	frm = frm || document.forms["mainSearchFormBox_frm"];
	var chkVal = "";
	var els;
	if(frm){
		els = frm.areaAndCategoryCondition;
		if(els){
			for(var i=0;i<els.length;i++){
				if(els[i].checked){
					chkVal = els[i].value;
					break;
				}
			}
			if(chkVal){
				els = frm.elements;
				for(var i=0;i<els.length;i++){
					if(els[i].id.indexOf("allAreasCustomTable_")!=-1){
						if( chkVal=="allAreas" ){
							els[i].style.display = "";
							els[i].name = els[i].id.replace(/allAreasCustomTable_/,"");
						}else{
							els[i].style.display = "none";
							els[i].name = "";
						}
					}
					else if(els[i].id.indexOf("currentAreaCustomTable_")!=-1){
						if( chkVal=="currentArea" || chkVal=="category" ){
							els[i].style.display = "";
							els[i].name = els[i].id.replace(/currentAreaCustomTable_/,"");
						}else{
							els[i].style.display = "none";
							els[i].name = "";
						}
					}
				}
			}
		}
	}
}

// questo perche' ci possono essere degli script che vanno in conflitto per il metodo onload
var oPopUp;
addLoadEvent(startPersistentSession);
addLoadEvent(cookieAlert);
addUnloadEvent(closePopUp);









