function pegaQuery(param){
	var query=window.location.search;
	//document.write(query+" **<br><br>");
	var str=param+"=";
	//document.write(str+" //<br>");
	var comeco = query.indexOf(str);
	var ini = comeco+str.length;
	//document.write(ini+" -->ini<br>");
	if( comeco == -1) {
		return "x0";
	}
	else {
		
		var fim = query.length - str.length -1;
		//document.write(fim+" -->fim<br>");
		var aux = "";
		if(fim<=ini){
			fim = query.length;
		}
		aux=query.slice(ini,fim);
		//document.write(aux+" -->aux<br>");
		var loc = aux.indexOf("&");
		//document.write(loc+" -->loc<br>");
		if(loc != -1) {
			ini = 0;
			fim = loc;
			aux = aux.slice(ini, fim);	
		}
		return aux;
	}
}

/****** Manutenção de Cookie ******/

// name - Nome do cookie
// value - Valor do cookie
// [expires] - Data de Expiração (opcional)
// [domain] - Dominio (opcional) 
// Grava o cookie, com expiração
function setCookie(name, value, expires, domain) {
	var curCookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((domain) ? "; domain=" + domain : "") ;
	document.cookie = curCookie;
}

// name - Nome do cookie
// Retorna o valor do Cookie do parametro, ou null se não existir
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));
}

// name - Nome do Cokie
// [domain] - Dominio, mesmo usado na  criação (opcional) 
// Cookie é apagado
function deleteCookie(name, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" + 
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
		history.go(0);
	}
}

function fixDate(date) {
	var base = new Date(0);
	var skew = base.getTime();
	if (skew > 0)
		date.setTime(date.getTime() - skew);
}

function saveCookies(){
	var now = new Date();
	var param = pegaQuery('codPiece');
	var retorno = "";	
	fixDate(now+1);
	now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
	if(param!="x0"){
		setCookie('codPiece', param, now);
	}
}

function LevaCookies(Acao){
	var sCookie = getCookie('codPiece');
	var retorno = "";
	if(sCookie!=null){ // tem cookie gravado,
		retorno = "http://www.id360.com.br/idlinks/Public/Process.aspx?action=save&codPiece="+sCookie+"&codAction="+Acao ;
	}else{ // Não tem cookie, mandar 0 como CodPiece
		retorno = "http://www.id360.com.br/idlinks/Public/Process.aspx?action=save&codPiece=0&codAction="+Acao ;
	}
	// document.Resposta.location = retorno;
	document.getElementById("IFrame1").src = retorno; 
}

function setValueWithCookie(){
	var sCookie = getCookie('codPiece');
	// document.Resposta.location = retorno;
	document.getElementById("CodPeca").value = sCookie; 
}


function pop(x,w,h) {
	var l = ( (window.screen.width  - w) / 2 );
	var t = ( (window.screen.height - h) / 2 );
    janelinha = window.open(x,'','leftmargin=0,topmargin=0,marginheight=0,marginwidth=0,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,top='+t+',left='+l+',screenX='+l+',screenY='+t+',copyhistory=no,height='+h+',width='+w+'');
	janelinha.close();
}