//Post-it only once per browser session? (0=no, 1=yes)
//Specifying 0 will cause postit to display every time page is loaded

var coeficiente = 1;
var once_per_browser = 0;
///No need to edit beyond here///
var ie4 = document.all;
var ns6 = document.getElementById&&!document.all;

var postitActivo = "vacio";

if (ie4||ns6)
	var crossobj = document.getElementById(postitActivo);
	
function eligePostit(cual) {
	postitActivo = cual;

	if (ie4||ns6)
		crossobj = document.getElementById(cual);
}

function closeit() {
	if (ie4||ns6)
		crossobj.style.visibility = "hidden";
}

function get_cookie(Name) {

	var search = Name + "=";
	var returnvalue = "";
	if (document.cookie.length > 0) {
		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;
			returnvalue = unescape(document.cookie.substring(offset, end));
		}
	}
	return returnvalue;
}

function showornot(){
	if (get_cookie('postdisplay')=='') {
		showit();
		document.cookie = "postdisplay=yes";
	}
}

function showit() {
	if (ie4||ns6)
		crossobj.style.visibility = "visible";
}

if (once_per_browser)
	showornot();
else
	showit();

//drag drop function for ie4+ and NS6////
/////////////////////////////////

function drag_drop(e) {
	if (ie4&&dragapproved) {
		if (((tempx+event.clientX-offsetx) >= 16) && ((tempx+event.clientX-offsetx) <= 334))
			crossobj.style.left = tempx+event.clientX-offsetx;
		if ((tempy+event.clientY-offsety) >= 325)
			crossobj.style.top = tempy+event.clientY-offsety;
		return false;
	} else if (ns6&&dragapproved) {
		if (((tempx+e.clientX-offsetx) >= 16) && ((tempx+e.clientX-offsetx) <= 334))
			crossobj.style.left = tempx+e.clientX-offsetx + 'px';
		if ((tempy+e.clientY-offsety) >= 325)
			crossobj.style.top = tempy+e.clientY-offsety;
		return false;
	}
}

function initializedrag(e) {
	if (ie4&&event.srcElement.id==postitActivo||ns6&&e.target.id==postitActivo) {
		offsetx = ie4?event.clientX:e.clientX;
		offsety = ie4?event.clientY:e.clientY;
		tempx = parseInt(crossobj.style.left);
		tempy = parseInt(crossobj.style.top);
		dragapproved = true;
		document.onmousemove = drag_drop;
	}
}

