var en_calendar = {dateFormat: 'dd/mm/yy', firstDay: 0};
var es_calendar = {dateFormat: 'dd/mm/yy', firstDay: 1, monthNames: ['Enero','Febrero','Marzo','Abril','Mayo','Junio',
        'Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'],
        monthNamesShort: ['Ene','Feb','Mar','Abr','May','Jun',
        'Jul','Ago','Sep','Oct','Nov','Dic'],
        dayNames: ['Domingo','Lunes','Martes','Miércoles','Jueves','Viernes','Sábado'],
        dayNamesShort: ['Dom','Lun','Mar','Mié','Juv','Vie','Sáb'],
        dayNamesMin: ['Do','Lu','Ma','Mi','Ju','Vi','Sá'],
        prevText: '&#x3c;Ant', prevStatus: '',
        prevJumpText: '&#x3c;&#x3c;', prevJumpStatus: '',
        nextText: 'Sig&#x3e;', nextStatus: '',
        nextJumpText: '&#x3e;&#x3e;', nextJumpStatus: '',
        currentText: 'Hoy', currentStatus: '',
        todayText: 'Hoy', todayStatus: '',
        clearText: 'Limpiar', clearStatus: '',
        closeText: 'Cerrar', closeStatus: '',
        yearStatus: '', monthStatus: '',
        weekText: 'Sm', weekStatus: '',
        dayStatus: 'D, M d', defaultStatus: '',
        isRTL: false};
var fr_calendar = {dateFormat: 'dd/mm/yy', firstDay: 1, monthNames: ['Janvier','Février','Mars','Avril','Mai','Juin',
        'Juillet','Août','Septembre','Octobre','Novembre','Décembre'],
        monthNamesShort: ['Jan','Fév','Mar','Avr','Mai','Jun',
        'Jul','Aoû','Sep','Oct','Nov','Déc'],
        dayNames: ['Dimanche','Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi'],
        dayNamesShort: ['Dim','Lun','Mar','Mer','Jeu','Ven','Sam'],
        dayNamesMin: ['Di','Lu','Ma','Me','Je','Ve','Sa'],
        prevText: '&#x3c;Préc', prevStatus: 'Voir le mois précédent',
        prevJumpText: '&#x3c;&#x3c;', prevJumpStatus: 'Voir l\'année précédent',
        nextText: 'Suiv&#x3e;', nextStatus: 'Voir le mois suivant',
        nextJumpText: '&#x3e;&#x3e;', nextJumpStatus: 'Voir l\'année suivant',
        currentText: 'Courant', currentStatus: 'Voir le mois courant',
        todayText: 'Aujourd\'hui', todayStatus: 'Voir aujourd\'hui',
        clearText: 'Effacer', clearStatus: 'Effacer la date sélectionnée',
        closeText: 'Fermer', closeStatus: 'Fermer sans modifier',
        yearStatus: 'Voir une autre année', monthStatus: 'Voir un autre mois',
        weekText: 'Sm', weekStatus: 'Semaine de l\'année',
        dayStatus: '\'Choisir\' le DD d MM', defaultStatus: 'Choisir la date',
        isRTL: false};

function changedFromDate() {
          var from = document.forms["formsearch"]["from"].value;
          var to = document.forms["formsearch"]["to"].value;   

          if ((to == "") || (to == "dd/mm/yyyy") || (!isValidDate(to, 'DMY'))) {
                    var date_from = convertToDate(from,"DMY");
                    var date_to = new Date(date_from.getFullYear(), date_from.getMonth(), date_from.getDate() + 3, 0, 0, 0, 0);
                    
                    document.forms["formsearch"]["to"].value = twoDigits(date_to.getDate()) + "/" + twoDigits(date_to.getMonth()+1) + "/" + date_to.getFullYear();     
          }
}

function checkSearch()
{
          var errors = "";
          
          var from = document.forms["formsearch"]["from"].value;
          var to = document.forms["formsearch"]["to"].value;
          
          if ((from == "") || (from == "dd/mm/yyyy") || (!isValidDate(from, 'DMY'))) {
                    errors += "Arrival\n";
          }
          if ((to == "") || (to == "dd/mm/yyyy") || (!isValidDate(to, 'DMY'))) {
                    errors += "Departure\n";
          }
          
          if (errors == "") {
                    var date_from = convertToDate(from,"DMY");
                    var date_to = convertToDate(to,"DMY");
                    
                    if (date_from.getTime() >= date_to.getTime()) {
                        errors += "Arrival and departure dates\n";            
                    }
                    
                    var today = new Date();
                    var compareToday = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0, 0);
                    if (date_from.getTime() <= compareToday.getTime()) {
                        errors += "Last arrival\n";            
                    }
          }
          
          if (errors == "") {
                    //document.forms["formsearch"]["from"].value = from.replace(/\//g,"");
                    //document.forms["formsearch"]["to"].value = to.replace(/\//g,"");
                    document.forms["formsearch"].submit();
          } else {
                    alert("Please review the following information:\n\n" + errors);
                    //return false;
          }
	
}

