function getNextDate(date,days){
if(date.substring(0,1)=="0"){
day=parseInt(date.substring(1,2));}else{
day=parseInt(date.substring(0,2));}
if(date.substring(3,4)=="0"){
month=parseInt(date.substring(4,5))-1;}else{
month=parseInt(date.substring(3,5))-1;}
year=parseInt(date.substring(6,10));
actualDate=new Date(year,month,day);
millis=actualDate.getTime()+12*60*60*1000+days*24*60*60*1000;
actualDate.setTime(millis);
nday=new Number(actualDate.getDate());
nmonth=new Number(actualDate.getMonth())+1;
nyear=new Number(actualDate.getYear());
if(nyear<1000){
nyear+=1900;}
if(nday<10){
aux="0"+nday.toString()+"/";}else{
aux=nday.toString()+"/";}
if(nmonth<10){
aux=aux+"0"+nmonth.toString()+"/";}else{
aux=aux+nmonth.toString()+"/";}
aux=aux+nyear.toString();
return aux;}
function getDays(date1,date2){
if(!date2){
actualDate1=new Date();
actualDate1=new Date(actualDate1.getYear(),actualDate1.getMonth(),actualDate1.getDate());
millis1=actualDate1.getTime();
date2=date1;}else{
if(date1.substring(0,1)=="0"){
day1=parseInt(date1.substring(1,2));}else{
day1=parseInt(date1.substring(0,2));}
if(date1.substring(3,4)=="0"){
month1=parseInt(date1.substring(4,5))-1;}else{
month1=parseInt(date1.substring(3,5))-1;}
year1=parseInt(date1.substring(6,10));
actualDate1=new Date(year1,month1,day1);
millis1=actualDate1.getTime();}
if(date2.substring(0,1)=="0"){
day2=parseInt(date2.substring(1,2));}else{
day2=parseInt(date2.substring(0,2));}
if(date2.substring(3,4)=="0"){
month2=parseInt(date2.substring(4,5))-1;}else{
month2=parseInt(date2.substring(3,5))-1;}
year2=parseInt(date2.substring(6,10));
actualDate2=new Date(year2,month2,day2);
millis2=actualDate2.getTime();
dif=millis2-millis1;
days=dif/(24*60*60*1000);
var resultado=""+days;
if(resultado.indexOf(".")!=-1){
var aux=resultado.substring(resultado.indexOf(".")+1,resultado.indexOf(".")+2);
if(parseInt(aux)>5){
days=Math.ceil(days);}else{
days=Math.floor(days);}}
return days;}
function parseDate(str){
var compactedDate=str;
if(str.length==10){
compactedDate=str;}else{
var index=str.indexOf("/");
if(index==-1){
if(str.length==8){
if(str.substring(2,3)=="/"){
compactedDate=str.substring(0,6)+"20"+str.substring(6,8);}else{
compactedDate=str.substring(0,2)+"/"+str.substring(2,4)+"/"+str.substring(4,8);}}else if(str.length==6){
compactedDate=str.substring(0,2)+"/"+str.substring(2,4)+"/20"+str.substring(4,6);}}else{
index=compactedDate.indexOf("/");
var newFecha=compactedDate.substring(0,index)+"/";
if(index==1){
newFecha="0"+newFecha;}
compactedDate=compactedDate.substring(index+1);
index=compactedDate.indexOf("/");
var saux=compactedDate.substring(0,index);
compactedDate=compactedDate.substring(index+1);
if(index==1){
newFecha=newFecha+"0"+saux;}else{
newFecha=newFecha+saux;}
if(compactedDate.length==2){
compactedDate=newFecha+"/20"+compactedDate;}else{
compactedDate=newFecha+"/"+compactedDate;}}}
return compactedDate;}
function parseHour(str,separador){
var compactedHour=str;
if(separador==null){separador=":";}
if(str.length<5){
var index=str.indexOf(separador);
if(index==-1){
if(str.length==4){
compactedHour=str.substring(0,2)+separador+str.substring(2,4);}
if(str.length==2){
compactedHour=str+separador+"00";}
if(str.length==1){
compactedHour="0"+str+separador+"00";}}else{
if(str.length==4){
if(index==1){
compactedHour="0"+str;}else if(index==2){
compactedHour=str.substring(0,2)+separador+"0"+str.substring(3,4);}}else if(str.length==3){
if(index==1){
compactedHour="0"+str.substring(0,1)+separador+"0"+str.substring(2,3);}}}}
return compactedHour;}
function validaDate(str){
if(str.length>0){
if(str.length==10){
compactedDate=str.substring(0,2)+str.substring(3,5)+str.substring(6,10);}else if(str.length==8){
if(str.substring(2,3)=="/"){
compactedDate=str.substring(0,2)+str.substring(3,5)+"20"+str.substring(6,8);}else{
compactedDate=str;}}else if(str.length==6){
compactedDate=str.substring(0,4)+"20"+str.substring(4,6);}else{
return false;}
if(!validate(compactedDate,DIGITS)){
return false;}
if(compactedDate.substring(0,1)=="0"){
day=parseInt(compactedDate.substring(1,2));}else{
day=parseInt(compactedDate.substring(0,2));}
if(compactedDate.substring(2,3)=="0"){
month=parseInt(compactedDate.substring(3,4));}else{
month=parseInt(compactedDate.substring(2,4));}
year=parseInt(compactedDate.substring(4,8));
REGULAR_MONTHS=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
LEAP_MONTHS=new Array(31,29,31,30,31,30,31,31,30,31,30,31);
curDate=new Date();
if((month<1)||(month>12)){
return false;}
if(isLeapYear(year)){
if(day<1||day>LEAP_MONTHS[month-1]){
return false;}}
else
{
if(day<1||day>REGULAR_MONTHS[month-1]){
return false;}}}
return true;}
function validaHour(str){
if(str.length>0){
if(!validate(str,DIGITS+":")){
return false;}
if((str.length!=4)&&(str.length!=5)){
return false;}
Phora=parseInt(str.substring(0,2));
if(str.indexOf(":")==2){
Pminutos=parseInt(str.substring(3,5));}else{
Pminutos=parseInt(str.substring(2,4));}
if(!(Phora<0||Phora>23||Pminutos<0||Pminutos>59)){
return true;}else{
return false;}}
return true;}
function calculaDias(objIni,objDias,objFin,upd){
if(upd=="inicio"){
if(objIni.value!=''){
if(validaDate(objIni.value)){
objIni.value=parseDate(objIni.value);
if(objDias.value!=''){
objFin.value=getNextDate(objIni.value,objDias.value);}else{
if(objFin.value!=''){
if(validaDate(objFin.value)){
objDias.value=getDays(objIni.value,objFin.value);}}}}}else{
objDias.value="";}}
if(upd=="dias"){
if(objDias.value!=""){
if(objIni.value!=''){
if(validaDate(objIni.value)){
objFin.value=getNextDate(objIni.value,objDias.value);}}else{
if(objFin.value!=''){
if(validaDate(objFin.value)){
objIni.value=getNextDate(objFin.value,-objDias.value);}}}}else{
objFin.value="";}}
if(upd=="fin"){
if(objFin.value!=""){
if(validaDate(objFin.value)){
objFin.value=parseDate(objFin.value);
if(objIni.value!=""){
if(validaDate(objIni.value)){
objDias.value=getDays(objIni.value,objFin.value);}}else{
if(document.forms[0].dias.value!=""){
objIni.value=getNextDate(objFin.value,-objDias.value);}}}}else{
objDias.value="";}}}

