function checkCookie(cname) {	    
	var c=document.cookie, b=c.indexOf(cname+"=");		
	if(b>-1){ 
	   return true;    	
	}	 	
	return false;
}

function getCookieValue(CookieName) {	  
    var CookieString = document.cookie; 
    var CookieSet = CookieString.split (';');
    var SetSize = CookieSet.length;
    var CookiePieces;     
    for (x = 0; x < SetSize; x++) {
      CookiePieces = CookieSet[x].split ('=');	   
      if (CookiePieces[0].substring (0,1) == ' ') {
        CookiePieces[0] = CookiePieces[0].substring (1, CookiePieces[0].length);		 
      }	  
	  //alert(CookiePieces[0]);
      if (CookiePieces[0] == CookieName) {	  	    
        return CookiePieces[1];
      }
    }
    return "";
}

/*
function deleteAllCookie(path,domain){
 var CookieString = document.cookie;
 var CookieSet = CookieString.split (";");
 var SetSize = CookieSet.length;
 var CookiePieces;
 for (x = 0; x < SetSize; x++) {
   CookiePieces = CookieSet[x].split ("=");
   var name = CookiePieces[0];
    document.cookie = name + "=" +
   ((path) ? "; path=" + path : "") +
   ((domain) ? "; domain=" + domain : "") +
   "; expires=Fri, 02-Jan-1970 00:00:00 GMT";
 } 
}
*/

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
 function deleteCookie(name, path, domain, secure){	  
     document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "") +
    "; expires=Fri, 02-Jan-1970 00:00:00 GMT";	  
 }			 


function getYacasPar(pname,pval) {    
	var pString='';
	for (i=0;i < (pval.length-1) ;i+=2){
	   pString = pString +"%"+ pval.substring(i,i+2);
	}	
	var CookieString = unescape(pString);
	var CookieSet = CookieString.split (';');
    var SetSize = CookieSet.length;
    var CookiePieces;
    for (x = 0; x < SetSize; x++) {
      CookiePieces = CookieSet[x].split (':=');      
      if (CookiePieces[0] == pname) {	    
        return CookiePieces[1];
      }
    }
    return "";	
}

function getUserItol() {
	var pval = getCookieValue("ITOL_USER");
	var pString = '';
	for (i=0;i < (pval.length-1) ;i+=2){
	   pString = pString +"%"+ pval.substring(i,i+2);
	}	
	var user = unescape(pString);
	if (user.length > 11) {
		user = user.substring(0,10)+ "...";
	}
	return user;
}

function isAutenticato() {
    var login;	
	if(checkCookie("TIM_ID_YACAS")) {
		var c = getCookieValue("TIM_ID_YACAS");
		return getYacasPar("Msisdn", c);
	} else if (checkCookie("ITOL_USER")) {
		return getUserItol();
	} else {
		return false;
	}
}

function isAutenticatoItol() {
	if (checkCookie("ITOL_USER")) {
		return getUserItol();
	} else {
		return false;
	}
}

function isAutenticatoIbox() {
	if (checkCookie("TIM_ID_YACAS")) {
		var c = getCookieValue("TIM_ID_YACAS");
		return getYacasPar("UserId", c);
	}
	else {
		return false;
	}
}

function redirect(url){
	window.location=url;
	return false;
}

function printFormLogin(urlLogin, urlOk, urlRegistrati, urlHaiPersoLaPassword) {
if (isAutenticato() == false) {
	var testoForm ="<form action=\""+urlLogin+"\" method=\"post\">\n"
				 + "<fieldset>\n"
				 + "<label for=\"login\" class=\"bold color_blue\">Username</label>\n"
				 + "<input type=\"text\" id=\"username\" name=\"login\" class=\"inputlog color_gray\" value=\"inserisci username\"\n"
				 + "onfocus=\"if (this.value == 'inserisci username') this.value='';\" />\n"
				 + "\n"
				 + "<label for=\"password\" class=\"bold color_blue\">Password</label>\n"
				 + "<input type=\"password\" id=\"passwd\" name=\"password\" class=\"inputlog color_gray\" value=\"inserisci password\"\n"
				 + "onfocus=\"if (this.value == 'inserisci password') this.value='';\" />\n"
				 + "<input type=\"hidden\" name=\"portale\" value=\"timPortale\" />"
				 + "<input type=\"hidden\" name=\"urlOk\" value=\""+urlOk+"\" />"
				 + "<div id=\"butts_login\">\n"
				 + "<input type=\"button\" onclick=\"redirect('"+urlRegistrati+"');\" id=\"lastbtt\" name=\"btt_lastbtt\" class=\"bttlogin color_gray\" title=\"Registrati\" value=\"REGISTRATI\" />\n"
				 + "<input type=\"submit\" id=\"firstbtt\" name=\"btt_firstbtt\" class=\"bttlogin color_gray\" title=\"Entra\" value=\"ENTRA\" />\n"
				 + "</div>\n"
				 + "<div id=\"recpwd\">"
				 + "<a href=\""+urlHaiPersoLaPassword+"\" title=\"Hai perso la password\">Hai perso la password?</a>\n"
				 + "</div>\n"
				 + "<div class=\"clearing\"></div>\n"
				 + "</fieldset>\n"
				 + "</form>";
	
	document.write(testoForm);
 }
}

function promoPinGluePin(doc) {
	var totalPin = doc.getElementById("totalPin");
	var pin1 = doc.getElementById("conPin1");
	var pin2 = doc.getElementById("conPin2");
	var pin3 = doc.getElementById("conPin3");
	totalPin.value=pin1.value+pin2.value+pin3.value;
	alert(totalPin);
	return true;
}

function getCookie(Name) {
   var search = Name + "="
   if (document.cookie.length > 0) { // if there are any cookies
      offset = document.cookie.indexOf(search) 
      if (offset != -1) { // if cookie exists 
         offset += search.length 
         // set index of beginning of value
         end = document.cookie.indexOf(";", offset) 
         // set index of end of cookie value
         if (end == -1) 
            end = document.cookie.length
         return unescape(document.cookie.substring(offset, end))
      } else { return (-1)
	  }
   } else { return (-1)
   }
}

function popup3(url, name) {
 if ( getCookie('EMAIL') != "-1") {
    url = url + '&mail=' + getCookie('EMAIL');
  } else {
   url = url + '&mail=0';
  }
 //alert(url);
 myWin = open(url,name,'width=626,height=450,status=no,toolbar=no,menubar=no,resizable=no,left=150,top=200');
}

function openPopupGeneral(url, width, height) {
   window.open(url, '', 'left=100,top=100,width='+width+',height='+height+',resizable=no,scrollbars=no,location=no,toolbar=no');
   return false;
}

function openCatalogoServizi(url, isPopUp, width, height){
	if(isPopUp){
		window.open(url, '', 'left=100,top=100,width='+width+',height='+height+',resizable=no,scrollbars=no,location=no,toolbar=no');
	}
	else {
		window.open(url);
	}

}

function getQueryStirng(){
	var url=document.URL;
	if(url.indexOf("?")!=-1)
		return escape(url.split("?")[1]);
	else return "";
}

function confirmAction()
	{
	var user="";
	action=confirm('Sei autenticato?');
	if (action==true)
		user="autenticato";
	if (action==false)
		user="nonAutenticato";
	return user;
	}

function Popup(url, width, height) {
    window.open(url,"","width=" + width + ",height=" + height);
} 