function revisarDigito( dvr )
{	
	dv = dvr + ""	
	if ( dv != '0' && dv != '1' && dv != '2' && dv != '3' && dv != '4' && dv != '5' && dv != '6' && dv != '7' && dv != '8' && dv != '9' && dv != 'k'  && dv != 'K')	
	{		
		alert("Debe ingresar un digito verificador valido");		
		window.document.form1.rut.focus();		
		window.document.form1.rut.select();		
		return false;	
	}	
	return true;
}

function revisarDigito2( crut )
{	
	largo = crut.length;	
	if ( largo < 2 )	
	{		
		alert("Debe ingresar el rut completo")		
		window.document.form1.rut.focus();		
		window.document.form1.rut.select();		
		return false;	
	}	
	if ( largo > 2 )		
		rut = crut.substring(0, largo - 1);	
	else		
		rut = crut.charAt(0);	
	dv = crut.charAt(largo-1);	
	revisarDigito( dv );	

	if ( rut == null || dv == null )
		return 0	

	var dvr = '0'	
	suma = 0	
	mul  = 2	

	for (i= rut.length -1 ; i >= 0; i--)	
	{	
		suma = suma + rut.charAt(i) * mul		
		if (mul == 7)			
			mul = 2		
		else    			
			mul++	
	}	
	res = suma % 11	
	if (res==1)		
		dvr = 'k'	
	else if (res==0)		
		dvr = '0'	
	else	
	{		
		dvi = 11-res		
		dvr = dvi + ""	
	}
	if ( dvr != dv.toLowerCase() )	
	{		
		alert("EL rut es incorrecto")		
		window.document.form1.rut.focus();		
		window.document.form1.rut.select();		
		return false	
	}

	return true
}

function Rut(texto)
{	
	var tmpstr = "";	
	for ( i=0; i < texto.length ; i++ )		
		if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' )
			tmpstr = tmpstr + texto.charAt(i);	
	texto = tmpstr;	
	largo = texto.length;	

	if ( largo < 2 )	
	{		
		alert("Debe ingresar el rut completo")		
		window.document.form1.rut.focus();		
		window.document.form1.rut.select();		
		return false;	
	}	

	for (i=0; i < largo ; i++ )	
	{			
		if ( texto.charAt(i) !="0" && texto.charAt(i) != "1" && texto.charAt(i) !="2" && texto.charAt(i) != "3" && texto.charAt(i) != "4" && texto.charAt(i) !="5" && texto.charAt(i) != "6" && texto.charAt(i) != "7" && texto.charAt(i) !="8" && texto.charAt(i) != "9" && texto.charAt(i) !="k" && texto.charAt(i) != "K" )
 		{			
			alert("El valor ingresado no corresponde a un R.U.T valido");			
			window.document.form1.rut.focus();			
			window.document.form1.rut.select();			
			return false;		
		}	
	}	

	var invertido = "";	
	for ( i=(largo-1),j=0; i>=0; i--,j++ )		
		invertido = invertido + texto.charAt(i);	
	var dtexto = "";	
	dtexto = dtexto + invertido.charAt(0);	
	dtexto = dtexto + '-';	
	cnt = 0;	

	for ( i=1,j=2; i<largo; i++,j++ )	
	{		
		//alert("i=[" + i + "] j=[" + j +"]" );		
		if ( cnt == 3 )		
		{			
			dtexto = dtexto + '.';			
			j++;			
			dtexto = dtexto + invertido.charAt(i);			
			cnt = 1;		
		}		
		else		
		{				
			dtexto = dtexto + invertido.charAt(i);			
			cnt++;		
		}	
	}	

	invertido = "";	
	for ( i=(dtexto.length-1),j=0; i>=0; i--,j++ )		
		invertido = invertido + dtexto.charAt(i);	

	window.document.form1.rut.value = invertido.toUpperCase()		

	if ( revisarDigito2(texto) )		
		return true;	

	return false;
}


function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function ComprobarClave(formulario){
  if(formulario.clave.value.length < 5){
    alert('La clave debe tener mínimo 5 caracteres.');
    formulario.clave.focus();
    formulario.clave.select();
    return false;
  }
  if(formulario.clave.value != formulario.clave_confirm.value){
    alert('Las claves deben ser iguales.');
    formulario.clave_confirm.focus();
    formulario.clave_confirm.select();
    return false;
  }
  return true;
}

var nav4 = window.Event ? true : false;
function acceptNum(evt){
// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57
var key = nav4 ? evt.which : evt.keyCode;
return (key <= 13 || (key >= 48 && key <= 57));
}

function showLargeImage(strDisplayPath, ad_id, admin) {
	var ad_id = ad_id ? ad_id : "";
	var admin = admin ? admin : false;
	var image = document.getElementById("display_image" + ad_id).firstChild;

	if (admin) {
		resizeImage(image, strDisplayPath, null, admin);
	} else {
		if (navigator.userAgent.toLowerCase().indexOf('safari') > 0) {
			var next_image = new Image;
			next_image.src = strDisplayPath;

			image.src = next_image.src;
			waitForNextImage(next_image, ad_id);
		} else {
			image.src = strDisplayPath;
		}
	}
}

