// Needs utilsNet.js
var INDEPENDENT=11;
var ALAVA_ID = 3;
var GUIPUZCOA_ID = 4;
var VIZCAYA_ID = 5;
var MALE=0;
var FEMALE=1;

var SUGGESTED_QUOTATION=0.298;

var OVER_50_MIN = 916.50*12;
var OVER_50_MAX = 1682.70*12;
var LESS_THAN_50_MIN = 850.20*12;
var LESS_THAN_50_MAX = 3230.10*12;

var SUGGESTED_OVER_50_MIN = parseInt(OVER_50_MIN*SUGGESTED_QUOTATION);
var SUGGESTED_OVER_50_MAX = parseInt(OVER_50_MAX*SUGGESTED_QUOTATION);
var SUGGESTED_LESS_THAN_50_MIN = parseInt(LESS_THAN_50_MIN*SUGGESTED_QUOTATION);
var SUGGESTED_LESS_THAN_50_MAX = parseInt(LESS_THAN_50_MAX*SUGGESTED_QUOTATION);



function changeQuotation(index){
	var obj;
	
	if (index == INDEPENDENT) {
		CustomValidatorQuotation.enabled = true;
		RangeValidatorRetirementAge.enabled = false;
		
	   document.getElementById('spanQuotation').style.display = "";
		obj = document.forms['FormUserData'].TextRetirementAge;			
		obj.value = "65";
		obj.disabled = true;

		FillQuotation();
	} else {
	   FillQuotation();
	   CustomValidatorQuotation.isvalid = true;
	   ValidatorUpdateDisplay(CustomValidatorQuotation);
	    
		CustomValidatorQuotation.enabled = false;
		RangeValidatorRetirementAge.enabled = true;
		
	   document.getElementById('spanQuotation').style.display = "none";
		document.forms['FormUserData'].TextQuotation.value = "";	
		document.forms['FormUserData'].TextRetirementAge.disabled = false;		
	}
}	


function changeResidence(lst){
   var id = parseInt(lst.options[lst.selectedIndex].value);
   if (id == ALAVA_ID || id == GUIPUZCOA_ID || id == VIZCAYA_ID) {
      document.getElementById('spanProductType').style.display = "";
   } else {
      document.getElementById('spanProductType').style.display = "none";
      document.getElementById('selectProductType').selectedIndex = 0;   
   }
}