function actualizaValores(cual) {
	// PHP genera en conexion.php el array porcent
	var posicion = parseInt(cual.style.left);
	if (cual.id.split('_')[1] == 0) {
		if (posicion <= 28) {
			document.getElementById('elImporte').value = '0';
			crossobj.style.left = '16px';
		} else if (posicion <= 53) {
			document.getElementById('elImporte').value = '6000';
			crossobj.style.left = '40px';
			if (document.getElementById('entrada') != null)
				document.getElementById('entrada').value = '6000';			
		} else if (posicion <= 83) {
			document.getElementById('elImporte').value = '9000';
			crossobj.style.left = '66px';
			if (document.getElementById('entrada') != null)
				document.getElementById('entrada').value = '9000';
		} else if (posicion <= 117) {
			document.getElementById('elImporte').value = '12000';
			crossobj.style.left = '100px';
			if (document.getElementById('entrada') != null)
				document.getElementById('entrada').value = '12000';
		} else if (posicion <= 151) {
			document.getElementById('elImporte').value = '15000';
			crossobj.style.left = '134px';
			if (document.getElementById('entrada') != null)
				document.getElementById('entrada').value = '15000';			
		} else if (posicion <= 184) {
			document.getElementById('elImporte').value = '18000';
			crossobj.style.left = '167px';
			if (document.getElementById('entrada') != null)
				document.getElementById('entrada').value = '18000';			
		} else if (posicion <= 218) {
			document.getElementById('elImporte').value = '21000';
			crossobj.style.left = '201px';
			if (document.getElementById('entrada') != null)
				document.getElementById('entrada').value = '21000';			
		} else if (posicion <= 253) {
			document.getElementById('elImporte').value = '24000';
			crossobj.style.left = '236px';
			if (document.getElementById('entrada') != null)
				document.getElementById('entrada').value = '24000';			
		} else if (posicion <= 288) {
			document.getElementById('elImporte').value = '27000';
			crossobj.style.left = '271px';
			if (document.getElementById('entrada') != null)
				document.getElementById('entrada').value = '27000';			
		} else if (posicion <= 319) {
			document.getElementById('elImporte').value = '30000';
			crossobj.style.left = '305px';
			if (document.getElementById('entrada') != null)
				document.getElementById('entrada').value = '30000';			
		} else if (posicion <= 333) {
			document.getElementById('elImporte').value = '33000';
			crossobj.style.left = '333px';
			if (document.getElementById('entrada') != null)
				document.getElementById('entrada').value = '33000';			
		}
	} else {
		if (posicion <= 35) {
			document.getElementById('elPlazo').value = '0';
			if (document.getElementById('plazoPeq') != null)
				document.getElementById('plazoPeq').value = '0';
			crossobj.style.left = '16px';
			coeficiente = 1;
		} else if (posicion <= 74) {
			document.getElementById('elPlazo').value = '12';
			if (document.getElementById('plazoPeq') != null)
				document.getElementById('plazoPeq').value = '12';
			crossobj.style.left = '54px';
			coeficiente = porcent[2] /*1.0874824*/;
		} else if (posicion <= 114) {
			document.getElementById('elPlazo').value = '24';
			if (document.getElementById('plazoPeq') != null)
				document.getElementById('plazoPeq').value = '24';
			crossobj.style.left = '94px';
			coeficiente = porcent[3] /*1.0448866*/;
		} else if (posicion <= 154) {
			document.getElementById('elPlazo').value = '36';
			if (document.getElementById('plazoPeq') != null)
				document.getElementById('plazoPeq').value = '36';
			crossobj.style.left = '134px';
			coeficiente = porcent[4] /*1.03070103*/;
		} else if (posicion <= 194) {
			document.getElementById('elPlazo').value = '48';
			if (document.getElementById('plazoPeq') != null)
				document.getElementById('plazoPeq').value = '48';
			crossobj.style.left = '175px';
			coeficiente = porcent[5] /*1.02361795*/;
		} else if (posicion <= 234) {
			document.getElementById('elPlazo').value = '60';
			if (document.getElementById('plazoPeq') != null)
				document.getElementById('plazoPeq').value = '60';
			crossobj.style.left = '214px';
			coeficiente = porcent[6] /*1.01937586*/;
		} else if (posicion <= 274) {
			document.getElementById('elPlazo').value = '72';
			if (document.getElementById('plazoPeq') != null)
				document.getElementById('plazoPeq').value = '72';
			crossobj.style.left = '254px';
			coeficiente = porcent[7]/*1.01666993*/;
		} else if (posicion <= 314) {
			document.getElementById('elPlazo').value = '84';
			if (document.getElementById('plazoPeq') != null)
				document.getElementById('plazoPeq').value = '84';
			crossobj.style.left = '294px';
			coeficiente = porcent[8]/*1.01466209*/;
		} else if (posicion <= 334) {
			document.getElementById('elPlazo').value = '96';
			if (document.getElementById('plazoPeq') != null)
				document.getElementById('plazoPeq').value = '96';
			crossobj.style.left = '334px';
			coeficiente = porcent[9]/*1.01320957*/;			
		}
	}
	if (document.getElementById('elPlazo').value != 0)
		calcular();
}

function introducirImporte() {
	var importe = parseInt(document.getElementById('elImporte').value);
	crossobj = document.getElementById('flecha_0');
	if (importe <= 6000) {
		crossobj.style.left = parseInt(importe*24/6000) + 16 + 'px';
	} else if ((importe > 6000) && (importe <= 33000)) {
		if (importe == 9000)
			crossobj.style.left = '66px';
		else if (importe == 12000)
			crossobj.style.left = '100px';
		else if (importe == 15000)
			crossobj.style.left = '134px';
		else if (importe == 18000)
			crossobj.style.left = '167px';
		else if (importe == 21000)
			crossobj.style.left = '201px';
		else if (importe == 24000)
			crossobj.style.left = '236px';
		else if (importe == 27000)
			crossobj.style.left = '271px';
		else if (importe == 30000)
			crossobj.style.left = '305px';
		else if (importe == 33000)
			crossobj.style.left = '333px';
		else
			crossobj.style.left = parseInt((importe-6000)*293/27000) + 40 + 'px';
	} else if (importe > 33000) {
		crossobj.style.left = '333px';
	}
	if (document.getElementById('entrada') != null)
		document.getElementById('entrada').value = importe;
	if (document.getElementById('elPlazo').value != 0)
		calcular();
	crossobj = document.getElementById('vacio');
}

