var reNumber = /\D|\-|\s/g;
var reText   = /\W|\-|\s/g;

function showHotels(selectId, disabledValue)
{
	var choice = new String();		
	choice = (selectId == 'choice1') ? prompts.first : prompts.second;	
	
	var html = new String();
	
	html += '<select name="' + selectId + '" id="' + selectId + '" class="regularselectfield" onchange="handleHotels(\'' + selectId + '\');" >' + "\n";
	
	for (var i = 0; i < hotels.length; i++)
	{
		
		var hotel    = new String(hotels[i]);
		var value    = new String();
		var selected = new String();

		if (selectId == "choice2" && hotel != disabledValue)
	 	{
			value    = (hotel == "xx") ? "xx" : hotel;
			hotel    = (hotel == "xx") ? prompts.selectChoice(choice) : hotel;
			if ($F(selectId) == hotel) selected = 'selected = "selected" '; 
			
			html += '<option value="' + value + '" ' + selected + '>' + hotel + "</options>\n";
	 	}
	 	
	 	if (selectId== "choice1")
	 	{
			value  = (hotel == "xx") ? "xx" : hotel;
			hotel  = (hotel == "xx") ? prompts.selectChoice(choice) : hotel;
			if ($F(selectId) == hotel) selected = 'selected = "selected" '; 
			
			html += '<option value="' + value + '" ' + selected + ' class="hoteloption">' + hotel + "</options>\n";
	 	}	
	}
	
	html += "</select>\n";
	
	return html;
}

function handleRates(id)
{
	if (id == 'choice1')
	{
		selectedIndex = getSelectedRate('choice2');
		showRates('choice1', selectedIndex);
	}
	else
	{
		selectedIndex = getSelectedRate('choice1');
		showRates('choice2', selectedIndex);
	}
}

function handleHotels(id)
{
	var oppositeId = (id == "choice1") ? "choice2" : "choice1";

	switch (id)
	{
		case "choice1":
			$('choice2area').innerHTML  = showHotels('choice2', document.getElementById("choice1").value);
			handleRates('choice1');
			break;
			
		case "choice2":
			$('choice1area').innerHTML  = showHotels('choice1', document.getElementById("choice2").value);
			handleRates('choice2');			
			break;	
	}
}