function ClientValidateQuotation (source, arguments) {
   if (!CustomValidatorQuotation.enabled) {
      arguments.IsValid = true;
      return;
   }
	arguments.IsValid = false;
	
    ValidatorValidate(RequiredFieldValidatorBirthday);
    if (!RequiredFieldValidatorBirthday.isvalid) {
        document.forms['FormUserData'].TIFCalendarBirthday.select();
        arguments.IsValid=true;
        return;
    }
    ValidatorValidate(CompareValidatorBirthday);
    if (!CompareValidatorBirthday.isvalid) {
        document.forms['FormUserData'].TIFCalendarBirthday.select();
        arguments.IsValid=true;
        return;
    }    
    ValidatorValidate(RangeAgeValidator);
    if (!RangeAgeValidator.isvalid) {
        document.forms['FormUserData'].TIFCalendarBirthday.select();
        arguments.IsValid=true;
        return;
    }
    ValidatorValidate(CompareValidatorQuotation);
    if (!CompareValidatorQuotation.isvalid){
        document.forms['FormUserData'].TextQuotation.select();
        arguments.IsValid=true;
        return;
    }

    var currentYear = new Date().getFullYear();
    var birthdayDate = string2fecha(document.forms['FormUserData'].TIFCalendarBirthday.value);
    var birthday = birthdayDate.getFullYear();
    var age = currentYear - birthday;

	var quotation=parseInt(formatoEscritura(document.forms['FormUserData'].TextQuotation.value, tifdsep, tiftsep, 0));

	var index;
	if (document.getElementsByName("SelectSex")[1].checked) {
	    index = MALE;
	} else {
	    index = FEMALE;
	}

	if (age>=0 && age<=49) {
		if(quotation>=SUGGESTED_LESS_THAN_50_MIN && quotation<=SUGGESTED_LESS_THAN_50_MAX){
			arguments.IsValid=true;
		} else {
			// We permit that the number are formatted according to the regional configuration. 
			CustomValidatorQuotation.errormessage='<b>Dato incorrecto:</b><br /> La cotización debe estar entre '+ formatoLectura(String(SUGGESTED_LESS_THAN_50_MIN), tifdsep, tiftsep, 0) +' y ' + formatoLectura(String(SUGGESTED_LESS_THAN_50_MAX), tifdsep, tiftsep, 0);
			document.forms['FormUserData'].TextQuotation.select();
			arguments.IsValid=false;
		}
		return;
	} else if (age>=50) {
	   if (quotation>=SUGGESTED_OVER_50_MIN && quotation<=SUGGESTED_OVER_50_MAX){
			arguments.IsValid=true;
		} else {
			CustomValidatorQuotation.errormessage='<b>Dato incorrecto:</b><br /> La cotización debe estar entre '+ formatoLectura(String(SUGGESTED_OVER_50_MIN), tifdsep, tiftsep, 0) +' y ' + formatoLectura(String(SUGGESTED_OVER_50_MAX), tifdsep, tiftsep, 0);
			document.forms['FormUserData'].TextQuotation.select();
			arguments.IsValid=false;
		}
		return;
	}			
	arguments.IsValid=false;
	return;
}

function CalculateAge(calendar){
	var currentYear = new Date().getFullYear();
	var calendarDate = string2fecha(calendar);
	var age=currentYear - calendarDate.getFullYear();
	return age;
}

function FillQuotation(){
    var age;
    var quotation;
    var salary;
    var obj;

    ValidatorValidate(RequiredFieldValidatorBirthday);
    if (!RequiredFieldValidatorBirthday.isvalid) {
        document.forms['FormUserData'].TIFCalendarBirthday.select();
        arguments.IsValid=true;
        return;
    }
    ValidatorValidate(CompareValidatorBirthday);
    if (!CompareValidatorBirthday.isvalid) {
        document.forms['FormUserData'].TIFCalendarBirthday.select();
        arguments.IsValid=true;
        return;
    }    
    ValidatorValidate(RangeAgeValidator);
    if (!RangeAgeValidator.isvalid) {
        document.forms['FormUserData'].TIFCalendarBirthday.select();
        arguments.IsValid=true;
        return;
    }
    
    salary = parseFloat(formatoEscritura(document.forms['FormUserData'].TextSalary.value, tifdsep, tiftsep, 0));
    quotation = salary * 0.298;
    obj = document.forms['FormUserData'].TextQuotation;
    obj.value = formatoLectura(String(parseInt(quotation)), tifdsep, tiftsep, 0);

    age = CalculateAge(document.forms['FormUserData'].TIFCalendarBirthday.value);    
	var index;
	if (document.getElementsByName("SelectSex")[1].checked) {
	    index = MALE;
	} else {
	    index = FEMALE;
	}

	if (age>=0 && age<=49) {
		if(salary<LESS_THAN_50_MIN){
			obj.value=String(SUGGESTED_LESS_THAN_50_MIN);
		}
		if(salary>LESS_THAN_50_MAX){
			obj.value=String(SUGGESTED_LESS_THAN_50_MAX);
		}
		obj.value = formatoLectura(obj.value, tifdsep, tiftsep, 0);
		ValidatorValidate(CustomValidatorQuotation);
		return;
	} else if (age>=50) {
      if (salary<OVER_50_MIN){
         obj.value=String(SUGGESTED_OVER_50_MIN);
      }
      if (salary>OVER_50_MAX){
         obj.value=String(SUGGESTED_OVER_50_MAX);
      }
      obj.value = formatoLectura(obj.value, tifdsep, tiftsep, 0);
      ValidatorValidate(CustomValidatorQuotation);
      return;
	}				
	arguments.IsValid=false;
	return;
}

