//--------------------------------------------------------------------
// Module Name: dynform.js
// Created by:  G. Ajamian
// Copywrite of G. Ajamian and the Arabic Baptist Church.
//--------------------------------------------------------------------
//
// This script is called upon initialization of the form and 
// whenever the "Add Another Peroson" button is clicked.
//
//--------------------------------------------------------------------



//---------------------------------------------------------------
// Cost is set in Fees.js called by RegDyn.asp
//---------------------------------------------------------------

// global variable used to validate and write to dB
var elementI = 1;
// TODO:: eliminate elementI global var
/*
function SetValue()
{
	document.RegFormS3.totRegistered.value = elementI;
}
*/

//---------------------------------------------------------------
// addPerson
//---------------------------------------------------------------
function addPerson()
{
	setRecalcMsg();
	var tbl = document.getElementById('tblReg');
	var lastRow = tbl.rows.length;
	// if there's no header row in the table, then iteration = lastRow + 1
	var iteration = lastRow;
	var row = tbl.insertRow(lastRow);

	// cell 1: first name
	var cell1 = row.insertCell(0);
	var fname	= document.createElement('input');
	fname.type = 'text';
	fname.name = 'RegFName' + iteration;
	fname.id = 'RegFName' + iteration;
	fname.size = 10;
	fname.maxlength = 32;
	cell1.appendChild(fname);


	// cell 2: last name
	var cell2 = row.insertCell(1);
	var lname	= document.createElement('input');
	lname.type = 'text';
	lname.name = 'RegLName' + iteration;
	lname.id = 'RegLName' + iteration;
	lname.size = 14;
	lname.maxlength = 32;
	cell2.appendChild(lname);


	// cell 3: gender
	var cell3 = row.insertCell(2);
	var gender = document.createElement('select');
	gender.name = 'RegGender' + iteration;
	gender.id = 'RegGender' + iteration;
	gender.options[0] = new Option('', '0');
	gender.options[1] = new Option('Male', 'M');
	gender.options[2] = new Option('Female', 'F');
	cell3.appendChild(gender);


	// cell 4: day of arrival
	var cell4 = row.insertCell(3);
	var arvDay	= document.createElement('select');
	arvDay.name = 'RegDay' + iteration;
	arvDay.id = 'RegDay' + iteration;
	arvDay.onfocus = setRecalcMsg;
	arvDay.onblur = function() {SetAlert(iteration);}
	
	// MayConf declared as global var in Fees.inc
	if (true == MayConf)
	{
		if ("Suite" == document.RegFormS3.AccomType.value)
		{
			arvDay.options[0] = new Option(aDormCost[0].strPlanName, 'Friday');
		}
		
		else if ("Hotel" == document.RegFormS3.AccomType.value)
		{
			arvDay.options[0] = new Option('', '0');
			arvDay.options[1] = new Option(aMealCost[0].strPlanName, 'Reg_Meal1');
			arvDay.options[2] = new Option('Weekend Reg', 'Conf_Reg');
		}
		
		else 
		{
			arvDay.options[0] = new Option('', '0');
			arvDay.options[1] = new Option(aDormCost[0].strPlanName, 'Friday');
			arvDay.options[2] = new Option(aDormCost[1].strPlanName, 'Saturday');
			arvDay.options[3] = new Option(aMealCost[0].strPlanName, 'Reg_Meal1');
			arvDay.options[4] = new Option(aMealCost[1].strPlanName, 'Reg_Meal2');
			arvDay.options[5] = new Option('Weekend Reg', 'Conf_Reg');
			arvDay.options[6] = new Option('1 Day Reg', 'Day_Reg');
		}
	}
	else
	{
		arvDay.options[0] = new Option('', '0');
		arvDay.options[1] = new Option(aDormCost[0].strPlanName, 'Friday');
	}
	cell4.appendChild(arvDay);


	// cell 5: age
	var cell5 = row.insertCell(4);
	var age	= document.createElement('select');
	age.name = 'RegAge' + iteration;
	age.id = 'RegAge' + iteration;
	age.onfocus = setRecalcMsg;
	age.onblur = function() {SetAlert(iteration);}
//	age.onblur = getIndivCost;
	if (true == MayConf)
	{
		age.options[0] = new Option('', '0');
		age.options[1] = new Option('Over 10', 'Over10');
		age.options[2] = new Option('4-10', '4To10');
		age.options[3] = new Option('Under 4', 'Under4');
	}
	else
	{
		age.options[0] = new Option('', '0');
		age.options[1] = new Option('Adult', 'Over10');
	}
	cell5.appendChild(age);


	// cell 6: cost
	var cell6 = row.insertCell(5);
	var cost = document.createElement('input');
	cost.name = 'RegCost' + iteration;
	cost.id = 'RegCost' + iteration;
	cost.size = 6;
	cost.maxlength = 8;
	cost.readonly = 'true';
	cell6.appendChild(cost);

//	elementI++;
	elementI=iteration;
}


