/** 
 * Gerencia os cookies
 * Grava, buscar, apaga
 * Ex. MyCookie.setCookie('nomeDoCookie','valor',diasParaExpirar);
 */
var MyCookie = {
	
	date: new Date(),

	setCookie: function(name, value, expire) {
		if (expire) {
			this.date.setTime(this.date.getTime()+(expire*24*60*60*1000));
			var expires = "; expires=" + this.date.toGMTString();
		} else {
			var expires = "";
		}
		document.cookie = name + "=" + value + expires + "; path=/";
	},

	getCookie: function(name) {
		value = "";
		if (document.cookie) {
			 var arrCookie = document.cookie.split(escape(name) + '=');
			 if ( 2 <= arrCookie.length ) {
				var arrCookie2 = arrCookie[1].split(';');
				value = unescape(arrCookie2[0]);
			 }
		}
		return value;
	},

	delCookie: function(name) {
		this.setCookie(name,"",0);
	}
	
}

function cookieCL(content){

	var isCheck = document.getElementById('cookie').checked;
	content = (content != undefined) ? content : 'Y';
	
	if(isCheck){
		MyCookie.setCookie('UserCL',content,30);
	} else {
		MyCookie.setCookie('UserCL',content,0);
	}
}

function verifyUserCL(){
	var ckValue = MyCookie.getCookie('UserCL');
	//alert(ckValue);
	if (ckValue == 'Y'){
		return true;
	} else if (ckValue != ''){
		window.location.assign(ckValue);
	} else {
		return false;
	}
}

if ((geoip_country == 'CL') && (!verifyUserCL())) {
	document.write('<div id="BGalerta"></div>');
	document.write('<div id="POPalerta">');
	document.write('<strong>Estimado usuario,</strong>');
	document.write('<div>Nuestros registros indican que estás en Chile y por eso queremos invitarte a conocer Terra Chile.</div> ');
	document.write('<div><a href="http://www.terra.com/redirect/redcl.htm" onclick="cookieCL(this.href);">Visitar Terra Chile</a></div>');
	document.write('<div>Si prefieres seguir navegando en Terra USA, por favor haz click en el link abajo:</div>');
 	document.write('<div><a href="http://www.terra.com/redirect/redus_cl.htm" onclick="cookieCL();">Seguir en Terra USA</a><br /></div>');
	document.write('<div>Muchas gracias!<br></div>');
	document.write('<div><br></div>');
	document.write('<form><label for="cookie"><input type="checkbox" value="" id="cookie" onclick="cookieCL();"> No mostrar más este aviso</label></form>');
	document.write('</div>');
}
