/* cookies global var  */
var expDate= new Date(2050,11,11);
var min=11;
var max=20;

endCookie = function() {
	if (event.clientY < 0){
		//alert("Delete Cookies");
		setCookie("fontsize",0,expDate,"/");
		//deleteCookie("fontsize");
	}
	
}
keyPressFunction = function(){
	var alt = window.event.altKey;
	var tecla = window.event.keyCode;
	if(alt){// delete cookie for ALT+F4
		if(tecla==115){
			setCookie("fontsize",0,expDate,"/");
		}
		
	}
}
window.onunload=endCookie;
//window.onload=endCookie;
document.onkeydown=keyPressFunction;

function setCookie(name, value, expires, path, domain, secure){
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}  

function getCookie(name){
	    var dc = document.cookie;
	    var prefix = name + "=";
	    var begin = dc.indexOf("; " + prefix);
	    if (begin == -1)
	    {
	        begin = dc.indexOf(prefix);
	        if (begin != 0) return null;
	    }else{
	        begin += 2;
	    }
	    var end = document.cookie.indexOf(";", begin);
	    if (end == -1){
	        end = dc.length;
	    }
	    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain){
	if (getCookie(name)){
		document.cookie = name + "=" + 
			((path) ? "; path=" + path : "") + 
			((domain) ? "; domain=" + domain : "") + 
			"; expires=Thu, 01-Jan-1970 00:00:01 GMT";
		
		}
}
	
// Text Resizer function
// set text size when loading page; override the font-size set in the css;
function initTextSize(){
	if(history.length<=1){
		setCookie("fontsize",0,expDate,"/");
	}
	var fontSize = getCookie("fontsize");
	if(fontSize != null && fontSize!=0){
		resizeData(fontSize);
		}
	
}
//call the function
initTextSize();

function decreaseFontSize(){
	
	var size = parseInt(getCookie("fontsize")); 
	if(size==0 || isNaN(size)){
		size = min;
	 }
	if(size>=min) {
         size -= 1;
      }	
	resizeData(size);
	deleteCookie("fontsize");
	setCookie("fontsize",size,expDate,"/");
}
function increaseFontSize(){
	 var size = parseInt(getCookie("fontsize")); 
	if(size==0 || isNaN(size)){
		size = min;
	 }
	 if(size<=max) {
         size += 1;
      }	
	 resizeData(size);
	 deleteCookie("fontsize");
	 setCookie("fontsize",size,expDate,"/");
}

function resizeData(theSize){
	//alert("resizeData::"+theSize);
	 var body = document.getElementsByTagName('body');
	 var p = document.getElementsByTagName('p');
     var a = document.getElementsByTagName('a');
	 var td = document.getElementsByTagName('td');
	 if(body){
		 for(i=0;i<body.length;i++) {
					body[i].style.fontSize = theSize+"px"
		 }
	 }
	 if(p){
		 for(i=0;i<p.length;i++) {
				p[i].style.fontSize = theSize+"px"
		  }
	}
	if(a){
			 for(i=0;i<a.length;i++) {
				a[i].style.fontSize = theSize+"px"
		   }
	}
	if(td){
			 for(i=0;i<td.length;i++) {
				td[i].style.fontSize = theSize+"px"
		   }
	}
}
