/**
 * Functión que crea el objeto XMLHttpRequest
 */
function initXMLHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}
var objAjax = initXMLHTTPObject();
var strPage = "date.php";
var strAjaxCallFunction = "ShowDate";


/**
 * Peticion simple
 */
function sendSimpleRequest(arrFields,arrValues,HttpMethod){
	// Determinar destino de los datos
	if (!(typeof strPage != 'undefined') || strPage == "") {
		return false;
	}
	
	//
	if (!(typeof arrFields != 'undefined')) { arrFields = new Array(); }
	if (!(typeof arrValues != 'undefined')) { arrValues = new Array(); }
	
	// Determinar método de envío
	if (!HttpMethod) {
		HttpMethod = "POST";
	}
	if (HttpMethod == "get") {
		HttpMethod = "GET";
	} else {
		HttpMethod = "POST";
	}	
	//
	var strParams = "";
	var strSeparator = "&";
	var strUrl = "";
	
	var intElementsQuantity = arrFields.length;
	//var intRndNumber = Math.floor(Math.random()*11111111);
	intRndNumber = 1;
	strParams = "rndNumber=" + intRndNumber;
	for(i=0;i<intElementsQuantity;i++) {
		strParams = strParams + "&" + arrFields[i] + "=" + arrValues[i];
	}
	if (objAjax) {
		if (HttpMethod == "POST") {
			strUrl = strPage;
			strSendParams = strParams;
			objAjax.open(HttpMethod,strUrl,true);
			objAjax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		} else {
			strUrl = strPage + strSeparator + strParams;
			strSendParams = null;
			objAjax.open(HttpMethod,strUrl,true);
		}
		// Reset parameters
		strPage = undefined;
		HttpMethod = undefined;
		//
		objAjax.onreadystatechange = getHttpResponse;
		objAjax.send(strSendParams);
	}
	return false;
}


/**
 * Controla las respuesta del servidor
 * 
 */
function getHttpResponse() {
	
	if (objAjax.readyState == 4) {
		
		if (objAjax.status == 200) {
			
			if (strAjaxCallFunction != "") {
				
				strString = strAjaxCallFunction + "();";				
				eval(strString);
				
			}
			
		} else {
			alert("Página " + strPage + " no encontrada...");
		}
	}
	
} 