function disableEnterKey(e)
{
     var key;      
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox      

     return (key != 13);
}

function saveTransaction(flat_id,checkin,checkout,amount,flat_price,people,cradle,firstname,lastname,nif,dob,email,country,postcode,address,phone,comments,status,nights,has_special_seasson,discount,price_before) {
	var xmlhttp = InstanceXMLHttpRequest();
	xmlhttp.open('POST', '/ajax?method=savetransaction', true);

	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	xmlhttp.onreadystatechange = function() {		
		if (xmlhttp.readyState == 4) {
			//alert(xmlhttp.responseText);
			document.forms["formbooking"].submit();
			//location.href = "/confirm";
		}
	} 
	xmlhttp.send('flat_id=' + flat_id + '&checkin=' + checkin + '&checkout=' + checkout + '&amount=' + amount + '&flat_price=' + flat_price + '&people=' + people + '&cradle=' + cradle + '&firstname=' + firstname + '&lastname=' + lastname + '&nif=' + nif + '&dob=' + dob + '&email=' + email + '&country=' + country + '&postcode=' + postcode + '&address=' + address + '&phone=' + phone + '&comments=' + comments + '&status=' + status + '&ref=' + '&nights=' + nights + '&has_special_seasson=' + has_special_seasson + '&discount=' + discount + '&price_before=' + price_before);
}

function testBooking()
{
     document.forms["formbooking"].action = "confirm";
     checkBooking();
}

function checkBooking()
{
	var errors = "";
	
	if (!document.forms["formbooking"]["terms"].checked) {
		alert("Please, Acept the Terms and Conditions");
	} else {
		if (document.forms["formbooking"]["first_name"].value == "") {
			errors += "Name\n";
		}
		if (document.forms["formbooking"]["last_name"].value == "") {
			errors += "Lastname\n";
		}
		if (document.forms["formbooking"]["nif"].value == "") {
			errors += "Passport/NIF/DNI\n";
		}
		if (!isValidEmail(document.forms["formbooking"]["email"].value)) {
			errors += "E-mail\n";
		}
		if (document.forms["formbooking"]["email"].value != document.forms["formbooking"]["confirm"].value) {
			errors += "E-mail confirmation\n";
		}
		
		var phone = document.forms["formbooking"]["phone"].value;
		if ((phone == "") || (phone.length < 10)) {
			//errors += "Telephone\n";
		} else {
			document.forms["formbooking"]["night_phone_a"].value = phone.substring(0,3);
			document.forms["formbooking"]["night_phone_b"].value = phone.substring(3,6);
			document.forms["formbooking"]["night_phone_c"].value = phone.substring(6,phone.length);
		}
		
		if (errors == "") {
			//return true;
			document.forms["formbooking"]["item_name"].value = document.forms["backbooking"]["item_name"].value;
			document.forms["formbooking"]["amount"].value = document.forms["backbooking"]["amount"].value;
			document.forms["formbooking"]["zip"].value = document.forms["formbooking"]["postcode"].value;
			
			var flat_id = document.forms["backbooking"]["flat_id"].value;
			var checkin = document.forms["backbooking"]["checkin"].value;
			var checkout = document.forms["backbooking"]["checkout"].value;
			var amount = document.forms["backbooking"]["amount"].value;
			var flat_price = document.forms["backbooking"]["flat_price"].value;
			var people = document.forms["backbooking"]["adults"].value;
			var cradle = document.forms["backbooking"]["cradle"].value;
			if (cradle == "1") cradle = "true";
			else cradle = "false";
			var firstname = document.forms["formbooking"]["first_name"].value;
			var lastname = document.forms["formbooking"]["last_name"].value;
			var nif = document.forms["formbooking"]["nif"].value;
			var dob = document.forms["formbooking"]["year"].value + "-" + document.forms["formbooking"]["month"].value + "-" + document.forms["formbooking"]["day"].value;
			var email = document.forms["formbooking"]["email"].value;
			var country = document.forms["formbooking"]["country"].value;
			var postcode = document.forms["formbooking"]["postcode"].value;
			var address = document.forms["formbooking"]["address1"].value;
			var phone = document.forms["formbooking"]["phone"].value;
			var comments = document.forms["formbooking"]["comments"].value;
			
                        var status = "Pending";
                        
                        var nights = document.forms["backbooking"]["nights"].value;
                        var has_special_seasson = document.forms["backbooking"]["has_special_seasson"].value;
                        var discount = document.forms["backbooking"]["discount"].value;
                        var price_before = document.forms["backbooking"]["price_before"].value;
			
			saveTransaction(flat_id,checkin,checkout,amount,flat_price,people,cradle,firstname,lastname,nif,dob,email,country,postcode,address,phone,comments,status,nights,has_special_seasson,discount,price_before);
			writeLayer("bookingbutton", "<img src='/support/img/loadingAnimation.gif' alt='Reserva en proceso...' widht='208' height='13' align='middle' /><br /><br />");
			
		} else {
			alert("Please review the following information:\n\n" + errors);
			//return false;
		}
	}
	
}