function BlurBirthday () {
    if (document.forms['FormUserData'].TIFCalendarBirthday.value != "") {
        if (document.forms['FormUserData'].TextQuotation.value != "") {
            if (CustomValidatorQuotation.enabled) {
		        ValidatorValidate(CustomValidatorQuotation);
		    }
	    } else {
		    BlurSalary();
	    }
    }
}

function BlurSex () {
    if (CustomValidatorQuotation.enabled) {
	    ValidatorValidate(CustomValidatorQuotation);
	}
}

function BlurSalary(){
	if ( (document.forms['FormUserData'].TextSalary.value != "") && 
	     (document.getElementById('spanQuotation').style.display == "") ) {
		FillQuotation();
	}
}

function ShowFirstDescendant(){
	var i;
	var obj;

	for(i=1;i<=10;i++){
		obj=document.getElementById('Desc'+i+'_Fila');
		if (obj.style.display=='none'){
			obj.style.display='';
			obj=document.getElementById('Desc'+i+'_TextBoxAge');
			obj.focus();
			break;
		}
	}
}

function OccultDescendant(index){
	var obj;

	obj=document.getElementById('Desc'+index+'_TextBoxAge');
	obj.value='';
	obj=document.getElementById('Desc'+index+'_DropdownlistIncapacity');
	obj.selectedIndex=0;
	
	obj=document.getElementById('Desc'+index+'_Fila');
	obj.style.display='none';
	
	var numVisibles = 0
	for(i=1;i<=10;i++){
		obj=document.getElementById('Desc'+i+'_Fila');
		if (obj.style.display!='none') {
			numVisibles++;
		}
	}
	if (numVisibles == 0) {
		document.getElementById('cntDesc').style.display='none';
		document.getElementById('DropdownlistDesc').selectedIndex = 0;
	}
}

function ChangeDesc(index){
	if (index==0){
		var i;
		
		for(i=1;i<=10;i++){
			OccultDescendant(i);
		}
		document.getElementById('cntDesc').style.display='none';
	} else {
		document.getElementById('cntDesc').style.display='';
		ShowFirstDescendant();
	}
}

function ShowFirstAscendant(){
	var i;
	var obj;

	for(i=1;i<=10;i++) {
		obj=document.getElementById('Asc'+i+'_Fila');
		if (obj.style.display=='none'){
			obj.style.display='';
			obj=document.getElementById('Asc'+i+'_TextBoxAge');
			obj.focus();
			break;
		}
	}
}

function OccultAscendant(index){
	var obj;
	obj=document.getElementById('Asc'+index+'_TextBoxAge');
	obj.value='';
	obj=document.getElementById('Asc'+index+'_DropdownlistIncapacity');
	obj.selectedIndex=0;
	
	obj=document.getElementById('Asc'+index+'_Fila');
	obj.style.display='none';
	
	var numVisibles = 0
	for(i=1;i<=10;i++){
		obj=document.getElementById('Asc'+i+'_Fila');
		if (obj.style.display!='none') {
			numVisibles++;
		}
	}
	if (numVisibles == 0) {
		document.getElementById('cntAsc').style.display='none';
		document.getElementById('DropdownlistAsc').selectedIndex = 0;
	}
}

function ChangeAsc(index){
	if (index==0) {
		var i;		
		for(i=1;i<=10;i++){
			OccultAscendant(i);
		}
		document.getElementById('cntAsc').style.display='none';
	} else {
		document.getElementById('cntAsc').style.display='';
		ShowFirstAscendant();
	}
}	