//---------------------------------------------------------------
// copy - copy values from one field to another
// 
// This works because each name is unique and is not an array
// of this type as with the static. The DOM objects created 
// need to be accessed differently.
//
// this also works
// document.getElementById("RegLName1").value = document.RegFormS1.LastName.value;
//---------------------------------------------------------------
function copy()
{
	document.getElementById("RegFName1").setAttribute("value",  document.RegFormS3.FirstName.value);
	document.getElementById("RegLName1").setAttribute("value", document.RegFormS3.LastName.value);
//	document.getElementById("RegAge1").setAttribute("value", "Over10");
}


//---------------------------------------------------------------
// getIndivCost
// Called in the following places:
//    - OnClick event for the submit button in RegDyn.asp
//    - OnBlur event in the creation of a new line
// future use
// This will work only for the first case. Need to rework so 
// that they are all done in DOM
//
// Note that func validRegInfo() is p/o validate.js
//
// Key:
//	0x100 = Registration Only			0x010 = Friday							0x001 = Under 4 years
//	0x200 = Day Registration Only		0x020 = Saturday						0x002 = 4-10 years
//										0x040 = Fri Registration & Meals		0x004 = Over 10 years
//										0x080 = Sat Registration & Meals		
//			
//	
//---------------------------------------------------------------
function getIndivCost()
{
	var costMask = 0x000;
	var indCost = 0;
	var totCost = 0;
	var aCost;
	
	if(true == MayConf) {
		if ("Dorm" == document.RegFormS3.AccomType.value) {
			aCost = aDormCost;
		}
		else if("Suite" == document.RegFormS3.AccomType.value) {
			aCost = aSuiteCost;
		}
		else if("Hotel" == document.RegFormS3.AccomType.value) {
			aCost = aHotelCost;
		}

		else {
			alertUser(document.getElementById("Accom"), "Please Select Accommodation Type");
			return false;
		}	
	}
	else {
		aCost = aDormCost;
	}

	// Detrmine Arrival Day or Type of plan required
	for(j = 1; j < document.getElementById('tblReg').rows.length; j++) 
	{
		if ("0" == document.getElementById("RegDay"+j).value)
		{
			if (ValidateRegStep3()) continue; else return false;
		}

		else if ("Friday" == document.getElementById("RegDay"+j).value)
		{
			costMask = 0x010;
		}

		else if ("Saturday" == document.getElementById("RegDay"+j).value)
		{
			costMask = 0x020;
		}

		else if ("Reg_Meal1" == document.getElementById("RegDay"+j).value)
		{
			costMask = 0x040;
		}

		else if ("Reg_Meal2" == document.getElementById("RegDay"+j).value)
		{
			costMask = 0x080;
		}
		
		else if ("Conf_Reg" == document.getElementById("RegDay"+j).value)
		{
			costMask = 0x100;
		}

		else if ("Day_Reg" == document.getElementById("RegDay"+j).value)
		{
			costMask = 0x200;
		}

		else
		{
			alert("Error: Undefined RegDay Value in getIndivCost");
			return false;
		}


		// Detrmine Age
		if ("0" == document.getElementById("RegAge"+j).value)
		{
			if (validRegInfo()) continue; else return false;
		}

		else if ("Under4" == document.getElementById("RegAge"+j).value)
		{
			costMask |= 0x01;
		}

		else if ("4To10" == document.getElementById("RegAge"+j).value) 
		{
			costMask |= 0x02;
		}

		else if ("Over10" == document.getElementById("RegAge"+j).value) 
		{
			costMask |= 0x04;
		}

		else
		{
			alert("Error: Undefined RegAge Value in getIndivCost");
			return false;
		}

		if		(costMask == 0x11)	indCost = aCost[0].iUnder4;
		else if	(costMask == 0x12)	indCost = aCost[0].i4TO10;
		else if	(costMask == 0x14)	indCost = aCost[0].iOver10;
		
		else if	(costMask == 0x21)	indCost = aCost[1].iUnder4;
		else if	(costMask == 0x22)	indCost = aCost[1].i4TO10;
		else if	(costMask == 0x24)	indCost = aCost[1].iOver10;

		else if	(costMask == 0x41)	indCost = aMealCost[0].iUnder4;			
		else if	(costMask == 0x42)	indCost = aMealCost[0].i4TO10;			
		else if	(costMask == 0x44)	indCost = aMealCost[0].iOver10;
		
		else if	(costMask == 0x81)	indCost = aMealCost[1].iUnder4;		
		else if	(costMask == 0x82)	indCost = aMealCost[1].i4TO10;	
		else if	(costMask == 0x84)	indCost = aMealCost[1].iOver10;
                                         
		
		else if	(costMask == 0x101)	indCost = 0;		
		else if	(costMask == 0x102)	indCost = 0;
		else if	(costMask == 0x104)	indCost = cREGISTRATION;
		                                  
		else if	(costMask == 0x201)	indCost = 0;		
		else if	(costMask == 0x202)	indCost = 0;			
		else if	(costMask == 0x204)	indCost = cREGISTPERDAY;
                                         
//		 alert("costMask: " + costMask + "; " + "indCost: " + indCost);

		document.getElementById("RegCost"+j).style.textAlign = "right";
		document.getElementById("RegCost"+j).value = formatCurrency(indCost);
		document.getElementById("RegCost"+j).readOnly=true;
		totCost += eval(indCost);
	}

	if("Hotel" == document.RegFormS3.AccomType.value)
	{
		HotelRmCost = (aCost[0].iOver10 * document.RegFormS3.QtyHotelRoom.value);
		document.getElementById('HotelCost').value = formatCurrency(HotelRmCost);
		totCost += eval(HotelRmCost);
	}
	document.RegFormS3.totalCost.value = formatCurrency(totCost);
	document.getElementById("mssg").style.visibility = "hidden";

	return true;
}

