///////////////////
// Globals
///////////////////

var gChildrenArray = new Array;

var ModalDialogWindow;
var ModalDialogInterval;
var ModalDialog = new Object;

ModalDialog.first_name = '';
ModalDialog.last_name = '';
ModalDialog.gender = '';
ModalDialog.age = '';
ModalDialog.status = '';
ModalDialog.eventhandler = '';


///////////////////
// Functions
///////////////////

function InitPageEdit()
{
	var myForm = document.TheForm;
	
	if (init) {

		document.TheForm.first.value = myInfo['first'];
		document.TheForm.last.value = myInfo['last'];
		document.TheForm.address.value = myInfo['address'];
		document.TheForm.city.value = myInfo['city'];
		SetDropDownValue(document.TheForm.state,myInfo['state']);
		document.TheForm.zip.value = myInfo['zip'];
		document.TheForm.phone.value = myInfo['phone'];
		document.TheForm.email.value = myInfo['email'];
		document.TheForm.emergency_first_name.value = myInfo['emergency_first_name'];
		document.TheForm.emergency_last_name.value = myInfo['emergency_last_name'];
		document.TheForm.emergency_phone.value = myInfo['emergency_phone'];
	
	}	
}

function InitPageReview()
{
	var myForm = document.TheForm;
	
	if (init) {

		// Initialize children
		
		if (myInfo['child1_first']) {
			myForm.child1_first.value = window.myInfo['child1_first'];
			myForm.child1_last.value = window.myInfo['child1_last'];
			SetDropDownValue(myForm.child1_gender,window.myInfo['child1_gender']);
			SetDropDownValue(myForm.child1_age,window.myInfo['child1_age']);
		}
		if (myInfo['child2_first']) {
			myForm.child2_first.value = window.myInfo['child2_first'];
			myForm.child2_last.value = window.myInfo['child2_last'];
			SetDropDownValue(myForm.child2_gender,window.myInfo['child2_gender']);
			SetDropDownValue(myForm.child2_age,window.myInfo['child2_age']);
		}

		if (myInfo['child3_first']) {		
			myForm.child3_first.value = window.myInfo['child3_first'];
			myForm.child3_last.value = window.myInfo['child3_last'];
			SetDropDownValue(myForm.child3_gender,window.myInfo['child3_gender']);
			SetDropDownValue(myForm.child3_age,window.myInfo['child3_age']);
		}
		
		if (myInfo['child4_first']) {
			myForm.child4_first.value = window.myInfo['child4_first'];
			myForm.child4_last.value = window.myInfo['child4_last'];
			SetDropDownValue(myForm.child4_gender,window.myInfo['child4_gender']);
			SetDropDownValue(myForm.child4_age,window.myInfo['child4_age']);
		}
		
		if (myInfo['child5_first']) {
			myForm.child5_first.value = window.myInfo['child5_first'];
			myForm.child5_last.value = window.myInfo['child5_last'];
			SetDropDownValue(myForm.child5_gender,window.myInfo['child5_gender']);
			SetDropDownValue(myForm.child5_age,window.myInfo['child5_age']);
		}
	}	
}


function ModalDialogHelper()
{
	window.clearInterval(ModalDialogInterval);
	
	if (ModalDialog.status == 'open') {
		ModalDialogInterval = window.setInterval("ModalDialogHelper()",5);
	} else {
		eval(ModalDialog.eventhandler);
	}	
}

function ModalDialogShow(Title,BodyText,Buttons,EventHandler)
{

   ModalDialog.eventhandler = EventHandler;

	var args='width=300,height=225,left=325,top=300,toolbar=0,';
	args+='location=0,status=0,menubar=0,scrollbars=1,resizable=0';  

	ModalDialogWindow=window.open("","",args); 
	ModalDialogWindow.document.open(); 
	ModalDialogWindow.document.write('<html>');
	ModalDialogWindow.document.write('<head>'); 
	ModalDialogWindow.document.write('<link href="scripts/styles.css" rel="stylesheet" type="text/css">');
	ModalDialogWindow.document.write('<script language="javascript" src="scripts/ryan.js"></script>');	
	ModalDialogWindow.document.write('<title>' + Title + '</title>');
	ModalDialogWindow.document.write('</head>');
	ModalDialogWindow.document.write('<body>');	
	ModalDialogWindow.document.write('<table border=0 width="95%" align=center cellspacing=0 cellpadding=2>');
	ModalDialogWindow.document.write('<tr><td align=left class=body6>' + BodyText + '</td></tr>');
	ModalDialogWindow.document.write('<tr><td align=center>' + Buttons + '</td></tr>');
	ModalDialogWindow.document.write('<tr><td align=center><a href=javascript:CloseForm();><IMG src="images/btn_addchild.gif" border=0></a></td></tr></table>');	
	
	
	ModalDialogWindow.document.write('</body>');
	ModalDialogWindow.document.write('</html>'); 
	ModalDialogWindow.document.close(); 
	ModalDialogWindow.focus(); 

}

