function btnCotizar(idprod, descripcion, cotizar){
	var cookie = 'pac[' + idprod + '][' + descripcion + ']';
	if(cotizar){
		 createCookie(cookie, 1, 0);
		 var html = "<a href=\"javascript:void(0);\" onClick=\"btnCotizar('" + idprod + "', '" + descripcion + "', false);\">No Cotizar</a>";
	}else{
		 createCookie(cookie, '', -1);
		 var html = "<a href=\"javascript:void(0);\" onClick=\"btnCotizar('" + idprod + "', '" + descripcion + "', true);\">Cotizar</a>";
	}
	if (document.getElementById('btnpac[' + idprod + ']')){
		document.getElementById('btnpac[' + idprod + ']').innerHTML = html;
	}
	if (document.getElementById('frmpac')){
		window.location.reload();
	}
	muestrapac();
}

function muestrapac(){
	var ca = document.cookie.split(';');
	var htmlpac = '<table width=\"100%\" cellpadding=\"4\" cellspacing=\"0\" class=\"tabla\" style=\"margin-bottom:5px;\">\n<tr><th bgcolor=\"#EAEAEA\" class=\"margen_titulo\" scope=\"col\">Productos a cotizar</th></tr>\n';
	var haypac = false;
	for(var i=0;i < ca.length;i++) {
		//recorro las cookies buscando productos a cotizar
		var x = ca[i];
		x = x.substring(1,4);
		if (x == 'pac'){
			haypac = true;
			//obtengo el idproducto y la descripcion de la cookie actual
			var a = ca[i].split('['); var idprod = a[1]; var descripcion = a[2];
			idprod = idprod.substring(0,idprod.length -1);
			descripcion = descripcion.substring(0,descripcion.length -3);
			//escribo la fila del producto en pac
			htmlpac = htmlpac + "	<tr><td style=\"border-top-width:1px;border-top-style:dotted;border-top-color:#999999;\"><div style=\"float:left;margin-right:4px\"><a href=\"javascript:void(0);\" onclick=\"btnCotizar('" + idprod + "', '" + descripcion + "', false);\" class=\"Vinculo_en_tabla\">(X)</a></div><div align=\"left\"><a href=\"ficha_producto.php?idproducto=" + idprod + "\" class=\"margen_texto\">" + unescape(descripcion) + "</a></div></td></tr>\n";
		}
	}
	htmlpac = htmlpac + "<tr><td style=\"border-top-width:1px;border-top-style:dotted;border-top-color:#999999;\"><div align=\"center\"><a href=\"productos_cotizar.php?op=cpac\" class=\"Vinculo_en_tabla\">Confirmar pedido de cotización</a></div></td></tr>\n</table>";
	//armo footerpacdiv
	var htmlfooterpac = "<table width=\"100%\" cellpadding=\"4\" cellspacing=\"0\" style=\"border-bottom-style:solid;border-color:#CCCCCC;border-width:1px;background-color:#990000;\">\n<tr><td style=\"border-top-width:1px;border-top-style:dotted;border-top-color:#999999;\"><div align=\"center\"><a href=\"productos_cotizar.php?op=cpac\" style=\"text-decoration:none;font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px;color:#FFF;\">Confirmar pedido de cotización</a></div></td></tr>\n</table>";
		document.getElementById("pacdiv").innerHTML = htmlpac;
		document.getElementById("footerpacdiv").innerHTML = htmlfooterpac;
	//evaluo si muestro pac en relacion a la existencia de pac
	if (haypac){
		document.getElementById("pacdiv").style.display = 'block';
		document.getElementById("footerpacdiv").style.display = 'block';
	}else{
		document.getElementById("pacdiv").style.display = 'none';
		document.getElementById("footerpacdiv").style.display = 'none';
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function hideDiv(pass) { 
var divs = document.getElementsByTagName('div'); 
for(i=0;i<divs.length;i++){ 
if(divs[i].id.match(pass)){//if they are 'see' divs 
if (document.getElementById) // DOM3 = IE5, NS6 
divs[i].style.display="none";// show/hide 
else 
if (document.layers) // Netscape 4 
document.layers[divs[i]].display = 'none'; 
else // IE 4 
document.all.hideShow.divs[i].display = 'none'; 
} 
} 
} 

function showDiv(pass) { 
var divs = document.getElementsByTagName('div'); 
for(i=0;i<divs.length;i++){ 
if(divs[i].id.match(pass)){ 
if (document.getElementById){ 
divs[i].style.display="block";
}else{ 
if (document.layers){ // Netscape 4 
document.layers[divs[i]].display = 'block'; 
}else{ // IE 4 
document.all.hideShow.divs[i].display = 'block'; 
}
}
} 
} 
} 

function postwith (to,p) {
  var myForm = document.createElement("form");
  myForm.method="post" ;
  myForm.action = to ;
  for (var k in p) {
    var myInput = document.createElement("input") ;
    myInput.setAttribute("name", k) ;
    myInput.setAttribute("value", p[k]);
    myForm.appendChild(myInput) ;
  }
  document.body.appendChild(myForm) ;
  myForm.submit() ;
  document.body.removeChild(myForm) ;
//<a href="javascript:postwith('post.aspx',{user:'peter',cc:'aus'})">click</a>
}

function saveScrollCoordinates(){ 
  // Firefox, Chrome, Opera, Safari
  if (window.pageYOffset){
	  createCookie('scrollx', window.pageXOffset, 0);
	  createCookie('scrolly', window.pageYOffset, 0);
  }
  // IE 6
  if (document.documentElement && document.documentElement.scrollTop){
	  createCookie('scrollx', document.documentElement.scrollLeft, 0);
	  createCookie('scrolly', document.documentElement.scrollTop, 0);
  }
  // Other IE
  if (document.body.scrollTop){
	  createCookie('scrollx', document.body.scrollLeft, 0);
	  createCookie('scrolly', document.body.scrollTop, 0);
  }
} 

function scrollToCoordinates(){ 
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var x = ca[i].substring(1,8);
		if (x == 'scrollx'){
			var a = ca[i].split('=');
			var scrollx = a[1];
		}
		if (x == 'scrolly'){
			var a = ca[i].split('=');
			var scrolly = a[1];
		}	
	}
	if (scrolly != ''){
		window.scrollTo(scrollx, scrolly);
		createCookie('scrollx', '', -1);
		createCookie('scrolly', '', -1);
	}else{
		window.scrollTo(0, 0);
	}
}

function confirmar(url, pregunta) {
	var respuesta = confirm(pregunta);
	if (respuesta){
		window.location = url;
	}
}

function validarCampo(valor, campo) {
	var expr = /^([a-zA-Z0-9_.-])+$/;
	if (expr.test(valor)){
		document.getElementById(campo).style.backgroundColor = '';
		document.getElementById("btnSubmit").disabled = false;
	} else {
		alert("Sólo se permiten caracteres alfanuméricos, \"_\", \".\" y \"-\".");
		document.getElementById(campo).style.backgroundColor = '#CF9';
		document.getElementById("btnSubmit").disabled = true;
	}
}

function validarEmail(valor, campo) {
	if (valor == '') {
		document.getElementById(campo).style.backgroundColor = '';
		document.getElementById("btnSubmit").disabled = false;
	} else {
		var expr = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		if (expr.test(valor)){
			document.getElementById(campo).style.backgroundColor = '';
			document.getElementById("btnSubmit").disabled = false;
		} else {
			alert("La dirección de email es incorrecta.");
			document.getElementById(campo).style.backgroundColor = '#CF9';
			document.getElementById("btnSubmit").disabled = true;
		}
	}
}

function validarVacios() {
	var valido = true;
	for (i=0; i < arguments.length; i++) {
		var campo = document.getElementById(arguments[i]);
		var str = campo.value;
		str = str.replace(/^\s*|\s*$/g,"");
		if (str == ''){
			campo.style.backgroundColor = '#CF9';
			valido = false;
		}else{
			campo.style.backgroundColor = '';
		}
	}
	if (!valido){
		alert("No ha completado los campos obligatorios marcados en color.");
	}
	return valido;
}

function validarFecha(valor){
	var valido = true;
	var checkstr = "0123456789";
	var Datevalue = "";
	var DateTemp = "";
	var seperator = ".";
	var day;
	var month;
	var year;
	var leap = 0;
	var err = 0;
	var i;
	err = 0;
	DateValue = valor;
	/* Borra todos los caracteres excepto 0..9 */
	for (i = 0; i < DateValue.length; i++) {
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
		 DateTemp = DateTemp + DateValue.substr(i,1);
	  }
	}
	DateValue = DateTemp;
	/* Valida formato de 8 digitos */
	if (DateValue.length != 8) {
	  err = 19;}
	/* El año esta mal si = 0000 */
	year = DateValue.substr(4,4);
	if (year == 0) {
	  err = 20;
	}
	/* Validacion de mes*/
	month = DateValue.substr(2,2);
	if ((month < 1) || (month > 12)) {
	  err = 21;
	}
	/* Validacion de dia*/
	day = DateValue.substr(0,2);
	if (day < 1) {
	 err = 22;
	}
	/* Validacion año bisiesto / febrero / dia */
	if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
	  leap = 1;
	}
	if ((month == 2) && (leap == 1) && (day > 29)) {
	  err = 23;
	}
	if ((month == 2) && (leap != 1) && (day > 28)) {
	  err = 24;
	}
	/* Validacion del resto de los meses */
	if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
	  err = 25;
	}
	if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
	  err = 26;
	}
	/* Si se ingresa 00, no hay error */
	if ((day == 0) && (month == 0) && (year == 00)) {
	  err = 0;
	}
	if (err != 0) {
	  valido = false;
	}
	if (!valido){
		alert("La fecha ingresada es incorrecta! (Error: " + err + ")");
	}
	return valido;
}