//---------------------------------------------------------------
// SetAlert() 
// called by RegDay and RegAge to set appropriate message.
// this reduces the number of times the alert is called
// when RegAge is empty. getIndivCost is only called
// when RegAge is set to a value other than null.
//---------------------------------------------------------------
function SetAlert(i)
{
	if (0 != document.getElementById("RegAge"+i).value){
		getIndivCost();
	}	
}


//---------------------------------------------------------------
// Add an event handler to handle selection.  initialized in bottom RegDyn. 
//---------------------------------------------------------------
function handleAccom()
{
	document.RegFormS2.AccomType[0].onclick = function() { removeTblHotelRooms();}
	document.RegFormS2.AccomType[1].onclick = function() { removeTblHotelRooms();}
	document.RegFormS2.AccomType[2].onclick = function() { ReqNoOfRooms();}
}


//---------------------------------------------------------------
// display the Accomodation Type only if it is May Conf
//---------------------------------------------------------------
function ShowAccomType()
{
	var mystr;
	
	if (true == MayConf)
	{
		mystr = "<table width='600px' border='1px' cellpadding='1px' cellspacing='0' bgcolor='#ffffcc'><tr>";
		mystr += "<td class='frmDesc' style='width:200px; text-align:right; vertical-align:middle; padding-right:10px;'><span style='color:#ff0000; font-weight:bold;margin-left:10px;'>*</span>Accommodation Type:</td>";
		mystr += "<td style='text-align:left' id='Accom'>";
		mystr += "<span style='margin-left:10px;'><input type='radio' name='AccomType' value='Dorm'  /> Dorm</span>";
		mystr += "<span style='margin-left:10px;'><input type='radio' name='AccomType' value='Suite' /> Suite</span>";
		mystr += "<span style='margin-left:10px;'><input type='radio' name='AccomType' value='Hotel' /> Hotel</span>";
		mystr += "</td></tr></table>";

		document.getElementById('idAccomType').innerHTML = mystr;

		// initialize radio button handler
		handleAccom();
	}
}


