String.prototype.trim =	function(){return this.replace(/^\s+|\s+$/g,"")}
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,bForceClose){
	// se ci sono dei DL e sono nello stato iniziale il box deve rimanere aperto in ogni caso
	// altrimenti viene chiuso/aperto in base alle specifiche del template oppure del bottone cliccato
	var fields = frm.getElementsByTagName("dl")
	if( bForceClose!=undefined ){
		if( bForceClose==false || fields.length )
			return;
	}
	for(var i=0;i<fields.length;i++)
		expandNode(null,null,fields[i]);
	
	var fields2 = frm.getElementsByTagName("fieldset");
	for(var i=0;i<fields2.length;i++)
		expandNode(null,null,fields2[i]);
	
	if( fields.length || fields2.length ){
		if( ( fields[0] || fields2[0] ).style.display=="none" )
			href.style.backgroundPosition = "0 -16px" ;
		else
			href.style.backgroundPosition = "0 0" ;
		
		href.blur();
	}
	return false;
}

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,customT3){
	customT1 = customT1>>0;
	customT2 = customT2>>0;
	var grid = document.getElementById("varaintsListBox");
	if(grid){
		var rowEls = grid.getElementsByTagName("TR");
		if(customT1) var rgx = new RegExp("variant_"+customT1+"_");
		else if(customT2) var rgx = new RegExp("_"+customT2+"_");
		else if(customT3) var rgx = new RegExp("_"+customT3+"$");
		// 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/wonder/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 advancedFormCheckTables(frm){
	if(frm){
		var chkVal;
		var els = frm.areaAndCategoryCondition;
		if(els){
			for(var i=0;i<els.length;i++)
				if(els[i].checked){
					chkVal = els[i].value;
					break;
				}
		}
		var els = frm.getElementsByTagName("select");
		for(var i=0,name;i<els.length;i=i+2){
			name = els[i].name || els[i+1].name;
			switch(chkVal){
				case "allAreas":
					els[i].style.display = "";
					els[i].name = name;
					els[i+1].style.display = "none";
					els[i+1].name = "";
				break;
				default:
					els[i].style.display = "none";
					els[i].name = "";
					els[i+1].style.display = "";
					els[i+1].name = name;
				break;
			}
		}
	}
}

function advancedFormCheckAreaOption(frm){
	if(frm){
		var els = frm.areaAndCategoryCondition;
		if(els){
			var val = querySt("areaAndCategoryCondition");
			for(var i=0;i<els.length;i++)
				if(els[i].value==val){
					els[i].checked = true;
					break;
				}
		}
	}
}

function formCheckSelectValueFromQS(frm){
	if(frm){
		var els = frm.getElementsByTagName("select");
		for(var i=0,name,o,val;i<els.length;i++){
			name = els[i].name;
			if(name){
				val = querySt(name);
				for(o=0;o<els[i].options.length;o++){
					if(els[i].options[o].value==val){
						els[i].options[o].selected = true;
						break;
					}
				}
			}
		}
	}
}
function advancedFormSetFilter(fld,value){
	if(fld){
		var frm = fld.form;
		if(frm){
			fld.value = value;
			// controllo che ci sia almeno un filtro impostato altrimenti non faccio eseguire la ricerca (togliendo il valore "searchProd" da cmd)
			var filterValues = [];
			for(var i=0;i<frm.elements.length;i++){
				switch(frm.elements[i].name){
					case "cmdSearchOK": case "cmdOP": case "cmd": case "areaAndCategoryCondition":
					break;
					case "cmdString": case "idC":
					default:
						filterValues.push(frm.elements[i].value);
					break;
				}
			}
			filterValues = filterValues.join("");
			if( filterValues.length==0 )
				frm.cmd.value = "";
			
			frm.submit();
		}
	}
	return false;
}
function setSelectOnchangeSubmit(frm){
	var selects = frm.getElementsByTagName("select");
	for(var i=0;i<selects.length;i++)
		selects[i].onchange = function(){ this.form.submit(); }
}
function brandFormInit(){
	var frm = document.getElementById('frmSearchBrand');
	if(frm){
		setSelectOnchangeSubmit(frm)
		formCheckSelectValueFromQS(frm);
		
		var flds = frm.getElementsByTagName("a")||[];
		for(var i=0,tmp,k,v;i<flds.length;i++){
			tmp = flds[i].id.split("_");
			if(tmp[0]=="filterLink"){
				flds[i].style.fontWeight = "normal";
				k = tmp[1];
				v = tmp[2];
				if( querySt(k)==v )
					flds[i].style.fontWeight = "bold";
			}
		}
	}
}
function advancedFormInit(frm){
	if(frm){
		//advancedFormCheckAreaOption(frm);
		//advancedFormCheckTables(frm);
		formCheckSelectValueFromQS(frm);
		// ^^ serve per valorizzare gli elenchi completi delle select (perche' sono memorizzate in cache)
		
		var defaultValue = string_urldecode(querySt("cmdString")||"");
		frm.cmdString.value = defaultValue;
		frm.onsubmit = function(){
			var bStringChanged = (frm.cmdString.value||"").trim() != defaultValue.trim();
			for(var i=0;i<frm.elements.length;i++){
				switch( frm.elements[i].name ){
					case "cmdSearchOK": case "cmdOP": case "cmd":
					case "areaAndCategoryCondition":
					case "cmdString": case "idC":
					break;
					default:
						// svuoto i filtri settati in precedenza nel caso la ricerca per stringa sia cambiata
						// solo per i filtri di link
						if( bStringChanged && frm.elements[i].type=="hidden" )
							frm.elements[i].value = "";
					break;
				}
			}
		}
	}
}
function string_urldecode (str) {
	str = str.replace('+', ' ');
	str = unescape(str);
	return str;
}

function querySt(ji) {
	var hu = window.location.search.substring(1);
	var gy = hu.split("&");
	for (var i=0,ft;i<gy.length;i++) {
		ft = gy[i].split("=");
		if (ft[0] == ji) {
			return ft[1];
		}
	}
}

function showHideDescription(oSelect){
	var els = oSelect.options;
	for(var i=0,desc,status;i<els.length;i++){
		desc = document.getElementById(oSelect.name+"Desc_"+els[i].value);
		if(desc){
			status = i==oSelect.selectedIndex?'':'none';
			desc.style.display = status;
		}
	}
}

function expandeDDElements(href,n){
	conteiner = href.parentNode.parentNode;
	if(conteiner){
		var els = conteiner.getElementsByTagName("dd");
		for(var i=n;i<els.length-1;i++){
			els[i].style.display = els[i].style.display=='none'?'':'none';
		}
		if(href.className=="open"){
			href.parentNode.className = "";
			href.style.display = 'none';
			href.previousSibling.style.display = '';
		}else{
			href.parentNode.className = "expandButton";
			href.style.display = 'none';
			href.nextSibling.style.display = '';
		}
	}
	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);