function ShowDate() {
	
	var dateBOGOTA = this.document.getElementById("fechaBOGOTA");
	var datePARIS = this.document.getElementById("fechaPARIS");
	
	var Valores = new Array();
	
	textResult = objAjax.responseText;
	var arrItems = textResult.split("|");

	for(i=0;i<arrItems.length;i++) {
		
		var arrData = arrItems[i].split(/###/gi);
		
		if (i == 0) {
			horaBogota = arrData[4];
			minutosBogota = arrData[5];
			meridiamBogota = arrData[6];
			
			diaSemanaBogota = arrData[1];
			diaMesBogota = arrData[2];
			mesBogota = arrData[3];

			Valores[0] = "<table width=\"224\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"styleTexto\"><tr height=\"25\" bgcolor=\"#FFFFFF\"><td width=\"54\" align=\"left\"><span class=\"sText\">" + arrData[0] + ", </span></td><td width=\"170\"><span class=\"sText\"><span id='diaSemanaBogota'>" + arrData[1] + "</span> <span id='diaMesBogota'>"  + arrData[2] +  "</span> <span id='mesBogota'>" + arrData[3] + "</span>,</span><span class=\"sText2\"> <span id='horaBogota'>" + arrData[4] + "</span>:<span id='minutosBogota'>" + arrData[5] + "</span> <span id='meridiamBogota'>" + arrData[6] + "</span></span></td></tr></table><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td height=\"5\"></td></tr></table>";

		}
	
		if (i == 1) {
			horaParis = arrData[4];
			minutosParis = arrData[5];
			meridiamParis = arrData[6];
			
			diaSemanaParis = arrData[1];
			diaMesParis = arrData[2];
			mesParis = arrData[3];
			
			Valores[1] = "<table width=\"224\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"styleTexto\"><tr height=\"25\" bgcolor=\"#FFFFFF\"><td width=\"54\" align=\"left\"><span class=\"sText\">" + arrData[0] + ", </span></td><td width=\"170\"><span class=\"sText\"><span id='diaSemanaParis'>" + arrData[1] + "</span> <span id='diaMesParis'>"  + arrData[2] +  "</span> <span id='mesParis'>" + arrData[3] + "</span>,</span><span class=\"sText2\"> <span id='horaParis'>" + arrData[4] + "</span>:<span id='minutosParis'>" + arrData[5] + "</span> <span id='meridiamParis'>" + arrData[6] + "</span></span></td></tr></table>";
		}	
	
	}

	dateBOGOTA.innerHTML = Valores[0];
	datePARIS.innerHTML = Valores[1];

	setInterval("changeTime()", 60000);
	
}

// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}


function changeTime() {
	
	// Tomar valores
	var day_of_week = this.document.getElementById("diaSemanaBogota").innerHTML;
	var day_of_month = this.document.getElementById("diaMesBogota").innerHTML;
	var month = this.document.getElementById("mesBogota").innerHTML;
	var hour = this.document.getElementById("horaBogota").innerHTML;
	if (hour == "00"  && meridiam == "AM" ) { hour = 12; }
	var minutes = this.document.getElementById("minutosBogota").innerHTML;
	var meridiam = this.document.getElementById("meridiamBogota").innerHTML;


	// PROCESAR
	minutes = parseInt(minutes);
	minutes++;

	if (minutes == 60) {
		minutes = 0;
		
		hour = parseInt(hour);
		hour++;

		if (hour == 12 && meridiam == "PM") {
			meridiam = "AM";

			day_of_month = parseInt(day_of_month) + 1;
			
			// Dias del mes
			
			if (month == "Enero" || month == "Marzo" || month == "Mayo" || month == "Julio" || month == "Agosto" || month == "Octubre" || month == "Diciembre") {
				var totaldias = 31;
			} else {
				var totaldias = 30;
			}
			
			if (day_of_month > totaldias) {
				day_of_month = 1;
				if (month == "Octubre") { month = "Noviembre"; 
				} else if (month == "Noviembre") { month = "Diciembre";
				} else if (month == "Diciembre") { month = "Enero"; 
				} else if (month == "Enero") { month = "Febrero"; 
				} else if (month == "Febrero") { month = "Marzo"; 
				} else if (month == "Marzo") { month = "Abril"; 
				} else if (month == "Abril") { month = "Mayo"; 
				} else if (month == "Mayo") { month = "Junio"; 
				} else if (month == "Junio") { month = "Julio"; 
				} else if (month == "Julio") { month = "Agosto"; 
				} else if (month == "Agosto") { month = "Septiembre"; 
				} else if (month == "Septiembre") { month = "Octubre"; }
			}
			
			// Dias de la semana
			switch(day_of_week) {
				case 'Lunes': day_of_week = 'Martes';break;
				case 'Martes': day_of_week = 'Miércoles';break;
				case 'Miércoles': day_of_week = 'Jueves';break;
				case 'Jueves': day_of_week = 'Viernes';break;
				case 'Viernes': day_of_week = 'Sábado';break;
				case 'Sábado': day_of_week = 'Domingo';break;
				case 'Domingo': day_of_week = 'Lunes';break;
			}
			
		} else if (hour == 12 && meridiam == "AM") {
			meridiam = "PM";
		}
		
		if (hour == 13) {
			hour = 1;
		}
	}
	// FIN DE PROCESAMIENTO
	
	if (hour == "12" && meridiam == "AM") { hour = "00";}
	
	//
	
	/*minutes = ""+minutes;
	if (minutes.length == 1) { minutes = "0"+minutes; }
	*/
	/*hour = ""+hour;
	if (hour.length == 1) { hour = "0"+hour; }	
	*/
	
	// Asignacion
	this.document.getElementById("horaBogota").innerHTML = hour;
	this.document.getElementById("minutosBogota").innerHTML = minutes;
	this.document.getElementById("meridiamBogota").innerHTML = meridiam;
	this.document.getElementById("diaMesBogota").innerHTML = day_of_month;
	this.document.getElementById("diaSemanaBogota").innerHTML = day_of_week;
	this.document.getElementById("mesBogota").innerHTML = month;
	
	changeTime2();
	
}


function changeTime2() {

	var day_of_week = this.document.getElementById("diaSemanaParis").innerHTML;
	var day_of_month = this.document.getElementById("diaMesParis").innerHTML;
	var month = this.document.getElementById("mesParis").innerHTML;
	var hour = this.document.getElementById("horaParis").innerHTML;
	if (hour == "00"  && meridiam == "AM" ) { hour = 12;	}
	var minutes = this.document.getElementById("minutosParis").innerHTML;
	var meridiam = this.document.getElementById("meridiamParis").innerHTML;
	
	// PROCESAR
	minutes = parseInt(minutes);
	minutes++;
	if (minutes == 60) {
		minutes = 0;
		
		hour = parseInt(hour);
		hour++;
		
		if (hour == 12 && meridiam == "PM") {
			meridiam = "AM";
			
			day_of_month = parseInt(day_of_month) + 1;
			
			// Dias del mes
			
			if (month == "Enero" || month == "Marzo" || month == "Mayo" || month == "Julio" || month == "Agosto" || month == "Octubre" || month == "Diciembre") {
				var totaldias = 31;
			} else {
				var totaldias = 30;
			}
			
			if (day_of_month > totaldias) {
				day_of_month = 1;
				if (month == "Octubre") { month = "Noviembre"; 
				} else if (month == "Noviembre") { month = "Diciembre";
				} else if (month == "Diciembre") { month = "Enero"; 
				} else if (month == "Enero") { month = "Febrero"; 
				} else if (month == "Febrero") { month = "Marzo"; 
				} else if (month == "Marzo") { month = "Abril"; 
				} else if (month == "Abril") { month = "Mayo"; 
				} else if (month == "Mayo") { month = "Junio"; 
				} else if (month == "Junio") { month = "Julio"; 
				} else if (month == "Julio") { month = "Agosto"; 
				} else if (month == "Agosto") { month = "Septiembre"; 
				} else if (month == "Septiembre") { month = "Octubre"; }
			}
			
			// Dias de la semana
			switch(day_of_week) {
				case 'Lunes': day_of_week = 'Martes';break;
				case 'Martes': day_of_week = 'Miércoles';break;
				case 'Miércoles': day_of_week = 'Jueves';break;
				case 'Jueves': day_of_week = 'Viernes';break;
				case 'Viernes': day_of_week = 'Sábado';break;
				case 'Sábado': day_of_week = 'Domingo';break;
				case 'Domingo': day_of_week = 'Lunes';break;
			}
			
		} else if (hour == 12 && meridiam == "AM") {
			meridiam = "PM";
		}
		
		if (hour == 13) {
			hour = 1;
		}
	}
	// FIN DE PROCESAMIENTO	
	
	if (hour == "12" && meridiam == "AM") { hour = "00";}
	
	//
	this.document.getElementById("horaParis").innerHTML = hour;
	this.document.getElementById("minutosParis").innerHTML = minutes;
	this.document.getElementById("meridiamParis").innerHTML = meridiam;
	this.document.getElementById("diaMesParis").innerHTML = day_of_month;
	this.document.getElementById("diaSemanaParis").innerHTML = day_of_week;
	this.document.getElementById("mesParis").innerHTML = month;
	
}

window.onload = sendSimpleRequest;