var BASE = "";
var atlas = false;

if(document.images) atlas = true;

function windowob(name,src, width, height,options) {
        this.name = name;
        this.src = BASE + src;
        this.width = width;
        this.height = height;
        if(options) this.options = options;
}

var deathwin = new windowob('deathwin2','time.htm',290,260)

function launchwindow(obj,sz) {
var width = obj.width;
var height = obj.height;
if (sz==0) height = 150;
var src = obj.src;

        if(obj.options) 
        obj = window.open("",obj.name,"width="+width+",height="+height+","+obj.options);
        else
        obj = window.open("",obj.name,"resize=no,toolbar=no,menubar=no,width="+width+",height="+height);
        if(obj != null) {
                if(obj.opener == null) obj.opener = self; 
                obj.location.href = src;
                if(atlas) obj.focus();
        }
}

function Valid(val,errormsg) {
        for(var y = 0; y<val.length; y++) {
                var letter = val.substring(y,y+1);
                if(letter < "0" || letter > "9") {
                        alert("Carácter ilegal en "+errormsg+" field.\nSolo puede colocar números en este campo.");
                        return false;
                }       
        }
        return true;
}

function okForm(myday,mymonth,myyear,fagyears,fagmonths,fagdays,fags) {
        //Check for valid numbers

        if(!Valid(fagdays,"Total Smoking Time: Days")) {
                document.Main.Days.value = 1;
                return false;
        }

        if(!Valid(fagmonths,"Total Smoking Time: Months")) {
                document.Main.Months.value = 1;
                return false;
        }

        if(!Valid(fagyears,"Total Smoking Time: Years")) {
                document.Main.Years.value = 1;
                return false;
        }

        if(!Valid(fags,"Cigarettes/Day")) {
                document.Main.Cigarettes.value = 1;
                return false;
        }

        if(!Valid(myday,"Day")) {
                document.Main.Day.value = 1;
                return false;
        }

        if(!Valid(myyear,"Year")) {
                document.Main.Year.value = 73;
                return false;
        }
        if (myyear>1900) myyear=myyear-1900;


        if(!myday || myday<1) {
                alert("Debe especificar en que día nació usted.");
                document.Main.Day.value = 1;
                return false;
        }
        if(myday > 31) {
                alert("El máximo números de días en el mes son 31.\n"+myday+" no es un número válido.");
                document.Main.Day.value = 1;
                return false;
        }
        if((mymonth==3) || (mymonth==5) || (mymonth==8) || 
        (mymonth==10)) {
                if(myday>30) {
                alert("El mes que seleccionó solo tiene 30 días.");
                document.Main.Day.value = 30;
                return false;
                }
        }
        if((mymonth==1) && (myday > 29)) {
                alert("El día máximo para Febrero es 29.\n"+myday+" no es válido.");
                document.Main.Day.value = 29;
                return false;
        }
        if(!myyear || myyear == 0) {
                alert("Debe colocar el año en que nació.");
                document.Main.Year.value = 1973;
                return false;
        }

        return true;
}

function startDeath(form) {
        if(okForm(form.Day.value,form.Month.selectedIndex,form.Year.value,form.Years.value,form.Months.value,form.Days.value,form.Cigarettes.value)) {
                trueMonth = form.Month.options[form.Month.selectedIndex].text;
                trueSex = form.Sex.options[form.Sex.selectedIndex].text;
                trueMode = form.Mode.options[form.Mode.selectedIndex].text;
                deathwin.src = BASE + "time.htm?Day=" + form.Day.value + "&Month=" + trueMonth + "&Year=" + form.Year.value + "&Sex=" + trueSex + "&Mode=" + trueMode + "&Smoker=" + form.Smoker.selectedIndex+"&Years="+form.Years.value+"&Months="+form.Months.value+"&Days="+form.Days.value+"&Cigs="+form.Cigarettes.value;
                launchwindow(deathwin,form.Smoker.selectedIndex);
        }
}