function saveTransactionCaixa(flat_id,checkin,checkout,amount,flat_price,people,cradle,firstname,lastname,nif,dob,email,country,postcode,address,phone,comments,status,ref,nights,has_special_seasson,discount,price_before) {
	var xmlhttp = InstanceXMLHttpRequest();
	xmlhttp.open('POST', '/ajax?method=savetransaction', true);

	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	xmlhttp.onreadystatechange = function() {		
		if (xmlhttp.readyState == 4) {
			//alert(xmlhttp.responseText);
			document.forms["formbooking_caixa"].submit();
			//location.href = "/confirm";
		}
	}
		 
	xmlhttp.send('flat_id=' + flat_id + '&checkin=' + checkin + '&checkout=' + checkout + '&amount=' + amount + '&flat_price=' + flat_price + '&people=' + people + '&cradle=' + cradle + '&firstname=' + firstname + '&lastname=' + lastname + '&nif=' + nif + '&dob=' + dob + '&email=' + email + '&country=' + country + '&postcode=' + postcode + '&address=' + address + '&phone=' + phone + '&comments=' + comments + '&status=' + status + '&ref=' + ref+ '&nights=' + nights + '&has_special_seasson=' + has_special_seasson + '&discount=' + discount + '&price_before=' + price_before);
}