function handleRooms()
{
	var fnames = new Array();
	var roomNum = $F('qrooms');
	var html  = '';
	
	var roomOccupants = new Array(); var smoking = new Array();  var bed = new Array();  var spreq = new Array(); var rate = new Array(); var arrival = new Array(); var departure = new Array();
	
	for (var i = 2; i <= roomNum; i++) 
	{
		roomOccupants[i] = new Array('','','','');
		roomOccupants[i][0] = $('guest' + i + '0') ? $F('guest' + i + '0') : '';
		roomOccupants[i][1] = $('guest' + i + '1') ? $F('guest' + i + '1') : '';
		roomOccupants[i][2] = $('guest' + i + '2') ? $F('guest' + i + '2') : '';
		roomOccupants[i][3] = $('guest' + i + '3') ? $F('guest' + i + '3') : '';
		
		if ($('hotelarrival' + i)) arrival[i] = $('hotelarrival' + i).selectedIndex;
		if ($('hoteldeparture' + i)) departure[i] = $('hoteldeparture' + i).selectedIndex;
		spreq[i] = ($('specialrequests' + i))  ? $F('specialrequests' + i) : '';
		bed[i] = isBedSet(i);
		rate[i] = isRateSet(i);
		
		if ($('smoking' + i + '_yes') && $('smoking' + i + '_yes').checked) smoking[i] = $F('smoking' + i + '_yes');
		if ($('smoking' + i + '_no') && $('smoking' + i + '_no').checked)  smoking[i] = $F('smoking' + i + '_no');
	}
	
	for (var i = 2; i <= roomNum; i++)
	{
		var persons = (roomOccupants[i]) ? roomOccupants[i] : new Array('','','','');
		
		html += '<div class="databox"><div class="databoxtitle"><span class="databoxtitlespan">' + prompts.room + ' ' + i + '</span></div>';
		html += '<div class="databoxcontent">';
		html += '<div style="float: left; width: 290px; margin-top: 10px;">';
		html += '<label for="hotelarrival' + i + '" style="float: left;">' + prompts.arrival + ':&nbsp;</label>';
		html += '<select class="halfselectfield" name="hotelarrival' + i + '" id="hotelarrival' + i + '" style="float: right; clear: right;">'
		html += '<option value="xx">' + prompts.selectdate + '</option>';
		html += calDate.htmlOptions() + '</select><br />';
		html += '<div style="clear: both; margin: 0; padding: 0;"></div>';
		html += '<label for="hoteldeparture' + i + '" style="float: left;">' + prompts.departure + ':&nbsp;</label>';
		html += '<select class="halfselectfield" name="hoteldeparture' + i + '" id="hoteldeparture' + i + '" style="float: right; clear: right;">';
		html += '<option value="xx">' + prompts.selectdate + '</option>';
		html += calDate.htmlOptions() + '</select><br />';		
		html += '<div style="clear: both; margin: 0; padding: 0;"></div>';
		html += prompts.smoking + ':<br />';
		html += '<input name="smoking' + i + '" type="radio" id="smoking' + i + '_yes" value="yes" />&nbsp;' + prompts.yes + '&nbsp;&nbsp;';
		html += '<input type="radio" name="smoking' + i + '" id="smoking' + i + '_no" value="no" />&nbsp;' + prompts.no + '';
		html += '<div style="clear: both; margin: 0; padding: 0;"></div>';
		html += prompts.bed + ':<br /> <input type="radio" name="bed' + i + '" id="bed' + i + '_king" value="king" />&nbsp;' + prompts.bedking + '&nbsp;&nbsp;';
		html += '<input type="radio" name="bed' + i + '" id="bed' + i + '_double" value="double" />&nbsp;' + prompts.beddouble;
	    html += '<div style="clear: both;"></div></div>';
		
		html += '<div style="width: 420px; float: right; clear: right;">';
		html += '<ul style="margin: 0; padding: 0; width: 100%; clear: both;">';
		html += '<li style="float: left; text-align: left; list-style: none outside; width: 20%; margin: 0; padding: 0;">' + prompts.rate + ':</li>';
		html += '<li style="float: left; text-align: left; list-style: none outside; width: 20%; margin: 0; padding: 0;"><input name="rate' + i + '" type="radio" id="room' + i + '_rate_single" value="single" /><label for="room1_rate_single">' + prompts.ratesingle + '</label></li>';
		html += '<li style="float: left; list-style: none outside; width: 20%; margin: 0; padding: 0;"><input name="rate' + i + '" type="radio"  id="room' + i + '_rate_double" value="double" /><label for="room1_rate_double">' + prompts.ratedouble + '</label></li>';
		html += '<li style="float: left; list-style: none outside; width: 20%; margin: 0; padding: 0;"><input name="rate' + i + '" type="radio"  id="room' + i + '_rate_triple" value="triple" /><label for="room1_rate_triple">' + prompts.ratetriple + '</label></li>';
		html += '<li style="float: left; list-style: none outside; width: 20%; margin: 0; padding: 0;"><input name="rate' + i + '" type="radio"  id="room' + i + '_rate_quad" value="quad" /><label for="room1_rate_quad">' + prompts.ratequad + '</label></li></ul>';
		html += prompts.guestprompt + ':<br />';
		html += '1.&nbsp;<input type="text" name="guest' + i + '[]" id="guest' + i + '0" value="' + persons[0] + '"class="halftextfield" style="width: 90%;" /><br />';
		html += '2.&nbsp;<input type="text" name="guest' + i + '[]" id="guest' + i + '1" value="' + persons[1] + '"class="halftextfield" style="width: 90%;" /><br />';
		html += '3.&nbsp;<input type="text" name="guest' + i + '[]" id="guest' + i + '2" value="' + persons[2] + '"class="halftextfield" style="width: 90%;" /><br />';
		html += '4.&nbsp;<input type="text" name="guest' + i + '[]" id="guest' + i + '3" value="' + persons[3] + '"class="halftextfield" style="width: 90%;" /><br />'
		html += '</div><div style="clear: both;"></div>';
		html += prompts.specialrequests + '<br /><textarea name="specialrequests' + i + '" id="specialrequests' + i + '" class="regulartextfield" style="font-size: 12px; height: 40px;" value="' + spreq[i] + '"></textarea>';
		html += '</div></div>';
	}
	
	$('roomarea').innerHTML = html;
	
	for (var i = 2; i <= roomNum; i++)
	{
		$('hotelarrival' + i).selectedIndex = arrival[i];
		$('hoteldeparture' + i).selectedIndex = departure[i];
		$('specialrequests' + i).value = spreq[i];
		
		if (bed[i] == 'king') $('bed' + i + '_king').checked = 'checked';
		if (bed[i] == 'double') $('bed' + i + '_double').checked = 'checked';

		//alert(smoking[i]);
		if (smoking[i] == 'yes') $('smoking' + i + '_yes').checked = 'checked';
		if (smoking[i] == 'no') $('smoking' + i + '_no').checked = 'checked';
		
		if (rate[i] != false) $('room' + i + '_rate_' + rate[i]).checked = 'checked';
	}
}