//---------------------------------------------------------------
// used on RegStep2
// if hotel is selected display the the number of rooms required by guest
//---------------------------------------------------------------
function ReqNoOfRooms()
{
	var mystr;
	
	if (true == MayConf)
	{
		mystr = "<table id='tblHotelRooms' width='600px' border='1px' cellpadding='1px' cellspacing='0' bgcolor='#ffffcc'><tr>";
		mystr += "<td class='frmDesc' style='width:200px; text-align:right; vertical-align:middle; padding-right:10px;'><span style='color:#ff0000; font-weight:bold;margin-left:10px;'>*</span>Tell Us how many rooms you need:</td>";
		mystr += "<td style='text-align:left' id='HotelRooms'>";
		mystr += "<span style='margin-left:10px;'><input type='text' id='QtyHotelRoom' name='QtyHotelRoom' value='' size='1' maxlength='1' /> Hotel Room(s)</span>";
		mystr += "</td></tr></table>";

		document.getElementById('NoOfRooms').innerHTML = mystr;
	}
}

//---------------------------------------------------------------
//---------------------------------------------------------------
function removeTblHotelRooms() {
document.getElementById('NoOfRooms').innerHTML = "";
}


//---------------------------------------------------------------
// used on RegStep 3 to write the room cost line
//---------------------------------------------------------------
function InsertHotelRoomCost()
{
	var mystr;
	
	if ((true == MayConf) && ("Hotel" == document.RegFormS3.AccomType.value))
	{
		mystr = "<table width='100%' border='0px' cellpadding='1px' cellspacing='0' bgcolor='#ffffcc'><tr>";
		mystr += "<td style='vertical-align: middle; text-align: right; font:normal normal normal 8pt Tahoma;' width='88%'>Number of Rooms Requested: " + document.RegFormS3.QtyHotelRoom.value + ", Cost</td>";
		mystr += "<td style='text-align:right'>";
		mystr += "<input type='text' id='HotelCost' name='HotelCost' value='' size='6' maxlength='8' readonly onFocus='this.blur();' class='align_rt' />";
		mystr += "</td></tr></table>";

		document.getElementById('HotelRoomCost').innerHTML = mystr;
//		getIndivCost();
	}
}

//---------------------------------------------------------------
// Define function to format a value as currency:
// Courtesy of http://www7.brinkster.com/cyanide7/
//---------------------------------------------------------------
function formatCurrency(num)
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num =	num.substring(0,num.length-(4*i+3))+','+
				num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}


//---------------------------------------------------------------
// setRecalcMsg
// NOTE: Do not add paran in DOM call when using a dynamic msg handler
//---------------------------------------------------------------
function setRecalcMsg() 
{
	document.getElementById("mssg").style.visibility = "visible";
}

// --------------------------------------------------------------------------------------
// Ultimate client-side JavaScript client sniff.
// (C) Netscape Communications 1999.  Permission granted to reuse and distribute.
// Revised 17 May 99 to add is_nav5up and is_ie5up (see below).
// Revised 20 Dec 00 to add is_gecko and change is_nav5up to is_nav6up
//                      also added support for IE5.5 Opera4&5 HotJava3 AOLTV

// Everything you always wanted to know about your JavaScript client
// but were afraid to ask. Creates "is_" variables indicating:
// (1) browser vendor:
//     is_nav, is_ie, is_opera, is_hotjava
// (2) browser version number:
//     is_major (integer indicating major version number: 2, 3, 4 ...)
//     is_minor (float   indicating full  version number: 2.02, 3.01, 4.04 ...)
// (3) browser vendor AND major version number
//     is_nav2, is_nav3, is_nav4, is_nav4up, is_nav6,is_nav6up,is_gecko, is_ie3,
           is_ie4, is_ie5, is_ie5up, is_ie4up, is_ie5_5, is_ie5_5up, is_hotjava3,is_hotjava3up,
           is_opera4, is_opera5, is_opera5up