function CloseForm()
{
	window.opener.ModalDialog.first_name = window.ChildForm.child_first.value;
	window.opener.ModalDialog.last_name = window.ChildForm.child_last.value;
	window.opener.ModalDialog.gender = GetDropDownValue(window.ChildForm.gender);
	window.opener.ModalDialog.age = GetDropDownValue(window.ChildForm.age);			
	window.opener.ModalDialog.status = "";
	window.close();
}

function GetDropDownValue(inObj)
{

	var i;

	for (i = 0; i < inObj.options.length; i++) {
		if (inObj.options[i].selected) {
			return inObj.options[i].value;
		}
	}
	//alert("GetDropDownValue: Couldn't find selected value");	
}

function SetDropDownValue(inObj,inValue)
{
	var i;
	for (i = 0; i < inObj.options.length; i++) {
		if (inObj.options[i].value == inValue) {
			inObj.options[i].selected = true;
			return;
		}
	}
	//alert("SetDropDownValue: Couldn't set " + inValue);	
}

function AddChild(EventHandler)
{
	var myText=''; 
	myText = MakeNewRowText();
	ModalDialog.status = "open";
	ModalDialogShow("Dialog","Add Child:",myText ,EventHandler);
	ModalDialogInterval = window.setInterval("ModalDialogHelper()",5);	
}

function AddChildReturnMethod()
{
	var newChild = new Object;
	
	newChild.first_name = ModalDialog.first_name;
	newChild.last_name = ModalDialog.last_name;
	newChild.gender = ModalDialog.gender;
	newChild.age = ModalDialog.age;

	gChildrenArray[gChildrenArray.length] = newChild;	
	
	// Cleanup
	ModalDialog.value = '';
	ModalDialog.eventhandler = '';	
	
	GenerateChildrenTable();
}


function popup(url, width, height)
{
	var popup = window.open(url, null, 'width=' + width + ',height=' + height + ',resizable,scrollbars');
}

function MakeNewRowText()
{
	var NewRow = "";

	NewRow = NewRow + "<form name='ChildForm'><table><TR><TD CLASS=body1>First Name:</TD><TD><input type='text' name='child_first' size='14' style='border-style: solid; border-width: 1px; padding: 0' class='body2'></td></TR>";
	NewRow = NewRow + "<TR><TD CLASS=body1>Last Name:</TD><TD><input type='text' name='child_last' size='14' style='border-style: solid; border-width: 1px; padding: 0' class='body2'></TD></TR>";
	NewRow = NewRow + "<TR><TD CLASS=body1>Gender:</TD><TD><select name='gender' class='body2'><option value='M'>Male</option><option value='F'>Female</option></select></TD></TR>";
	NewRow = NewRow + "<TR><TD CLASS=body1>Age:</TD><TD><select name='age' class='body2' size='1'><option value='1'>1 year</option><option value='1.5'>1.5 years</option><option value='2'>2 years</option><option value='3'>3 years</option><option value='4'>4 years</option><option value='5'>5 years</option><option value='6'>6 years</option><option value='7'>7 years</option><option value='8'>8 years</option><option value='9'>9 years</option><option value='10'>10 years</option><option value='11'>11 years</option><option value='12'>12 years</option></select></TD></TR></TABLE></FORM>";

	return(NewRow);
}

function GenerateChildrenTable()
{
	var i = 0;
	var tableHeader = "<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0 width=400 CLASS=body2><TR><TD CLASS=body1>First</TD><TD CLASS=body1>Last</TD><TD CLASS=body1>Gender</TD><TD CLASS=body1>Age</TD><TD CLASS=body1>&nbsp;</TD></TR>";
	var tableFooter = "</TABLE>";
	var tableBody = "";
	
	for (i = 0; i < gChildrenArray.length; i++) {
		if (gChildrenArray[i] != "") {
			tableBody = tableBody + "<TR><TD>" + gChildrenArray[i].first_name + "</TD><TD>" + gChildrenArray[i].last_name + "</TD><TD>" + gChildrenArray[i].gender + "</TD><TD>" + gChildrenArray[i].age + "</TD><TD><a href='javascript:RemoveChild("+i+")'><img src='images/btn_removechild.gif' border=0></a></TD></TR>";
		}
	}
	
	DIV_CHILDREN_TABLE.innerHTML = tableHeader + tableBody + tableFooter;
}

function RemoveChild(inID)
{
	gChildrenArray[inID] = "";
	GenerateChildrenTable();
}

function isdefined(variable)
{
	var result;
	
    if (typeof(window[variable]) == "undefined") {
    	result = false
	} else {
    	result = true;
    }
    
    return result;
}

