/**
 * get cookie
 *
 */
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)
   }
}

/**
 * get cookie value
 *
 * CookieName      name of the cookie
 */
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 "";
}

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

/**
 * get yacas parameter
 *
 * pname     name of the parameter
 * pval      value of the parameter string
 */
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 "";	
}

/**
 * get user itol
 *
 */
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;
}

/**
 * get tim nickname
 *
 */
function getTimNickname() {
	var pval = getCookieValue("TIM_NICKNAME");
	var pString = '';
	for (i=0;i < (pval.length-1) ;i+=2){
	   pString = pString +"%"+ pval.substring(i,i+2);
	}	
	var user = unescape(pString);
	return user;
}

/**
 * get welcome message
 *
 */
function getWelcomeMessage() {
 	var login = 'n.d.';	
	if (checkCookie("TIM_NICKNAME")) {
		login = getTimNickname();
		if (login != '') {
			return login;
		} else {
			if(checkCookie("TIM_ID_YACAS")) {
				var c = getCookieValue("TIM_ID_YACAS");
				return getYacasPar("Msisdn", c);
			} 
		}
	} else if(checkCookie("TIM_ID_YACAS")) {
		var c = getCookieValue("TIM_ID_YACAS");
		return getYacasPar("Msisdn", c);
	} else if (checkCookie("ITOL_USER")) {
		return getUserItol();
	} else {
		return 'n.d.';
	}
}

/**
 * check if user TIM/ITOL is authenticated
 *
 */
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;
	}
}

/**
 * check if user ITOL is authenticated
 *
 */
function isAutenticatoItol() {
	if (checkCookie("ITOL_USER")) {
		return getUserItol();
	} else {
		return false;
	}
}

/**
 * check if user TIM is authenticated
 *
 */
function isAutenticatoIbox() {
	if (checkCookie("TIM_ID_YACAS")) {
		var c = getCookieValue("TIM_ID_YACAS");
		return getYacasPar("UserId", c);
	}
	else {
		return false;
	}
}

/**
 * delete all cookies
 */
/* 
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";	  
 }

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