/******************************************************************************************
    This document contains the functions necessary to carry out the various 
	operations of the Bus Route application
	
	Filename: common.js	
	Created by: Ben Marshall
	Created date: 09/10/2007
	Last Modified: 09/11/2007
	
	Functions:
		
		
*******************************************************************************************/
/*******************************************************************************************

			Begin:     Function sendPost()
			
			Description: Sends input from form1 using POST method

*******************************************************************************************/
function sendPost(module, opt){
  
  var calling_module = "";
  
  var params = "";
  
  switch (module){
    case 'new_bus': params = "bus_num="+encodeURIComponent(document.getElementById('bus_num').value)+"&"+
	                         "passengers="+encodeURIComponent(document.getElementById('passengers').value)+"&"+
                             "driver="+encodeURIComponent(document.getElementById('driver').value)+"&"+
                             "route="+encodeURIComponent(document.getElementById('route').value);
					calling_module = "management";
					break;
					
    case 'update_bus': var busid = document.getElementById('bus_num_update');
	                   params = "id="+encodeURIComponent(busid.options[busid.selectedIndex].value)+"&"+
	                            "passengers="+encodeURIComponent(document.getElementById('passengers_update').value)+"&"+
                                "driver="+encodeURIComponent(document.getElementById('driver_update').value)+"&"+
                                "route="+encodeURIComponent(document.getElementById('route_update').value);
					   calling_module = "management";
					   break;
					   
	case 'remove_bus': var busid = document.getElementById('bus_num_remove');
					   var response = confirm("Really remove bus #"+busid.options[busid.selectedIndex].innerHTML+" from the database?")
					   if (!response){
					     return;
					   }	                  
	                   params = "id="+encodeURIComponent(busid.options[busid.selectedIndex].value);
					   calling_module = "management";
                       break;	
					   
    case 'choose_bus': var busid = document.getElementById('bus_num_choose');
	                   params = "id="+encodeURIComponent(busid.options[busid.selectedIndex].value);
					   calling_module = "assignments";
					   break;
					   
    case 'new_assignment': if (parseInt(document.getElementById('temp_pass').value) == 0){
		                     alert("You must specify at least one temporary passenger.");
							 return;
	                       }
                           if (Date.parse(document.getElementById('date_start').value) > Date.parse(document.getElementById('date_end').value)) {
                             alert("Invalid Date Range!\nStart Date cannot be after End Date!");
                             return;
                           }
                           if (Date.parse(document.getElementById('date_start').value) < Date.parse(document.getElementById('today').value)) {
                             alert("Invalid Date Range!\nStart Date cannot be before Today's Date!");
                             return;
                           }
						   if (document.getElementById('notes').value == ""){
		                     alert("You must add notes about this temporary assignment.");
							 return;
	                       }
	                       params = "bus_id="+encodeURIComponent(document.getElementById('bus_id').value)+"&"+
	                                "temp_pass="+encodeURIComponent(document.getElementById('temp_pass').value)+"&"+
                                    "date_start="+encodeURIComponent(document.getElementById('date_start').value)+"&"+
                                    "date_end="+encodeURIComponent(document.getElementById('date_end').value)+"&"+
                                    "notes="+encodeURIComponent(document.getElementById('notes').value);
					       calling_module = "assignments";
						   break;
						   
	case 'remove_assign': var response = confirm("Really remove this temporary assignment from the database?")
					      if (!response){
					        return;
					      }
	                      params = "bus_id="+encodeURIComponent(document.getElementById('bus_id').value)+"&"+
	                               "assign_id="+encodeURIComponent(opt);
	                      calling_module = "assignments";
						  break;
						  
	default: break;
  }

  makeRequest(calling_module+".php", params, null);  // Submit Form


  
  var wait = document.getElementById('wait');
  wait.innerHTML = '<div id="div1"></div><div id="div2">Please Wait...</div>';
  wait.style.visibility = "visible";  
  setTimeout("document.getElementById('wait').style.visibility = 'hidden';", 1800);
  
  /*if (calling_module != "assignments"){
    //setTimeout("makeRequest('"+calling_module+".php', null, null);", 1800);
  }*/
}
/*******************************************************************************************

			End:     Function sendPost()

*******************************************************************************************/
/*******************************************************************************************

			Begin:     Function checkMaxMin()
			
			Description: checks for proper maximum (maxNum) and minimum (0) values

*******************************************************************************************/
function checkMaxMin(object, maxNum){
  var invalid = false;
  var numValue = parseInt(object.value);

  if (!isNaN(numValue)) {
    if (numValue < 0 || numValue > maxNum){
      invalid = true;
    }
    else{
      object.style.background = '#ffffff';
	  if (document.getElementById('assignment_count') != null){
	    document.getElementById('assignment_count').innerHTML = maxNum-parseInt(object.value);
	  }
    }
  }
  else {
    invalid = true;
  }
  if (invalid){
    object.value = 0;
    object.style.background = '#ffff33';
    alert("The passenger assingment must be a number no greater than "+maxNum+".");
	if (document.getElementById('assignment_count') != null){
	  document.getElementById('assignment_count').innerHTML = maxNum;
	}
  }
}
/*******************************************************************************************

			End:     Function checkMaxMin()

*******************************************************************************************/
/*******************************************************************************************

			Begin:     Function getModule()
			
			Description: Sends a request to retrive a module/page from AJAX

*******************************************************************************************/
function getModule(module, params){
  var colOne = document.getElementById('colOne');
  if (module == 'photo'){
    colOne.style.padding = "0px";
    colOne.style.width = "690px";
    colOne.style.height = "435px";
	colOne.style.overflow = "hidden";
  }
  else{
    colOne.style.padding = "20px";
    colOne.style.width = "648px";
    colOne.style.height = "395px";
	colOne.style.overflowX = "hidden";
	colOne.style.overflowY = "auto";

  }
  
  if (params != null){
    makeRequest(module+'.php', params, null);
  }
  else {
    makeRequest(module+'.html', null, null);
  }
}
/*******************************************************************************************

			End:     Function getModule()

*******************************************************************************************/
/*******************************************************************************************

			Begin:     Function changeAmount()
			
			Description: 

*******************************************************************************************/
function changeAmount(object, action){
  var amountField = object.parentNode.parentNode.cells[1].firstChild;
  var amount = amountField.value;
  
  var minAmount = 0;
  var maxAmount = 65;
  
  if (document.getElementById('assignment_count_hidden') != null){
    maxAmount = parseInt(document.getElementById("assignment_count_hidden").innerHTML);
  }

  switch (action) {
    case 'up':  if(parseInt(amount) < maxAmount){
                  amountField.value = parseInt(amount) + 1;
                }
                break;
    case 'down': if (parseInt(amount) > minAmount){
                   amountField.value = parseInt(amount) - 1;
                 }
                 break;
  }
  
  if (document.getElementById('assignment_count') != null){
    document.getElementById("assignment_count").innerHTML = maxAmount-parseInt(amountField.value);
  }

  amountField.style.background = '#ffffff';
}
/*******************************************************************************************

			End:     Function changeAmount()

*******************************************************************************************/
/*******************************************************************************************

			Begin:     Function execJS()
			
			Description: Execute scripts embeded on first line of called ajax modules/pages

*******************************************************************************************/  
function execJS(node) {
  var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
  var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
  var bMoz = (navigator.appName == 'Netscape');
  
  var st = node.getElementsByTagName('SCRIPT');
  var strExec;
  for(var i=0;i<st.length; i++) {
    if (bSaf) {
      strExec = st[i].innerHTML;
	} 
    else if (bOpera) {
      strExec = st[i].text;
    } 
	else if (bMoz) {
      strExec = st[i].textContent;
	}
	else {
      strExec = st[i].text;
    }
    try {
      eval(strExec);
    } 
	catch(e) {
      alert(e);
    }
  }
}
/*******************************************************************************************

			End:     Function execJS()

*******************************************************************************************/
/*******************************************************************************************

			Begin:     Function updateData()
			
			Description: Execute scripts embeded on first line of called ajax modules/pages

*******************************************************************************************/  
function updateData(object) {
   var id = object.options[object.selectedIndex].value;
   
   for (var key in buses){ 
     if (buses[key]['id'] == id){
	   document.getElementById('passengers_update').value = buses[key]['passengers'];
	   document.getElementById('driver_update').value = buses[key]['driver'];
	   document.getElementById('route_update').value = buses[key]['route'];
	 }
   }
   
}
/*******************************************************************************************

			End:     Function updateData()

*******************************************************************************************/
/*******************************************************************************************

			Begin:     Function ismaxlength()
			
			Description: checks for max lenght

*******************************************************************************************/ 
function ismaxlength(obj){
  var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : "";
  if (obj.getAttribute && obj.value.length>mlength){
    obj.value=obj.value.substring(0,mlength);
  }
}
/*******************************************************************************************

			End:     Function ismaxlength()

*******************************************************************************************/
/*******************************************************************************************

			Begin:     Function printThis()
			
			Description: Prints the selected div tag.

*******************************************************************************************/
function printThis(divObj)
{ 
  var disp_setting='toolbar=yes,location=no,directories=yes,menubar=yes,'; 
      disp_setting+='scrollbars=yes,width=650, height=600, left=100, top=25'; 
  var content_vlue = document.getElementById(divObj).innerHTML; 
  
  var docprint=window.open("","",disp_setting); 
   docprint.document.open(); 
   docprint.document.write('<html><link href="css/buses.css" rel="stylesheet" type="text/css" /><head><title>The Marshall\'s Recipe Archive</title>'); 
   docprint.document.write('</head><body class="print" onload="self.print()">');          
   docprint.document.write(content_vlue);          
   docprint.document.write('</body></html>'); 
   docprint.document.close(); 
   docprint.focus(); 
}
/*******************************************************************************************

			End:     Function printThis()

*******************************************************************************************/