function getSelectedRate(id)
{
    var ratesId0 = new String(id); ratesId0 += "rates0";
    var ratesId1 = new String(id); ratesId1 += "rates1" 
    var ratesId2 = new String(id); ratesId2 += "rates2" 
    var ratesId3 = new String(id); ratesId3 += "rates3" 
    var ratesId4 = new String(id); ratesId4 += "rates4" 
	
    if ($(ratesId0) && $(ratesId0).checked) return '0';
    if ($(ratesId1) && $(ratesId1).checked) return '1';
    if ($(ratesId2) && $(ratesId2).checked) return '2';
    if ($(ratesId3) && $(ratesId3).checked) return '3';
    if ($(ratesId4) && $(ratesId4).checked) return '4';

    return false;
}

function isGuestSet(roomNumber)
{
	var	guests = new String($F('guest' + roomNumber + '0') + $F('guest' + roomNumber + '1') + $F('guest' + roomNumber + '2') + $F('guest' + roomNumber + '3'));
	return (guests.length > 0) ? true : false;
}

function isRateSet(roomNumber)
{
	if ($('room' + roomNumber + '_rate_single') && $('room' + roomNumber + '_rate_single').checked) return $F('room' + roomNumber + '_rate_single');
	if ($('room' + roomNumber + '_rate_double') && $('room' + roomNumber + '_rate_double').checked) return $F('room' + roomNumber + '_rate_double');
	if ($('room' + roomNumber + '_rate_triple') && $('room' + roomNumber + '_rate_triple').checked) return $F('room' + roomNumber + '_rate_triple');
	if ($('room' + roomNumber + '_rate_quad')   && $('room' + roomNumber + '_rate_quad').checked)   return $F('room' + roomNumber + '_rate_quad');
	return false;
}

function isBedSet(roomNumber)
{
	if ($('bed' + roomNumber + '_king') && $('bed' + roomNumber + '_king').checked)   return $F('bed' + roomNumber + '_king');
	if ($('bed' + roomNumber + '_double') && $('bed' + roomNumber + '_double').checked) return $F('bed' + roomNumber + '_double');
	return false;
}

function isCardtypeSet()
{
	if ($('pcard_visa').checked) return $F('pcard_visa');
	if ($('pcard_mastercard').checked) return $F('pcard_mastercard');
	if ($('pcard_discover').checked) return $F('pcard_discover');
	if ($('pcard_amex').checked) return $F('pcard_amex');
	return false;
}

/* address */

function changeRstatedesc()
{	
	if ($('mcountry').value == "US") 
	{
		$('mstatedesc').innerHTML    = "State";
		$('mpostaldesc').innerHTML   = "Zip Code";
		displayRegions('mstate', true);
	}
	else 
	{
		$('mstatedesc').innerHTML    = "Province/Region";
		$('mpostaldesc').innerHTML   = "Postal Code";
		displayRegions('mstate', false);
	}
	
	if ($F('mcountry') == "XX") getOtherCountry('mcountryother', true);
	else getOtherCountry('mcountryother', false);
}

function getOtherCountry(objectId, isOtherCountry)
{
	var regionPrefix = new String();
	if (objectId == "mcountryother") regionPrefix = "m";
	else regionPrefix = "b";
	
	if (isOtherCountry != false)
	{
		$(objectId).innerHTML = "Specify Country:&nbsp;<input type='text' name='"+ regionPrefix + "country' id='country_other' class='minitextfield' />";
	}
	else $(objectId).innerHTML = "";
}

