/* tgen.js
*  by HipDesign. v1.0
*  Copyright (c) 2010 Hair On The Move. All Rights Reserved.
*/
/*
--------------------------
Copyright (c) 2010 Hair On The Move.
All Rights Reserved.
---------------------------
*/
function calculate(FormNo) {	
	//persons
	var pArray = new Array();		
	for(i=0;i<FormNo;i++) {		
		pselectedIndex = eval("document.forms[FormNo].p" + (i+1) + ".selectedIndex");		
		pArray[i]= eval("document.forms[FormNo].p" + (i+1) + ".options[pselectedIndex].value");
		//alert("pArray[" + i + "]:" + pArray[i]);
	}	
	//names	
	var nArray = new Array();
	for(i=0;i<FormNo;i++) {				
		nArray[i]= eval("document.forms[FormNo].n" + (i+1) + ".value");
		//alert("nArray[" + i + "]:" + nArray[i]);
	}	
	
	//hs	
	var hsArray = new Array();
	for(i=0;i<FormNo;i++) {				
		for(j=0;j<eval("document.forms[FormNo].hs" + (i+1) + ".length");j++)
		{
			if (eval("document.forms[FormNo].hs" + (i+1) + "[j].checked") == true) {
				hsArray[i] = eval("document.forms[FormNo].hs" + (i+1) + "[j].value");
				//alert("hsArray[" + i + "]:" + hsArray[i]);
			}
		}
	}
	
	//Time	
	var TimeArray = new Array();
	for(i=0;i<FormNo;i++) {		
		if (pArray[i] == "Bride") {
			if (hsArray[i] == "Hair up") { TimeArray[i] = 60; } 
			else if (hsArray[i] == "Down with curls") { TimeArray[i] = 45; } 
			else if (hsArray[i] == "Blow dried & styled") { TimeArray[i] = 45; }	
			else { TimeArray[i] = 0; }	
		}										
		if (pArray[i] == "Bridesmaid") {
			if (hsArray[i] == "Hair up") { TimeArray[i] = 45; } 
			else if (hsArray[i] == "Down with curls") { TimeArray[i] = 30; } 
			else if (hsArray[i] == "Blow dried & styled") { TimeArray[i] = 30; }	
			else { TimeArray[i] = 0; }
		}
		if (pArray[i] == "Mother of the bride") {
			if (hsArray[i] == "Hair up") { TimeArray[i] = 45; } 
			else if (hsArray[i] == "Down with curls") { TimeArray[i] = 30; } 
			else if (hsArray[i] == "Blow dried & styled") { TimeArray[i] = 30; }	
			else { TimeArray[i] = 0; }
		}
		if (pArray[i] == "Mother of the groom") {
			if (hsArray[i] == "Hair up") { TimeArray[i] = 45; } 
			else if (hsArray[i] == "Down with curls") { TimeArray[i] = 30; } 
			else if (hsArray[i] == "Blow dried & styled") { TimeArray[i] = 30; }	
			else { TimeArray[i] = 0; }
		}
		if (pArray[i] == "Flower girl") {
			if (hsArray[i] == "Hair up") { TimeArray[i] = 30; } 
			else if (hsArray[i] == "Down with curls") { TimeArray[i] = 30; } 
			else if (hsArray[i] == "Blow dried & styled") { TimeArray[i] = 30; }	
			else { TimeArray[i] = 0; }
		}
		if (pArray[i] == "Other guest") {
			if (hsArray[i] == "Hair up") { TimeArray[i] = 45; } 
			else if (hsArray[i] == "Down with curls") { TimeArray[i] = 30; } 
			else if (hsArray[i] == "Blow dried & styled") { TimeArray[i] = 30; }	
			else { TimeArray[i] = 0; }
		}			
		//alert("TimeArray[" + i + "]:" + TimeArray[i]);				
	}	
	
	// Total time required
	var TotalTime = 0;
	for (i=0;i<TimeArray.length;i++) {
		TotalTime = TotalTime + TimeArray[i];
	}
	
	// Ready by time in minutes
	var by = document.getElementById("by");	
	var byVal = by.options[by.selectedIndex].value;	
	var byValMins = byVal * 60;
	
	// Ready by clock time
	var byHours = parseInt(byValMins/60);
	var byMins = byValMins % 60;
	if (byMins==0) {byMinsPart = ":00"; } else {byMinsPart = ":" + byMins};
		
	// Start time in hours and minutes
	var st = byValMins - TotalTime;
	var stHours = parseInt(st/60);
	var stMins = st % 60;
	
	// Display start time
	if (stHours<10) {stHours = "0" + stHours};	
	if (stMins==0) {stMinsPart = ":00"; } else {stMinsPart = ":" + stMins};
	document.getElementById("st").innerHTML = stHours + stMinsPart;
	document.getElementById("rb").innerHTML = byHours + byMinsPart;
	
	// W Date
	var wdate = document.getElementById("wdate");	
	var wdateVal = wdate.value;	
	document.getElementById("wdateVal").innerHTML = wdateVal;

	// Display timetable
	changeObjectVisibility("time_table","visible");
	// Hide 
	changeObjectVisibility("select_time","hidden");
	changeObjectVisibility("start","hidden");
	changeObjectVisibility("one","hidden");
	changeObjectVisibility("two","hidden");
	changeObjectVisibility("three","hidden");
	changeObjectVisibility("four","hidden");
	changeObjectVisibility("five","hidden");
	changeObjectVisibility("six","hidden");
	changeObjectVisibility("seven","hidden");
	changeObjectVisibility("eight","hidden");
	
	var stArray = new Array();
	var etArray = new Array();
	var ctsArray = new Array();
	stArray[0] = st;
	var totalCost = 0;
			
	for(i=0;i<FormNo;i++) {
		etArray[i] = stArray[i] + TimeArray[i];
		stArray[i+1] = etArray[i];

		var p_stHours = parseInt(stArray[i]/60);
		var p_stMins = stArray[i] % 60;
		if (p_stHours<10) p_stHours = "0" + p_stHours;
		if (p_stMins==0) {p_stMinsPart = ":00"; } else {p_stMinsPart = ":" + p_stMins};
		
		// Individual end times
		var p_etHours = parseInt(etArray[i]/60);
		var p_etMins = etArray[i] % 60;
		if (p_etHours<10) p_etHours = "0" + p_etHours;
		if (p_etMins==0) {p_etMinsPart = ":00"; } else {p_etMinsPart = ":" + p_etMins};		
		
		if (pArray[i] == "Bride") {		
			if (hsArray[i] == "Hair up") { ctsArray[i] = 90 } 
			else if (hsArray[i] == "Down with curls") { ctsArray[i] = 75; } 
			else if (hsArray[i] == "Blow dried & styled") { ctsArray[i] = 60; }
		}			
		if ((pArray[i] == "Bridesmaid") || (pArray[i] == "Mother of the bride") || (pArray[i] == "Mother of the groom") || (pArray[i] == "Other guest")){
			if (hsArray[i] == "Hair up") { ctsArray[i] = 50 } 
			else if (hsArray[i] == "Down with curls") { ctsArray[i] = 40; } 
			else if (hsArray[i] == "Blow dried & styled") { ctsArray[i] = 35; }
		}
		if (pArray[i] == "Flower girl") {
			if (hsArray[i] == "Hair up") { ctsArray[i] = 20 } 
			else if (hsArray[i] == "Down with curls") { ctsArray[i] = 20; } 
			else if (hsArray[i] == "Blow dried & styled") { ctsArray[i] = 20; }
		}		
		
		document.getElementById("tt_no"+(i+1)).innerHTML = (i+1) + ". ";	
		document.getElementById("tt_p"+(i+1)).innerHTML = pArray[i];	
		document.getElementById("tt_n"+(i+1)).innerHTML = nArray[i];
		document.getElementById("tt_hs"+(i+1)).innerHTML = hsArray[i];
		document.getElementById("tt_st"+(i+1)).innerHTML = p_stHours + p_stMinsPart;
		document.getElementById("tt_et"+(i+1)).innerHTML = "&nbsp;-&nbsp;" + p_etHours + p_etMinsPart;
		document.getElementById("tt_m"+(i+1)).innerHTML = TimeArray[i] + "m";		
		document.getElementById("tt_cts"+(i+1)).innerHTML = ctsArray[i];	
		
		totalCost = totalCost + ctsArray[i];
	}  
	document.getElementById("totalCost").innerHTML = totalCost;
}

