function changeImage()
{
	document.frm.mainimage.src = document.frm.list.options[document.frm.list.selectedIndex].value;
}

function prevImage()
{
	if(document.frm.list.selectedIndex == 0)	{
		document.frm.list.selectedIndex = document.frm.list.options.length-1;
	}
	else{
		document.frm.list.selectedIndex--;
	}
	changeImage();
}

function nextImage()
{
	if(document.frm.list.selectedIndex == document.frm.list.options.length-1){
		document.frm.list.selectedIndex = 0;
	}
	else{
		document.frm.list.selectedIndex++;
	}
	changeImage();
}

function emailCheck (emailStr) {
	/* The following variable tells the rest of the function whether or not
	to verify that the address ends in a two-letter country or well-known
	TLD.  1 means check it, 0 means don't. */
	var checkTLD=1;
	
	/* The following is the list of known TLDs that an e-mail address must end with. */
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	
	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. */
	var emailPat=/^(.+)@(.+)$/;
	
	/* The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ] */
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	
	/* The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed.*/
	var validChars="\[^\\s" + specialChars + "\]";
	
	/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")";
	
	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	
	/* The following string represents an atom (basically a series of non-special characters.) */
	var atom=validChars + '+';
	
	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")";
	
	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	
	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	
	/* Finally, let's start trying to figure out if the supplied address is valid. */
	
	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */
	var matchArray=emailStr.match(emailPat);
	
	if (matchArray==null) {
	/* Too many/few @'s or something; basically, this address doesn't
	even fit the general mould of a valid e-mail address. */
	alert("Direccion Email parece incorrecta (verifique la @ y .'s)");
	return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	
	// Start by checking that only basic ASCII characters are in the strings (0-127).
	for (i=0; i<user.length; i++) {
	if (user.charCodeAt(i)>127) {
	alert("El nombre de usuario en el Email contiene caracteres invalidos.");
	return false;
	   }
	}
	for (i=0; i<domain.length; i++) {
	if (domain.charCodeAt(i)>127) {
	alert("El nombre de dominio en el Email contiene caracteres invalidos.");
	return false;
	   }
	}
	
	// See if "user" is valid 
	
	if (user.match(userPat)==null) {
	// user is not valid
	alert("El nombre del usuario no es valido.");
	return false;
	}
	
	/* if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
	
	// this is an IP address
	for (var i=1;i<=4;i++) {
	if (IPArray[i]>255) {
	alert("La IP destino es invalida!");
	return false;
	   }
	}
	return true;
	}
	
	// Domain is symbolic name.  Check if it's valid.
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
	if (domArr[i].search(atomPat)==-1) {
	alert("El nombre de dominio no es valido.");
	return false;
	   }
	}
	
	/* domain name seems valid, but now make sure that it ends in a
	known top-level domain (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */
	if (checkTLD && domArr[domArr.length-1].length!=2 && 
	domArr[domArr.length-1].search(knownDomsPat)==-1) {
	alert("La direccion de Email debe terminar en un dominio conocido o las dos letras del pais");
	return false;
	}
	
	// Make sure there's a host name preceding the domain.
	if (len<2) {
	alert("A la direccion de Email le falta el nombre del host.");
	return false;
	}
	// If we've gotten this far, everything's valid!
	return true;
}

function wordCounter(field, maxlimit) {
var wordcounter=0;
var palabras=0;
	for (x=0;x<field.value.length;x++) {
		if (field.value.charAt(x) == " " && field.value.charAt(x-1) != " ")  {wordcounter++}  // Counts the spaces while ignoring double spaces, usually one in between each word.
		if (wordcounter > 250) {
			field.value = field.value.substring(0, x);
		  }
    	else {palabras = maxlimit - wordcounter;}
	}
	return palabras;
}

function ValidaFormaBienes(){
	/*
	while(''+document.forma.nrolegal.value.charAt(document.forma.nrolegal.value.length-1)==' '){
		document.forma.nrolegal.value=document.forma.nrolegal.value.substring(0,document.forma.nrolegal.value.length-1);
	}
	if (document.forma.nrolegal.value=='') {
		alert ("Por favor, ingrese el número de factura de sus avisos.");
		document.forma.nrolegal.focus();
		return false;
	}
	if (document.forma.nrolegal.value.length!=13) {
		alert ("El número de factura debe tener 13 dígitos");
		document.forma.nrolegal.focus();
		return false;
	}
	if (isNaN(document.forma.nrolegal.value)) {
		alert ("Caracteres no aceptados en el número de factura");
		document.forma.nrolegal.focus();
		return false;
	}*/
	if (document.forma.Tipo.value=='0') {
		alert ("Por favor, seleccione el tipo de aviso");
		document.forma.Tipo.focus();
		return false;
	}
	if (document.forma.Seccion.value=='0') {
		alert ("Por favor, seleccione la seccion del aviso");
		document.forma.Seccion.focus();
		return false;
	}
	if (document.forma.Sector.value=='0') {
		alert ("Por favor, seleccione el sector del bien");
		document.forma.Sector.focus();
		return false;
	}
	while(''+document.forma.DireccionBien.value.charAt(document.forma.DireccionBien.value.length-1)==' '){
		document.forma.DireccionBien.value=document.forma.DireccionBien.value.substring(0,document.forma.DireccionBien.value.length-1);
	}
	if (document.forma.DireccionBien.value=="") {
		alert ("Por favor, ingrese la dirección del bien");
		document.forma.DireccionBien.focus();
		return false;
	}
	while(''+document.forma.Precio.value.charAt(document.forma.Precio.value.length-1)==' '){
		document.forma.Precio.value=document.forma.Precio.value.substring(0,document.forma.Precio.value.length-1);
	}
	if (document.forma.Precio.value=="") {
		alert ("Por favor, ingrese el precio del bien");
		document.forma.Precio.focus();
		return false;
	}
	if (isNaN(document.forma.Precio.value)) {
		alert ("Por favor, ingrese el precio sin el símbolo de dólar y solo números");
		document.forma.Precio.focus();
		return false;
	}
	while(''+document.forma.Descripcion.value.charAt(document.forma.Descripcion.value.length-1)==' '){
		document.forma.Descripcion.value=document.forma.Descripcion.value.substring(0,document.forma.Descripcion.value.length-1);
	}
	if (document.forma.Descripcion.value=="") {
		alert ("Por favor, ingrese la descrición del bien.");
		document.forma.Descripcion.focus();
		return false;
	}
	//Validar 200 palabras
	var palabras;
	palabras = wordCounter(document.forma.Descripcion,200);
	if (palabras <= 0) {
		alert ("Ha excedido el máximo de palabras, por favor ajuste el texto a 200 palabras.");
		document.forma.Descripcion.focus();
		return false;
	}
return true;
}

function ValidaFormaFotos(){
	while(''+document.formaf.foto.value.charAt(document.formaf.foto.value.length-1)==' '){
		document.formaf.foto.value=document.formaf.foto.value.substring(0,document.formaf.foto.value.length-1);		
	}
	if (document.formaf.foto.value=="") {
		alert ("Por favor, elija la foto que desea cargar");
		document.formaf.foto.focus();
		return false;
	}
}

function ValidaFormaVehiculos(){
	/*
	while(''+document.forma.nrolegal.value.charAt(document.forma.nrolegal.value.length-1)==' '){
		document.forma.nrolegal.value=document.forma.nrolegal.value.substring(0,document.forma.nrolegal.value.length-1);
	}
	if (document.forma.nrolegal.value=='') {
		alert ("Por favor, ingrese el número de factura de sus avisos.");
		document.forma.nrolegal.focus();
		return false;
	}
	if (document.forma.nrolegal.value.length!=13) {
		alert ("El número de factura debe tener 13 dígitos");
		document.forma.nrolegal.focus();
		return false;
	}
	if (isNaN(document.forma.nrolegal.value)) {
		alert ("Caracteres no aceptados en el número de factura");
		document.forma.nrolegal.focus();
		return false;
	}
	*/
	if (document.forma.Tipo.value=='0') {
		alert ("Por favor, seleccione el tipo de aviso");
		document.forma.Tipo.focus();
		return false;
	}
	if (document.forma.Seccion.value=='0') {
		alert ("Por favor, seleccione la seccion del aviso");
		document.forma.Seccion.focus();
		return false;
	}
	if (document.forma.Sector.value=='0') {
		alert ("Por favor, seleccione la marca del vehículo");
		document.forma.Sector.focus();
		return false;
	}
	
	if (document.forma.Anio.value=='0') {
		alert ("Por favor, seleccione el año del vehículo");
		document.forma.Anio.focus();
		return false;
	}
	while(''+document.forma.Modelo.value.charAt(document.forma.Modelo.value.length-1)==' '){
		document.forma.Modelo.value=document.forma.Modelo.value.substring(0,document.forma.Modelo.value.length-1);
	}
	if (document.forma.Modelo.value=="") {
		alert ("Por favor, ingrese el modelo del vehículo");
		document.forma.Modelo.focus();
		return false;
	}
	/*
	while(''+document.forma.Kilometraje.value.charAt(document.forma.Kilometraje.value.length-1)==' '){
		document.forma.Kilometraje.value=document.forma.Kilometraje.value.substring(0,document.forma.Kilometraje.value.length-1);
	}
	if (document.forma.Kilometraje.value=="") {
		alert ("Por favor, ingrese el kilometraje del vehículo");
		document.forma.Kilometraje.focus();
		return false;
	}
	while(''+document.forma.Motor.value.charAt(document.forma.Motor.value.length-1)==' '){
		document.forma.Motor.value=document.forma.Motor.value.substring(0,document.forma.Motor.value.length-1);
	}
	if (document.forma.Motor.value=="") {
		alert ("Por favor, ingrese el motor del vehículo");
		document.forma.Motor.focus();
		return false;
	}
	while(''+document.forma.Color.value.charAt(document.forma.Color.value.length-1)==' '){
		document.forma.Color.value=document.forma.Color.value.substring(0,document.forma.Color.value.length-1);
	}
	if (document.forma.Color.value=="") {
		alert ("Por favor, ingrese el color del vehículo");
		document.forma.Color.focus();
		return false;
	}
	while(''+document.forma.Transmision.value.charAt(document.forma.Transmision.value.length-1)==' '){
		document.forma.Transmision.value=document.forma.Transmision.value.substring(0,document.forma.Transmision.value.length-1);
	}
	if (document.forma.Transmision.value=="") {
		alert ("Por favor, ingrese el tipo de transmisión del vehículo");
		document.forma.Transmision.focus();
		return false;
	}
	if (document.forma.DireccionVeh.value=='0') {
		alert ("Por favor, seleccione el tipo de dirección del vehículo");
		document.forma.DireccionVeh.focus();
		return false;
	}
	while(''+document.forma.Cilindraje.value.charAt(document.forma.Cilindraje.value.length-1)==' '){
		document.forma.Cilindraje.value=document.forma.Cilindraje.value.substring(0,document.forma.Cilindraje.value.length-1);
	}
	if (document.forma.Cilindraje.value=="") {
		alert ("Por favor, ingrese el cilindraje del vehículo");
		document.forma.Cilindraje.focus();
		return false;
	}
	if (document.forma.Matricula.value=='0') {
		alert ("Por favor, seleccione hasta cuando tiene matrícula del vehículo");
		document.forma.Matricula.focus();
		return false;
	}
	if (document.forma.Seguro.value=='0') {
		alert ("Por favor, seleccione si tiene o no seguro el vehículo");
		document.forma.Seguro.focus();
		return false;
	}
	*/
	while(''+document.forma.Precio.value.charAt(document.forma.Precio.value.length-1)==' '){
		document.forma.Precio.value=document.forma.Precio.value.substring(0,document.forma.Precio.value.length-1);
	}
	if (document.forma.Precio.value=="") {
		alert ("Por favor, ingrese el precio del vehículo");
		document.forma.Precio.focus();
		return false;
	}
	if (isNaN(document.forma.Precio.value)) {
		alert ("Por favor, ingrese el precio sin el símbolo de dólar y solo números");
		document.forma.Precio.focus();
		return false;
	}
	/*
	if (document.forma.Negociable.value=='0') {
		alert ("Por favor, seleccione si es negociable o no el vehículo");
		document.forma.Negociable.focus();
		return false;
	}
	if (document.forma.Vidrios.value=='0') {
		alert ("Por favor, seleccione si tiene o no vidrios eléctricos");
		document.forma.Vidrios.focus();
		return false;
	}
	if (document.forma.Aire.value=='0') {
		alert ("Por favor, seleccione si tiene o no aire acondicionado");
		document.forma.Aire.focus();
		return false;
	}
	while(''+document.forma.Placa.value.charAt(document.forma.Precio.value.length-1)==' '){
		document.forma.Precio.value=document.forma.Precio.value.substring(0,document.forma.Precio.value.length-1);
	}
	if (document.forma.Precio.value=="") {
		alert ("Por favor, ingrese el precio del vehículo");
		document.forma.Precio.focus();
		return false;
	}
	if (document.forma.Equipo.value=='0') {
		alert ("Por favor, seleccione si tiene o no equipo de sonido");
		document.forma.Equipo.focus();
		return false;
	}
	if (document.forma.Alarma.value=='0') {
		alert ("Por favor, seleccione si tiene o no alarma el vehículo");
		document.forma.Alarma.focus();
		return false;
	}
	*/
	while(''+document.forma.Descripcion.value.charAt(document.forma.Descripcion.value.length-1)==' '){
		document.forma.Descripcion.value=document.forma.Descripcion.value.substring(0,document.forma.Descripcion.value.length-1);
	}
	if (document.forma.Descripcion.value=="") {
		alert ("Por favor, ingrese la descrición del vehículo.");
		document.forma.Descripcion.focus();
		return false;
	}
	//Validar 100 palabras
	var palabras;
	palabras = wordCounter(document.forma.Descripcion,100);
	if (palabras <= 0) {
		alert ("Ha excedido el máximo de palabras, por favor ajuste el texto a 100 palabras.");
		document.forma.Descripcion.focus();
		return false;
	}
}

function Valida_Cedula(){
	var total = 0;
	var total2 = 0;
	var total1 = 0;
	var subtotal1 = 0;
	var li_count;
	var li_num;
	var li_coeficiente;
	var ld_sobra;
	var ld_dig;
	var n;
	var ultimosdigitos;
	var cedula = document.forma.id.value;
	var tipo;
	
	for (n=0;n<2;n++) {
		if (document.forma.tipoid[n].checked) {
			tipo = document.forma.tipoid[n].value;
		}
	}
	
	//Verificar si es cedula con 10 caracteres
	if (tipo == 'CI' && cedula.length != 10) {
		alert("La cédula debe tener 10 caracteres");
		return false;
	}

	//Verificar si es RUC
	if (tipo == 'RUC' && cedula.length != 13) {
		alert("El RUC debe tener 13 caracteres");
		return false;
	}
	if (tipo == 'RUC' && cedula.length == 13) {
		ultimosdigitos = cedula.substring(13, 10)
		cedula = cedula.substring(0, 10)

		for (n=0; n<3; n++) {
			if (isNaN(parseInt(ultimosdigitos.charAt(n)))) {
				alert("RUC incorrecto");
				return false;
			}
		}
	}
	
	//Menor que 6 Cédula de identidad persona natural
	if (cedula.length == 10 && parseInt(cedula.charAt(2)) < 6 && cedula.charAt(0) != 'E') {
		for (li_count = 0;  li_count < 9; li_count++){
			li_num = parseInt(cedula.charAt(li_count));
			ld_sobra = (li_count+1) % 2;
			if (ld_sobra == 0) {
				total2 = total2 + li_num;
			}
			else {
				subtotal1 = li_num * 2;
				if (subtotal1 > 9) {
					subtotal1 = subtotal1 - 9;
				}
				total1 = total1 + subtotal1;
			}
		}
		total = total1 + total2;
		ld_dig = parseInt(total % 10);
		if (ld_dig != 0) {
			ld_dig = 10 - ld_dig;
		}
		if (ld_dig != parseInt(cedula.charAt(9))) {
			alert("Número de cédula o RUC incorrecto");
			return false;
		}
		else {
			//alert("Cédula correcta");
			return true;
		}
	}
	else {
		if (cedula.length == 10 && parseInt(cedula.charAt(2)) == 9 && cedula.charAt(0) != 'E') {
			li_coeficiente = 4;
			for (li_count = 0;  li_count < 9; li_count++) {
				li_num = parseInt(cedula.charAt(li_count));
				if (li_coeficiente == 2) {
					total1 = total1 + (li_num * li_coeficiente);
					li_coeficiente = 7;
				}
				else {
					total1 = total1 + (li_num * li_coeficiente);
					li_coeficiente = li_coeficiente - 1;
				}
			}
			ld_dig = parseInt(total1 % 11);
			if (ld_dig != 0) {
				ld_dig = 11 - ld_dig;
			}
			if (ld_dig != parseInt(cedula.charAt(9))) {
				alert("Número de cédula o RUC incorrecto");
				return false;
			}
			else {
				//alert("Cédula correcta");
				return true;
			}
		}
		else {//Tercer dígito igual a 6 Sociedades Públicas
			if (cedula.length == 10 && parseInt(cedula.charAt(2))==6 && cedula.charAt(0) != 'E') {
				li_coeficiente = 3;
				if (parseInt(cedula.charAt(9)) == 0) {
					for (li_count = 0; li_count < 8; li_count++) {
						li_num = parseInt(cedula.charAt(li_count));
						if (li_coeficiente == 2) {
							total1 = total1 + (li_num * li_coeficiente);
							li_coeficiente = 7;
						}
						else {
							total1 = total1 + (li_num * li_coeficiente);
							li_coeficiente = li_coeficiente - 1;
						}
					}
					ld_dig = parseInt(total1 % 11);
					if (ld_dig != 0) {
						ld_dig = 11 - ld_dig;
					}
					if (ld_dig != parseInt(cedula.charAt(9))) {
						alert("Número de cédula o RUC incorrecto");
						return false;
					}
					else {
						//alert("Cédula correcta");
						return true;
					}
				}
			}
		}
	}
	alert("Número de cédula o RUC incorrecto");
	return false;
}

function ValidaFormaLogin(){
	while(''+document.forma.StrId.value.charAt(document.forma.StrId.value.length-1)==' '){
		document.forma.StrId.value=document.forma.StrId.value.substring(0,document.forma.StrId.value.length-1);
	}
	if (document.forma.StrId.value=="") {
		alert ("Ingrese el número de cédula o RUC con el que esta registrado");
		document.forma.StrId.focus();
		return false;
	}
	while(''+document.forma.StrCodigo.value.charAt(document.forma.StrCodigo.value.length-1)==' '){
		document.forma.StrCodigo.value=document.forma.StrCodigo.value.substring(0,document.forma.StrCodigo.value.length-1);
	}
	if (document.forma.StrCodigo.value=="") {
		alert ("Ingrese su código");
		document.forma.StrCodigo.focus();
		return false;
	}
	return true;
}

function askYN() {
	doyou = confirm("Esta seguro que desea suspender este Clasificado?");
	if (doyou == true)
		return true;
	else
		return false;
};