function changeRstatedescEs()
{	
	if ($('mcountry').value == "US") 
	{
		$('mstatedesc').innerHTML    = "Estado";
		$('mpostaldesc').innerHTML   = "Zona Postal";
		displayRegions('mstate', true);
	}
	else 
	{
		$('mstatedesc').innerHTML    = "Provincia/Region";
		$('mpostaldesc').innerHTML   = "Zona Postal";
		displayRegions('mstate', false);
	}
	
	if ($F('mcountry') == "XX") getOtherCountryEs('mcountryother', true);
	else getOtherCountry('mcountryother', false);
}

function getOtherCountryEs(objectId, isOtherCountry)
{
	var regionPrefix = new String();
	if (objectId == "mcountryother") regionPrefix = "m";
	else regionPrefix = "b";
	
	if (isOtherCountry != false)
	{
		$(objectId).innerHTML = "Indique pa&iacute;s:&nbsp;<input type='text' name='"+ regionPrefix + "country' id='country_other' class='minitextfield' />";
	}
	else $(objectId).innerHTML = "";
}

function displayRegions(objectId, isCountryUs)
{
	var regionPrefix = new String();
	if (objectId == "mstate") regionPrefix = "m";
	else regionPrefix = "b";
	
	if (isCountryUs != false) $(objectId).innerHTML = "<select name='" + regionPrefix + "region' id='" + regionPrefix + "region' class='regularselectfield'><option value='AL'>Alabama</option><option value='AK'>Alaska</option><option value='AZ'>Arizona</option><option value='AR'>Arkansas</option><option value='CA'>California</option><option value='CO'>Colorado</option><option value='CT'>Connecticut</option><option value='DE'>Delaware</option><option value='FL'>Florida</option><option value='GA'>Georgia</option><option value='HI'>Hawaii</option><option value='ID'>Idaho</option><option value='IL'>Illinois</option><option value='IN'>Indiana</option><option value='IA'>Iowa</option><option value='KS'>Kansas</option><option value='KY'>Kentucky</option><option value='LA'>Louisiana</option><option value='ME'>Maine</option><option value='MD'>Maryland</option><option value='MA'>Massachusetts</option><option value='MI'>Michigan</option><option value='MN'>Minnesota</option><option value='MS'>Mississippi</option><option value='MO'>Missouri</option><option value='MT'>Montana</option><option value='NE'>Nebraska</option><option value='NV'>Nevada</option><option value='NH'>New Hampshire</option><option value='NJ'>New Jersey</option><option value='NM'>New Mexico</option><option value='NY'>New York</option><option value='NC'>North Carolina</option><option value='ND'>North Dakota</option><option value='OH'>Ohio</option><option value='OK'>Oklahoma</option><option value='OR'>Oregon</option><option value='PA'>Pennsylvania</option><option value='PR'>Puerto Rico</option><option value='RI'>Rhode Island</option><option value='SC'>South Carolina</option><option value='SD'>South Dakota</option><option value='TN'>Tennessee</option><option value='TX'>Texas</option><option value='UT'>Utah</option><option value='VT'>Vermont</option><option value='VA'>Virginia</option><option value='WA'>Washington</option><option value='DC'>Washington D.C.</option><option value='WV'>West Virginia</option><option value='WI'>Wisconsin</option><option value='WY'>Wyoming</option></select>";
	else $(objectId).innerHTML = "<input type='text' name='" + regionPrefix + "region' value='' class='regulartextfield' />";
}

function showCompany()
{
	var assoCo = new String();
	assoCo = ($('associationco')) ? $F('associationco') : '';

	if ($F("maddresstype_work") == "work") $("mcompanyarea").innerHTML = prompts.company + ':<br /><input name="mcompany" id="mcompany" value="' + assoCo + '" class="regulartextfield" />';
	else $("mcompanyarea").innerHTML = "";
}
function showAssocCompany()
{
	if ($F("association_exhibitor") == "exhibitor") $("associationcoarea").innerHTML = prompts.company + ':<br /><input name="associationco" id="associationco" value="" class="regulartextfield" />';
	else $("associationcoarea").innerHTML = "";
}

function checkHotelDates(roomNumber)
{
	if ($F('hotelarrival' + roomNumber) == 'xx') return false;
	if ($F('hoteldeparture' + roomNumber) == 'xx') return false;
	
	var d1 = new Date($F('hotelarrival' + roomNumber));
	var d2 = new Date($F('hoteldeparture' + roomNumber));
	
	return (d1.getDate() >= d2.getDate()) ? false : true;
}