function introducirPlazo() {
	var plazo = parseInt(document.getElementById('elPlazo').value);
	crossobj = document.getElementById('flecha_1');
	if (plazo <= 6) {
		document.getElementById('elPlazo').value = '0';
		if (document.getElementById('plazoPeq') != null)
			document.getElementById('plazoPeq').value = '0';
		crossobj.style.left = '16px';
		coeficiente = 1;
	} else if (plazo <= 18) {
		document.getElementById('elPlazo').value = '12';
		if (document.getElementById('plazoPeq') != null)
			document.getElementById('plazoPeq').value = '12';
		crossobj.style.left = '54px';
		coeficiente = porcent[2] /*1.0874824*/;
	} else if (plazo <= 30) {
		document.getElementById('elPlazo').value = '24';
		if (document.getElementById('plazoPeq') != null)
			document.getElementById('plazoPeq').value = '24';
		crossobj.style.left = '94px';
		coeficiente = porcent[3] /*1.0448866*/;
	} else if (plazo <= 42) {
		document.getElementById('elPlazo').value = '36';
		if (document.getElementById('plazoPeq') != null)
			document.getElementById('plazoPeq').value = '36';
		crossobj.style.left = '134px';
		coeficiente = porcent[4] /*1.03070103*/;
	} else if (plazo <= 54) {
		document.getElementById('elPlazo').value = '48';
		if (document.getElementById('plazoPeq') != null)
			document.getElementById('plazoPeq').value = '48';
		crossobj.style.left = '175px';
		coeficiente = porcent[5] /*1.02361795*/;
	} else if (plazo <= 66) {
		document.getElementById('elPlazo').value = '60';
		if (document.getElementById('plazoPeq') != null)
			document.getElementById('plazoPeq').value = '60';
		crossobj.style.left = '214px';
		coeficiente = porcent[6] /*1.01937586*/;
	} else if (plazo <= 78) {
		document.getElementById('elPlazo').value = '72';
		if (document.getElementById('plazoPeq') != null)
			document.getElementById('plazoPeq').value = '72';
		crossobj.style.left = '254px';
		coeficiente = porcent[7]/*1.01666993*/;
	} else if (plazo <= 90) {
		document.getElementById('elPlazo').value = '84';
		if (document.getElementById('plazoPeq') != null)
			document.getElementById('plazoPeq').value = '84';
		crossobj.style.left = '294px';
		coeficiente = porcent[8]/*1.01466209*/;
	} else if (plazo <= 102) {
		document.getElementById('elPlazo').value = '96';
		if (document.getElementById('plazoPeq') != null)
			document.getElementById('plazoPeq').value = '96';
		crossobj.style.left = '334px';
		coeficiente = porcent[9]/*1.01320957*/;			
	}
	if  ((plazo == 0) || (plazo == 12) || (plazo == 24) || (plazo == 36) || (plazo == 48) || (plazo == 60) || (plazo == 72) || (plazo == 84) || (plazo == 96))
		document.getElementById('elPlazo').className = '';
	else
		document.getElementById('elPlazo').className = 'redondeo';
	if (document.getElementById('elPlazo').value != 0)
		calcular();
	crossobj = document.getElementById('vacio');
}

function calcular() {
	var parcial = document.getElementById('elImporte').value * coeficiente;
	//var resultadoDiv = parcial / document.getElementById('elPlazo').value; 
	var resultadoDiv = parcial; 
	document.getElementById('cuotaTotal').innerHTML = Math.round(resultadoDiv*100)/100;
}

document.onmousedown = initializedrag;
document.onmouseup = new Function("dragapproved=false;actualizaValores(crossobj)");

if ((document.getElementById('entrada') != null) && (document.getElementById('plazoPeq') != null)) {
	introducirImporte();
	introducirPlazo();
}