// JavaScript Document
function checkForm() {
    usr = document.postcard.deste.value;
    arrr = usr.indexOf("@");
    ptt = usr.lastIndexOf(".");
    if(arrr<1) {
        alert("El email de destino no es válido");
        return;
    }
    if(ptt-arrr < 3) {
        alert("El email de destino no es válido");
        return;
    }
    
    usr = document.postcard.reme.value;
    arrr = usr.indexOf("@");
    ptt = usr.lastIndexOf(".");
    if(arrr<1) {
        alert("Su email no es válido");
        return;
    }
    if(ptt-arrr < 3) {
        alert("Su email no es válido");
        return;
    }
    if(document.postcard.msg.value.length > 160) {
        alert("El texto debe ser de máximo 160 caracteres");
        return;
    }
	if(document.postcard.msg.value.length < 10) {
        alert("El mensaje debe contener como minimo 10 caracteres");
        return;
    }
    document.postcard.submit();
}

function previewCard() {
    var sel=-1;
    for(i=0;i<document.postcard.img.length;i++){
         if(document.postcard.img[i].checked) {
             sel =i;
         }
    }
    if(sel > -1) {
        var imgsrc = document.postcard.img[sel].value;
        var win1  = window.open("components/com_postal/views/postal/tmpl/preview.php","tipolove_preview","menubar=no,width=812,height=590,toolbar=no,status=no,location=no");
    }
    else {
        alert("Seleccione una imágen");
    }
}
var curindex=0
var delay=6000 //set delay in miliseconds	
var randomimages=new Array()
var total = 5; 
var init = 0;
for(i=1; i<total; i++){
	randomimages[i]="http://www.tipolove.com/site/images/tipolove/home/"+i+'.jpg';
}
var preload=new Array()
function randomImage(){
	for (n=0;n<randomimages.length;n++)
	{
		preload[n]=new Image()
		preload[n].src=randomimages[n]
	}
	init = Math.floor(Math.random()*(randomimages.length));
	document.write('<img name="defaultimage" src="'+randomimages[init]+'">')
	setInterval("rotateimage()",delay)	
}
function rotateimage(){
/*if (curindex==(tempindex=Math.floor(Math.random()*(randomimages.length)))){
	curindex=curindex==0? 1 : curindex-1
}
else
	curindex=tempindex*/
if(init != 0)
	init = init + 1;
if(init == total)
	init = 1;
if(init=='')
	init = 1;
document.images.defaultimage.src=randomimages[init];

}
function CountLeft(field) {
	// if the length of the string in the input field is greater than the max value, trim it
	field = document.getElementById(field);
	left = document.getElementById('leftCounter');
	if (field.value.length > 100)
		field.value = field.value.substring(0, 100);
	else
	// calculate the remaining characters
		left.innerHTML = 'Caracteres Permitidos: ' + (100 - field.value.length);
}

function MM_validateForm() { //v4.0
	bday();
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' debe contener una direccion de correo.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' debe contener un numero.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' es requerido.\n'; }
    } if (errors) alert('Los siguientes errores han ocurrido:\n'+errors);
   else
   		document.josForm.submit();
} }

function bday(){
			var bdayV = document.josForm.bday.options[document.josForm.bday.selectedIndex].value;
			var bmonthV = document.josForm.bmonth.options[document.josForm.bmonth.selectedIndex].value;
			document.getElementById('birthday').value = bdayV +' / '+ bmonthV;
	}