/* Video */
function flowplayer_conf(video_flv, add_splash, auto_buffer, image_path) {
	var conf = new Object();
	var image_path = image_path ? image_path : false;

	conf.showLoopButton = false;
	conf.showMenu = false;
	conf.autoPlay = false;
	conf.loop = false;
	conf.initialScale = 'scale';
	conf.showFullScreenButton = false;
	conf.fullScreenScriptURL = '';
	conf.bufferLength = 10;
	conf.videoFile = video_flv;
	conf.playList = '';

	if (auto_buffer) {
		conf.autoBuffering = true;
	} else if (add_splash && video_flv.indexOf('videos') > 0) {
		var video_splash = '';
		if (image_path == false) {
			video_splash = video_flv.replace(/.*videos/, add_splash).replace(/flv/, 'jpg');
		} else {
			var image_id = video_flv.substring(video_flv.lastIndexOf("/"));
			image_id = image_id.replace(/flv/, "jpg");
			video_splash = image_path + image_id.substring(0, 3) + image_id;
		}

		conf.autoBuffering = false;
		conf.splashImageFile = video_splash;
		conf.scaleSplash = true;
	} else if (add_splash && video_flv.indexOf('flush') > 0) {
		var video_splash = video_flv.replace(/flv/, 'splash');

		conf.splashImageFile = video_splash+'/.jpg';
		conf.scaleSplash = true;
	}


	return conf;
}

function flowplayer_conf_to_string(conf) {
        var conf_string = '{';
        for (var i in conf) {
                conf_string += i + ':' + (typeof(conf[i]) == 'string'?"'":'') + conf[i] + (typeof(conf[i]) == 'string'?"'":'');
                conf_string += ',';
        }
        conf_string = conf_string.substring(0, conf_string.length - 1);
        conf_string += '}';
        return conf_string;
}

function hide_video(ad_id) {
	ad_id = ad_id || '';

	var video = document.getElementById('flowplayerholder' + ad_id);
	var image = document.getElementById('display_image' + ad_id);

	if (video) {
		video.style.display = 'none';
		var container = video.parentNode;
		container.removeChild(video);
	}

	if (image)
		image.style.display = 'block';
}

function show_video(video_file, width, height, ad_id) {
	ad_id = ad_id || '';
	var image = document.getElementById('display_image' + ad_id);
	var container = image.parentNode;
	var video = document.getElementById('flowplayerholder' + ad_id);

	var conf = flowplayer_conf(video_file, false);
	conf.autoPlay = true;
	conf = flowplayer_conf_to_string(conf);

	if (ad_id && width > 400)  {
		height = Math.round(400 / width * height);
		width = 400;
	}

	if (!video) {
		video = document.createElement('div');

		video.className = 'ad_pict';

		video.id = 'flowplayerholder' + ad_id;
		container.appendChild(video);

		image.style.display = 'none';

		var fo = { 
			movie:"swf/FlowPlayer.swf", 
			width:width, 
			height:height, 
			majorversion:"7", 
			build:"0", 
			allowscriptaccess: "always",
			flashvars:"config=" + conf
			};
	
		UFO.create(fo, "flowplayerholder" + ad_id);

	} else if (video.style.display == 'none')  {
		image.style.display = 'none';
		video.style.display = 'block';
	} else {
		var fo = document.getElementById("flowplayerholder" + ad_id + "_obj");
		var time = fo.getTime();
		var dur = fo.getDuration();
		if (time >= dur)
			fo.Seek(0);


		if(!fo.getIsPaused() && fo.getIsPlaying())  {
			fo.Pause();
		} else { 
			fo.DoPlay(); 
		}
	}	
}

function next_image () {
	if(!images[counter])
		counter = 0;

	/* If next item is an video, play video */
	if(video_exist == 1 && counter == 0) {
		var v_thumb = document.getElementById('thumb' + images.length);
		v_thumb.name = 'video';
		counter = 0;

		thumbnailBorder(v_thumb, images.length + 1);
		show_video(video_url, video_width, video_height); 

		return;
	}
		
	/* Preload next image */
	var thumb = document.getElementById('thumb' + counter);
	var image = new Image;
	image.src = image_url + images[counter];

	thumbnailBorder(thumb, images.length);
	showLargeImage(image_url + images[counter]);			

	set_alt_title('main_pict');

	counter++;
}

function set_alt_title(call_div) {
	var main_image = document.getElementById('main_image');
	var adder = 0;

	if(call_div == 'thumb') {
		adder = 1;	
	}	

	/* When next thumb is a video, display other alt/title */
	if(video_exist == 1 && counter == images.length - 1 + adder)
	{
		main_image.alt = '';
		main_image.title = '';
	} else {
		main_image.alt = '';
		main_image.title = '';
	}
}

var styles = [];
function thumbnailBorder(thumb, image_num, ad_id) {
	var ad_id = ad_id ? ad_id : "";

	if (thumb.name != 'video')
		hide_video(ad_id);

	for (i = 0; i < image_num; i++) {
		var thumb_obj = document.getElementById('thumb' + i + ad_id);
		if (!styles[i]) {
			styles[i] = thumb_obj.className;
			if (styles[i].indexOf('ad_border_solid_black') > 0) {
				styles[i] = styles[i].substring(0, styles[i].indexOf('ad_border_solid_black')) + 'ad_border_solid_grey';
			}
		}
		thumb_obj.className = styles[i];

		if (thumb.id == thumb_obj.id) {
			thumb_obj.className = "ad_thumb ad_border_solid_black";
			if (thumb.name != 'video') {
				document.getElementById('display_image' + ad_id).className = 'ad_pict' + ((styles[i].indexOf('ad_border_dotted') > 0)?' ad_border_dotted':'');
			}
		}
	}
}


function getElementsByClassName(oElm, strTagName, strClassName) {
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];      
		if(oRegExp.test(oElement.className)){
			arrReturnElements[arrReturnElements.length] = oElement;
		}   
	}
	return (arrReturnElements)
}

function show_hidden_elements() {
	var elements = getElementsByClassName(document, "*", 'hide');
	for (var i = 0; i < elements.length; i++) {
		elements[i].className = elements[i].className.replace(/hide/, '');
	}
}