// (4) JavaScript version number:
//     is_js (float indicating full JavaScript version number: 1, 1.1, 1.2 ...)
// (5) OS platform and version:
//     is_win, is_win16, is_win32, is_win31, is_win95, is_winnt, is_win98
//     is_os2
//     is_mac, is_mac68k, is_macppc
//     is_unix
//        is_sun, is_sun4, is_sun5, is_suni86
//        is_irix, is_irix5, is_irix6
//        is_hpux, is_hpux9, is_hpux10
//        is_aix, is_aix1, is_aix2, is_aix3, is_aix4
//        is_linux, is_sco, is_unixware, is_mpras, is_reliant
//        is_dec, is_sinix, is_freebsd, is_bsd
//     is_vms
//
// Note: you don't want your Nav4 or IE4 code to "turn off" or
// stop working when new versions of browsers are released, so
// in conditional code forks, use is_ie5up ("IE 5.0 or greater") 
// is_opera5up ("Opera 5.0 or greater") instead of is_ie5 or is_opera5
// to check version in code which you want to work on future
// versions.

    // convert all characters to lowercase to simplify testing
    var agt=navigator.userAgent.toLowerCase();

    // *** BROWSER VERSION ***
    // Note: On IE5, these return 4, so use is_ie5up to detect IE5.
    var is_major = parseInt(navigator.appVersion);
    var is_minor = parseFloat(navigator.appVersion);

    // Note: Opera and WebTV spoof Navigator.  We do strict client detection.
    // If you want to allow spoofing, take out the tests for opera and webtv.
    var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
                && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
                && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
    var is_nav2 = (is_nav && (is_major == 2));
    var is_nav3 = (is_nav && (is_major == 3));
    var is_nav4 = (is_nav && (is_major == 4));
    var is_nav4up = (is_nav && (is_major >= 4));
    var is_navonly      = (is_nav && ((agt.indexOf(";nav") != -1) ||
                          (agt.indexOf("; nav") != -1)) );
    var is_nav6 = (is_nav && (is_major == 5));
    var is_nav6up = (is_nav && (is_major >= 5));
    var is_gecko = (agt.indexOf('gecko') != -1);


    var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
    var is_ie3    = (is_ie && (is_major < 4));
    var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5")==-1) );
    var is_ie4up  = (is_ie && (is_major >= 4));
    var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0") !=-1));
    var is_ie5_5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
    var is_ie5up  = (is_ie && !is_ie3 && is_ie4);
    var is_ie5_5up =(is_ie && !is_ie3 && !is_ie4 && !is_ie5);

    // IE6 in WinXP SP2
    var is_ie6_sp2 = (window.navigator.userAgent.indexOf("SV1") != -1);

    // KNOWN BUG: On AOL4, returns false if IE3 is embedded browser
    // or if this is the first browser window opened.  Thus the
    // variables is_aol, is_aol3, and is_aol4 aren't 100% reliable.
    var is_aol   = (agt.indexOf("aol") != -1);
    var is_aol3  = (is_aol && is_ie3);
    var is_aol4  = (is_aol && is_ie4);

    var is_opera = (agt.indexOf("opera") != -1);
        var is_opera4 = (agt.indexOf("opera 4") != -1);
        var is_opera5 = (agt.indexOf("opera 5") != -1);
        var is_opera5up = (is_opera && (is_major >=4));

    var is_webtv = (agt.indexOf("webtv") != -1); 

        
    var is_TVNavigator = ((agt.indexOf("navio") != -1) || (agt.indexOf("navio_aoltv") != -1)); 
        var is_AOLTV = is_TVNavigator;

    var is_hotjava = (agt.indexOf("hotjava") != -1);
    var is_hotjava3 = (is_hotjava && (is_major == 3));
    var is_hotjava3up = (is_hotjava && (is_major >= 3));