function checkBookingCaixa()
{
	var errors = "";
	
	if (!document.forms["formbooking"]["terms"].checked) {
		alert("Por favor, acepte los terminos y condiciones");
	} else {
		if (document.forms["formbooking"]["first_name"].value == "") {
			errors += "Name\n";
		}
		if (document.forms["formbooking"]["last_name"].value == "") {
			errors += "Lastname\n";
		}
		if (document.forms["formbooking"]["nif"].value == "") {
			errors += "Passport/NIF/DNI\n";
		}
		if (!isValidEmail(document.forms["formbooking"]["email"].value)) {
			errors += "E-mail\n";
		}
		if (document.forms["formbooking"]["email"].value != document.forms["formbooking"]["confirm"].value) {
			errors += "E-mail confirmation\n";
		}
		
		var phone = document.forms["formbooking"]["phone"].value;
		if ((phone == "") || (phone.length < 10)) {
			//errors += "Telephone\n";
		}
		
		if (errors == "") {
			//return true;
			document.forms["formbooking_caixa"]["Ds_Merchant_ProductDescription"].value = document.forms["backbooking"]["item_name"].value;
			document.forms["formbooking_caixa"]["Ds_Merchant_Amount"].value = Math.round(document.forms["backbooking"]["amount"].value * 100);
			
			var message = document.forms["formbooking_caixa"]["Ds_Merchant_Amount"].value;
			message += document.forms["formbooking_caixa"]["Ds_Merchant_Order"].value;
			message += document.forms["formbooking_caixa"]["Ds_Merchant_MerchantCode"].value;
			message += document.forms["formbooking_caixa"]["Ds_Merchant_Currency"].value;
			message += document.forms["formbooking_caixa"]["Ds_Merchant_TransactionType"].value;
			message += document.forms["formbooking_caixa"]["Ds_Merchant_MerchantURL"].value;
			message += "15G19948RF33272T"; //"qwertyasdf0123456789";
			
			document.forms["formbooking_caixa"]["Ds_Merchant_MerchantSignature"].value = hash(message);
			
			var flat_id = document.forms["backbooking"]["flat_id"].value;
			var checkin = document.forms["backbooking"]["checkin"].value;
			var checkout = document.forms["backbooking"]["checkout"].value;
			var amount = document.forms["backbooking"]["amount"].value;
			var flat_price = document.forms["backbooking"]["flat_price"].value;
			var people = document.forms["backbooking"]["adults"].value;
			var cradle = document.forms["backbooking"]["cradle"].value;
			if (cradle == "1") cradle = "true";
			else cradle = "false";
			var firstname = document.forms["formbooking"]["first_name"].value;
			var lastname = document.forms["formbooking"]["last_name"].value;
			var nif = document.forms["formbooking"]["nif"].value;
			var dob = document.forms["formbooking"]["year"].value + "-" + document.forms["formbooking"]["month"].value + "-" + document.forms["formbooking"]["day"].value;
			var email = document.forms["formbooking"]["email"].value;
			var country = document.forms["formbooking"]["country"].value;
			var postcode = document.forms["formbooking"]["postcode"].value;
			var address = document.forms["formbooking"]["address1"].value;
			var phone = document.forms["formbooking"]["phone"].value;
			var comments = document.forms["formbooking"]["comments"].value;
			var status = "Pending";
			var ref = document.forms["formbooking_caixa"]["Ds_Merchant_Order"].value;
                        
                        var nights = document.forms["backbooking"]["nights"].value;
                        var has_special_seasson = document.forms["backbooking"]["has_special_seasson"].value;
                        var discount = document.forms["backbooking"]["discount"].value;
                        var price_before = document.forms["backbooking"]["price_before"].value;
			
			saveTransactionCaixa(flat_id,checkin,checkout,amount,flat_price,people,cradle,firstname,lastname,nif,dob,email,country,postcode,address,phone,comments,status,ref,nights,has_special_seasson,discount,price_before);
			writeLayer("bookingbutton", "<img src='/support/img/loadingAnimation.gif' alt='Reserva en proceso...' widht='208' height='13' align='middle' /><br /><br />");
			
		} else {
			alert("Please review the following information:\n\n" + errors);
			//return false;
		}
	}
	
}

function fillYearMonthSeparateDays(formName, yearSelect, monthSelect, daysSelect) {
	var month = document.forms[formName][monthSelect].options[document.forms[formName][monthSelect].selectedIndex].value;
	var year = document.forms[formName][yearSelect].options[document.forms[formName][yearSelect].selectedIndex].value;
	
	var numDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	if (checkLeapYear(year)) {
		numDays[1] = 29;
	}
	
	for (var i=0; i < 31; i++) {
		document.forms[formName][daysSelect].options[i] = null;
	}
	for (var i=1; i <= numDays[month - 1]; i++) {
		document.forms[formName][daysSelect].options[i - 1] = new Option(i, twoDigits(i));
	}
}

function fillYearMonthDays(formName, dateSelect, daysSelect) {
	var dateString = document.forms[formName][dateSelect].options[document.forms[formName][dateSelect].selectedIndex].value;
	var month = dateString.substring(5,7);
	var year = dateString.substring(0,4);
	
	var numDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	if (checkLeapYear(year)) {
		numDays[1] = 29;
	}
	
	for (var i=0; i < 31; i++) {
		document.forms[formName][daysSelect].options[i] = null;
	}
	for (var i=1; i <= numDays[month - 1]; i++) {
		document.forms[formName][daysSelect].options[i - 1] = new Option(i, twoDigits(i));
	}
}

function twoDigits(number) {
	var string = "" + number;
	if (string.length == 1) {
		string = "0" + string;
	}
	return string;
}

function checkLeapYear(year) {
	year = parseInt(year);
	if (year % 4 == 0) {
		if (year % 100 != 0) {
			return true;
		} else {
			if(year % 400 == 0) {
				return true;
			} else {
				return false;
			}
		}
	}
	return false;
}
function bookNow() {
	var checkin = document.forms.yourbooking.month_checkin.value + "-" + document.forms.yourbooking.day_checkin.value;
	var checkout = document.forms.yourbooking.month_checkout.value + "-" + document.forms.yourbooking.day_checkout.value;
	
	document.forms.yourbooking.checkin.value = checkin;
	document.forms.yourbooking.checkout.value = checkout;

	document.forms.yourbooking.linkback.value = location.href;
	
	document.forms.yourbooking.submit();
}