<!--

// function switchDiv()
//  this function takes the id of a div
//  and calls the other functions required
//  to show that div
//
function switchDiv(div_id)
{
  var style_sheet = getStyleObject(div_id);
  if (style_sheet)
  {
    hideAll();
    changeObjectVisibility(div_id,"visible");
  }
  else 
  {
    alert("sorry, this only works in browsers that do Dynamic HTML");
  }
}

// function hideAll()
//  hides a bunch of divs
//
function hideAll()
{
   //changeObjectVisibility("start","hidden");
   changeObjectVisibility("one","hidden");
   changeObjectVisibility("two","hidden");
   changeObjectVisibility("three","hidden");
   changeObjectVisibility("four","hidden");
   changeObjectVisibility("five","hidden");
   changeObjectVisibility("six","hidden");
   changeObjectVisibility("seven","hidden");
   changeObjectVisibility("eight","hidden");
}

// function getStyleObject(string) -> returns style object
//  given a string containing the id of an object
//  the function returns the stylesheet of that object
//  or false if it can't find a stylesheet.  Handles
//  cross-browser compatibility issues.
//
function getStyleObject(objectId) {
  // checkW3C DOM, then MSIE 4, then NN 4.
  //
  if(document.getElementById && document.getElementById(objectId)) {
	return document.getElementById(objectId).style;
   }
   else if (document.all && document.all(objectId)) {  
	return document.all(objectId).style;
   } 
   else if (document.layers && document.layers[objectId]) { 
	return document.layers[objectId];
   } else {
	return false;
   }
}

function changeObjectVisibility(objectId, newVisibility) {
    // first get a reference to the cross-browser style object 
    // and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.visibility = newVisibility;
	return true;
    } else {
	// we couldn't find the object, so we can't change its visibility
	return false;
    }
}
// -->