function ClientValidateRetirementBirthday(source, arguments) {
    arguments.IsValid=false;

    ValidatorValidate(RequiredFieldValidatorBirthday);
    if (!RequiredFieldValidatorBirthday.isvalid) {
        document.forms['FormUserData'].TIFCalendarBirthday.select();
        arguments.IsValid=true;
        return;
    }
    ValidatorValidate(CompareValidatorBirthday);
    if (!CompareValidatorBirthday.isvalid) {
        document.forms['FormUserData'].TIFCalendarBirthday.select();
        arguments.IsValid=true;
        return;
    }
    ValidatorValidate(RangeValidatorRetirementAge);
    if (!RangeValidatorRetirementAge.isvalid) {
        document.forms['FormUserData'].TextRetirementAge.select();
        arguments.IsValid=true;
        return;
    }

    var currentMoment = new Date();     	
    var birthday = document.forms['FormUserData'].TIFCalendarBirthday.value;
    var birthdayDate = string2fecha(birthday);
    var yearBirthday = birthdayDate.getFullYear();
    var ageRetirement = parseInt(document.forms['FormUserData'].TextRetirementAge.value);
    var yearRetirement = ageRetirement + yearBirthday ;

    if (parseInt(yearRetirement) <= currentMoment.getFullYear()){
        arguments.IsValid = false;
        document.getElementById(source.controltovalidate).select();
    } else {
	     arguments.IsValid = true;
    }
    return;
}

function SendNewProfile() {
   var  Q1 = document.forms['FormUserData'].SelectId1.options[document.forms['FormUserData'].SelectId1.selectedIndex].value;
   var  Q2 = document.forms['FormUserData'].SelectId2.options[document.forms['FormUserData'].SelectId2.selectedIndex].value;
   var  Q3 = document.forms['FormUserData'].SelectId3.options[document.forms['FormUserData'].SelectId3.selectedIndex].value;
   var M1 = 0;
   var M2 = 0;
   var M3 = 0;
   var M4 = 0;

   if (Q1 == "0") {
      SelectProfile(0); //Garantizado
      return;
   } else {
      switch(Q1) {
         case "1": M1++; break;
         case "2": M2++; break;
         case "3": M3++; break;
         case "4": M4++; break;
         default: break;
      }
      switch(Q2) {
         case "1": M1++; break;
         case "2": M2++; break;
         case "3": M3++; break;
         case "4": M4++; break;
         default: break;
      }
      switch(Q3) {
         case "1": M1++; break;
         case "2": M2++; break;
         case "3": M3++; break;
         case "4": M4++; break;
         default: break;
      }
      
      if (M1 == 3 || M1 == 2) {
         SelectProfile(1);
         return;
      } else if (M2 == 3 || M2 == 2) {
         SelectProfile(2);
         return;
      } else if (M3 == 3 || M3 == 2) {
         SelectProfile(3);
         return;
      } else if (M4 == 3 || M4 == 2) {
         SelectProfile(4);
         return;
      }
   }
   SetProfileFromBirthdayAndRetirementDate();
}

function SetProfileFromBirthdayAndRetirementDate() {
   var currentYear = new Date().getFullYear();	 
   var birthday = document.forms['FormUserData'].TIFCalendarBirthday.value;
   var retirementAge = document.forms['FormUserData'].TextRetirementAge.value;	 
   var retirementYear = string2fecha(birthday).getFullYear() + parseInt( retirementAge) ;
   var yearsToRetirement = retirementYear - currentYear;

   if (yearsToRetirement > 25) {
      SelectProfile(4); //Agresivo
   } else if (yearsToRetirement > 15) {
      SelectProfile(3); //Dinamico
   } else if (yearsToRetirement > 5) {
      SelectProfile(2); //Moderado
   } else {
      SelectProfile(1); //Conservador
   }   
}

function SelectProfile(i) {
	eval ("document.forms['FormUserData'].SelectProfile.options[i].selected = true;");
}