function calculateBooking(name, id, step) {
	var xmlhttp = InstanceXMLHttpRequest();
	xmlhttp.open('POST', '/calculation', true);

	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	xmlhttp.onreadystatechange = function() {		
		if (xmlhttp.readyState == 4) {
			//alert(xmlhttp.responseText);
			writeLayer(name, xmlhttp.responseText);
		}
	}
	
	var checkin = document.forms.yourbooking.month_checkin.value + "-" + document.forms.yourbooking.day_checkin.value;
	var checkout = document.forms.yourbooking.month_checkout.value + "-" + document.forms.yourbooking.day_checkout.value;
	var adults = document.forms.yourbooking.adults.value;
	var cradle = false;
	if (document.forms.yourbooking.cradle) {
		cradle = document.forms.yourbooking.cradle.checked;
	}
	 
	xmlhttp.send('checkin=' + checkin + '&checkout=' + checkout + '&adults=' + adults + '&cradle=' + cradle + '&id=' + id + '&step=' + step);
	
	writeLayer(name, "<img src='/support/img/loadingAnimation.gif' alt='Calculando precio' widht='208' height='13' align='middle' /><br /><br />");
}

function getCalendar(name, day, month, year, id) {
	var xmlhttp = InstanceXMLHttpRequest();
	xmlhttp.open('POST', '/ajax?method=getcalendar', true);

	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	xmlhttp.onreadystatechange = function() {		
		if (xmlhttp.readyState == 4) {
			//alert(xmlhttp.responseText);
			writeLayer(name, xmlhttp.responseText);
		}
	}
	xmlhttp.send('name=' + name + '&day=' + day + '&month=' + month + '&year=' + year + '&id=' + id);		
}

function calendarRefresh(day, month, year, id) {
	getCalendar("calendar", day, month, year, id);
}

function checkContact()
{
	var errors = "";
	
	if (!document.forms["formcontact"]["terms"].checked) {
		alert("Por favor, acepte los terminos y condiciones");
	} else {
		if (document.forms["formcontact"]["firstname"].value == "") {
			errors += "Name\n";
		}
		if (!isValidEmail(document.forms["formcontact"]["email"].value)) {
			errors += "Last Name\n";
		}
		if (document.forms["formcontact"]["comments"].value == "") {
			errors += "Comments\n";
		}
		
		if (errors == "") {
			//return true;
			document.forms["formcontact"].submit();
		} else {
			alert("Please review the following information:\n\n" + errors);
			//return false;
		}
	}
	
}

function displayDiv(loc, visible){
	var foc = document.getElementById(loc);
	if (visible) {
		foc.style.display='block';
	} else {
		foc.style.display='none';
	}
}

function writeLayer(name, txt){
	var layer = document.getElementById(name);
	layer.innerHTML = txt;
}

function debug(obj, trace)
{
	var m='';
	for (i in obj) {
		m+= i +'\t';
		if (trace) alert(obj[i]);
	}
	alert(m);
}

function InstanceXMLHttpRequest() {
	if(window.XMLHttpRequest) {
		try {
			req = new XMLHttpRequest();
		} catch(e) {
			req = false;
		}
		// branch for IE/Windows ActiveX version
	} else if(window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				req = false;
			}
		}
	}
	return req;
}

function isValidEmail(email) {
	var re = /^ *([a-z0-9_-]+\.)*[a-z0-9_-]+@(([a-z0-9-]+\.)+(com|net|org|mil|edu|gov|arpa|info|biz|inc|name|[a-z]{2})|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}) *$/;
	return (re.test(email.toLowerCase()));
}

function isValidUsernamePassword(value) {
	re = /^\w+$/;
	return (re.test(value.toLowerCase()));
}

String.prototype.trim = function (){
	return this.replace(/(^\s+)/g, "").replace(/(\s+$)/g, "");
}