function checkCardDates()
{	
	var cdate = new String();

	switch ($F('pmonth'))
	{
		case '0':
		case '2':
		case '4':
		case '6':
		case '7':
		case '9':
		case '11':
			cdate = '31';
			
		case '3':
		case '5':
		case '8':
		case '10':
			cdate = '30';
			
		case '2':
			cdate = '29';
	}

	var d1 = new Date($F('pyear'), $F('pmonth'), cdate);
	var d2 = new Date();
	
	return (d1.getTime() <= d2.getTime()) ? false : true;
}

function checkHotel()
{	
	var success = new Boolean(true);
	var errorTxt = new String();
	
	var ccType = new String(); ccType = ($F('pcard_amex') == "amex") ? "amex" : "notamex";
	
	if ($F('choice1') == "xx") errorTxt += errorMsg.choice1;
//	if ($F('choice2') == "xx") errorTxt += errorMsg.choice2;
//	if ($F('choice1') == $F('choice2')) errorTxt += errorMsg.diffChoice;
	
	for (var i = 1; i <= $F('qrooms'); i++)
	{
		if (false == isRateSet(i)) errorTxt += errorMsg.rates(i);
		if (false == isGuestSet(i)) errorTxt += errorMsg.guests(i);
		if (false == checkHotelDates(i)) errorTxt += errorMsg.dates(i);
		if (false == isBedSet(i)) errorTxt += errorMsg.bedType(i);
	}
	
	var hphone = new String($F('mphoneh'));
	hphone = hphone.replace(reNumber, '');	
	
	if ($F('mfname') == "" || $F('mlname') == "" || $F('maddress') == "" || $F('mcity') == "" || $F('mregion') == "" || $F('mcountry') == "" || $F('memail') == "" || hphone.length < 6) 
	{
		errorTxt += errorMsg.contactInfo;
	}
	
	var bphone = new String($F('mphoneb'));
	bphone = bphone.replace(reNumber, '');
	
	if (bphone.length < 6) errorTxt += errorMsg.altContact;

	if ($F('association_exhibitor') == "exhibitor")
	{
		var assocCompany = new String($F('associationco'));
		var reAC = /\s/g;
		assocCompany = assocCompany.replace(reAC, '');
		
		if (assocCompany.length == 0) errorTxt += errorMsg.exhibitInfo;
	}

	var company = new String();
	company = ($('mcompany')) ? $F('mcompany') : '';
	company = company.replace(reText, '');

	if ($F('maddresstype_work') == 'work' && company.length == 0) errorTxt += errorMsg.company; 

	var account = new String($F('paccount'));
	x = account.toArray();
	
	account = account.replace(reNumber, '');
	
	if ($F('pcardholder').length == 0 || false == isCardtypeSet() || account.length == 0)
	{
		errorTxt += errorMsg.cardNeeded;
	}
	else
	{
		//if (false != isCardtypeSet() && 'amex' == isCardtypeSet() && account.length != 15) errorTxt += errorMsg.cardNumber;
		//if (false != isCardtypeSet() && 'amex' != isCardtypeSet() && account.length != 16) errorTxt += errorMsg.cardNumber;
		//if (account.length <= 14) errorTxt += errorMsg.cardNumber;
		if ($F('pcardholder').length == 0) errorTxt += errorMsg.cardHolder;
		if (false == checkCardDates()) errorTxt += errorMsg.cardExpired;
		
		if (account.length != 16 && ( $('pcard_visa').checked || $('pcard_mastercard').checked || $('pcard_discover').checked))
		{
			errorTxt += "- Please enter all the numbers on your charge card\n";
		}
		else if (account.length != 15 && $('pcard_amex').checked)
		{
			errorTxt += "- Please enter all the numbers on your charge card\n";
		}
		
		if (x[0] != '5' && $('pcard_mastercard').checked == true)	errorTxt += errorMsg.cardNumber;
		else if (x[0] != '4' && $('pcard_visa').checked == true)	errorTxt += errorMsg.cardNumber;
		else if (x[0] != '3' && $('pcard_amex').checked == true)	errorTxt += errorMsg.cardNumber;
		else if (x[0] != '6' && $('pcard_discover').checked == true)	errorTxt += errorMsg.cardNumber;
	}
	
	success = (errorTxt.length > 0) ? false : true;
	if (success == false) alert(errorMsg.pleaseTxt + errorTxt);
	return success;
}