function isValidDate(dateStr, format) {
   if (format == null) { format = "MDY"; }
   format = format.toUpperCase();
   if (format.length != 3) { format = "MDY"; }
   if ( (format.indexOf("M") == -1) || (format.indexOf("D") == -1) || (format.indexOf("Y") == -1) ) { format = "MDY"; }
   if (format.substring(0, 1) == "Y") { // If the year is first
      var reg1 = /^\d{4}(\/)\d{1,2}\1\d{1,2}$/

   } else if (format.substring(1, 2) == "Y") { // If the year is second
      var reg1 = /^\d{1,2}(\/)\d{4}\1\d{1,2}$/

   } else { // The year must be third
      var reg1 = /^\d{1,2}(\/)\d{1,2}\1\d{4}$/

   }
   if ( reg1.test(dateStr) == false ) { return false; }
   var parts = dateStr.split(RegExp.$1); // Split into 3 parts based on what the divider was
   // Check to see if the 3 parts end up making a valid date
   if (format.substring(0, 1) == "M") { var mm = parts[0]; } else 
      if (format.substring(1, 2) == "M") { var mm = parts[1]; } else { var mm = parts[2]; }
   if (format.substring(0, 1) == "D") { var dd = parts[0]; } else
      if (format.substring(1, 2) == "D") { var dd = parts[1]; } else { var dd = parts[2]; }
   if (format.substring(0, 1) == "Y") { var yy = parts[0]; } else
      if (format.substring(1, 2) == "Y") { var yy = parts[1]; } else { var yy = parts[2]; }
   if (parseFloat(yy) <= 50) { yy = (parseFloat(yy) + 2000).toString(); }
   if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
   var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
   if (parseFloat(dd) != dt.getDate()) { return false; }
   if (parseFloat(mm)-1 != dt.getMonth()) { return false; }
   return true;
}

function convertToDate(dateStr, format) {
   if (format == null) { format = "MDY"; }
   format = format.toUpperCase();
   if (format.length != 3) { format = "MDY"; }
   if ( (format.indexOf("M") == -1) || (format.indexOf("D") == -1) || (format.indexOf("Y") == -1) ) { format = "MDY"; }
   if (format.substring(0, 1) == "Y") { // If the year is first
      var reg1 = /^\d{4}(\/)\d{1,2}\1\d{1,2}$/

   } else if (format.substring(1, 2) == "Y") { // If the year is second
      var reg1 = /^\d{1,2}(\/)\d{4}\1\d{1,2}$/

   } else { // The year must be third
      var reg1 = /^\d{1,2}(\/)\d{1,2}\1\d{4}$/

   }
   if ( reg1.test(dateStr) == false ) { return false; }
   var parts = dateStr.split(RegExp.$1); // Split into 3 parts based on what the divider was
   // Check to see if the 3 parts end up making a valid date
   if (format.substring(0, 1) == "M") { var mm = parts[0]; } else 
      if (format.substring(1, 2) == "M") { var mm = parts[1]; } else { var mm = parts[2]; }
   if (format.substring(0, 1) == "D") { var dd = parts[0]; } else
      if (format.substring(1, 2) == "D") { var dd = parts[1]; } else { var dd = parts[2]; }
   if (format.substring(0, 1) == "Y") { var yy = parts[0]; } else
      if (format.substring(1, 2) == "Y") { var yy = parts[1]; } else { var yy = parts[2]; }
   if (parseFloat(yy) <= 50) { yy = (parseFloat(yy) + 2000).toString(); }
   if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
   var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
   if (parseFloat(dd) != dt.getDate()) { return false; }
   if (parseFloat(mm)-1 != dt.getMonth()) { return false; }
   return dt;
}

function popup(w,h,t,l,url,winName,status,toolBar,scrollBars,resizable) {
	if (scrollBars == undefined) scrollBars = "auto";
	if (resizable == undefined) resizable = "no";
  	var sWid = screen.width;
  	var sHi = screen.height;
	var wid = w;
	var hi = h;
	if ((t == 0) && (l == 0)) {
		var tp = (sHi/2)-(hi/2);
		var lft = (sWid/2)-(wid/2);
	} else {
		var tp = t;
		var lft = l;	
	}
	if (scroll && document.all && (navigator.userAgent.indexOf("Mac") > -1)) wid = wid+17;
	newwin=window.open(url,winName,"width=" + wid + ",height=" + hi + ",status=" + status + ",scrollbars=" + scrollBars + ",toolbar=" + toolBar + ",resizable=" + resizable + ", top = "+ tp +", left ="+ lft + ", screenX=" + lft +", screenY= "+tp);
	newwin.focus();
}

function changeClassName(name, className){
	var obj = document.getElementById(name);
	obj.className = className;
}

function displayLayer(name, visible){
	var foc = document.getElementById(name);
	if (foc) {
		if (visible) {
			foc.style.display='block';
		} else {
			foc.style.display='none';
		}
	}
}

function setStyle(name, property, value){
	var foc = document.getElementById(name);
	if (foc) {
		foc.style[property] = value;
	}
}
