// JavaScript Document

<!--
//
// Make sure page does not reside in another page's frame
//
if ( top.location != self.location ) {

     top.location = self.location 
}

// 
//    CONSTANTS     
//
var numDays     = 7;
var numMonths   = 12; 
var numWestPorts = 19;
var numEastPorts = 18;
var numOtherPorts = 18;
var oneDay      = 24;
var numPageImg  = 3;
var numTopImgs  = 5;
var mDate;
var functionName;

//  
//   Function Definitions
//   

function CreateStringArray( size ) {

          this.length = size;  
          return this;
}

function CreateImageArray(size) 
{
          this.length = size
          for (var i = 0; i<size; i++) {
               this[i] = new Image()
          }
          return this
}         

var amonths  	= new CreateStringArray( numMonths );
var days     	= new CreateStringArray( numDays );
var westPorts   = new CreateStringArray( numWestPorts );
var eastPorts   = new CreateStringArray( numEastPorts );
var otherPorts  = new CreateStringArray( numOtherPorts );



amonths[1]  = "January";    amonths[2]  = "February";  
amonths[3]  = "March";      amonths[4]  = "April";  
amonths[5]  = "May";        amonths[6]  = "June";
amonths[7]  = "July";       amonths[8]  = "August";  
amonths[9]  = "September";  amonths[10] = "October";  
amonths[11] = "November";   amonths[12] = "December"; 

days[1]  = "Sunday";    days[2]  = "Monday";  
days[3]  = "Tuesday";   days[4]  = "Wednesday";  
days[5]  = "Thursday";  days[6]  = "Friday";
days[7]  = "Saturday"; 

westPorts[0] = "Choose one..";
westPorts[1] = "Abidjan";
westPorts[2] = "Banjul";
westPorts[3] = "Conakry";
westPorts[4] = "Cotonou";
westPorts[5] = "Dakar";
westPorts[6] = "Douala";
westPorts[7] = "Freetown";
westPorts[8] = "Lagos";
westPorts[9] = "Libreville";
westPorts[10] = "Lome";
westPorts[11] = "Luanda";
westPorts[12] = "Matadi";
westPorts[13] = "Monrovia";
westPorts[14] = "Nouakchott";
westPorts[15] = "Pointe Noire";
westPorts[16] = "Port Harcourt";
westPorts[17] = "Takoradi";		
westPorts[18] = "Tema";

eastPorts[0] = "Choose one..";
eastPorts[1] = "Beira";
eastPorts[2] = "Bujumbara";
eastPorts[3] = "Dar Es Salaam";
eastPorts[4] = "Djibouti";
eastPorts[5] = "Embakasi";
eastPorts[6] = "Entebbe";
eastPorts[7] = "Kampala";
eastPorts[8] = "Kigali";
eastPorts[9] = "Kitwe";
eastPorts[10] = "Lubumbashi";
eastPorts[11] = "Lusaka";
eastPorts[12] = "Maputo";
eastPorts[13] = "Mbarare";
eastPorts[14] = "Mombassa";
eastPorts[15] = "Nacala";
eastPorts[16] = "Nairobi";
eastPorts[17] = "Ndola";

function setDivContent( obj )
{	
	var divText = "";

	if ( obj.selectedIndex == ( obj.options.length - 1 ) )
	{
		divText = "<input type='text' name='othersubject' value=''>";
	}
	writeToDiv( divText, 'othersubject' );

}

function writeToDiv( text, id )
{
//alert( id );
     if (document.getElementById)
     {
          x = document.getElementById(id);
          x.innerHTML = '';
          x.innerHTML = text;
     }
     else if (document.all)
     {
          x = document.all[id];
          x.innerHTML = text;
     }
     else if (document.layers)
     {
          x = document.layers[id];          
          x.document.open();
          x.document.write(text);
          x.document.close();
     }
}

function trim (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function fieldIsBlank( field )
{
     return ( ( field.value.length == 0 ) || ( trim( field.value ).length == 0 ) )
}

function rand() {
      
     var rndNum = Math.floor( Math.random() * 10 );
     return rndNum
}

function showTime( ) 
{
  var now = new Date( );
  var hr  = now.getHours();
  var min = now.getMinutes();
  var sec = now.getSeconds();
  var ampm = ( hr >= 12 ) ? " PM" : " AM";

  hr = ( hr >= 13 ) ? hr - 12 : hr;
  hr = ( hr == 0 ) ? 12 : hr;

  sec = ( sec < 10 ) ? "0" + sec : sec;
  min = ( min < 10 ) ? "0" + min : min;

  writeToDiv( hr + ":" + min + ":" + sec + ampm, 'timediv' );
  setTimeout( "showTime( )", 1000 );
  mDate += 1000;
}
	 
function showDate( splitit ) {

  var  now = new Date( );
  var   localYear = now.getYear();
  var splitdate = ", ";

  if ( splitit == 1 ) { splitdate =  "<br>"; }


  localYear = ( localYear <= 1000 ) ? localYear + 1900 : localYear;

      writeToDiv(  days[now.getDay() + 1] + splitdate + amonths[now.getMonth() + 1] + " " + now.getDate() + 
	      ",  " + now.getFullYear() + " &nbsp; - &nbsp;", 'datediv' );

}  
	 
function  writeLocalDateTime( )
{
	showTime( );
	showDate( );		
}
  
/*****************************************************
* Ensure that the argument looks like an email address
*****************************************************/

function emailIsValid( email ) 
{
   if ( ( (email.value.indexOf('@') != -1) &&
        (email.value.indexOf('.') != -1) ) || 
        ( email.value.length == 0 ) )
  {    
  return true;
}

/********************************
* Address does not contain @ sign
********************************/
if (confirm("\nE-mail address may not be valid." +
	"\nDo you want to continue?"))
 	return true;
else {
	 //email.value = "";
	 email.focus();
	 email.select();
	 return false;
}
}
    
/*
 Check if a string is in valid email format.
 Input  : the string to check
 Output : true if the string is a valid email address, false otherwise.
 */
 function isEmail(str)
 {
	var regex = /^[-_.a-z0-9]+@(([-a-z0-9]+\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;
	return regex.test(str);
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function validateContactForm(form )
{
     var noemail;
     
     if ( fieldIsBlank( form.sender )  )
     {
	  alert( "Please enter sender's name." )
	  form.sender.focus();
	  return false;
     }

	  //alert ( form.from_email.value );
     if ( fieldIsBlank( form.from_email )  )
     {
	  noemail =  confirm( "You did not enter the sender's email address\nDo you wish to continue without it? Ok = Yes, Cancel = No." );
	  
	  if ( ! noemail ) {	  
	  	return false;
	  }
     }
     else {
     	if ( ! emailIsValid( form.from_email ) )
     		return false;
     }
     if ( form.src.value == 'S' && form.to_email.selectedIndex == 0 ) {
     		alert( "Please make a selection for receipient of email." )
		form.to_email.focus();
	 	return false;
     }

	if ( form.subject.selectedIndex == 0 ) {
     		alert( "Please select subject of email." )
		form.subject.focus();
	  	return false;
     }
     
     if ( fieldIsBlank( form.emessage )  )
     {
	  alert( "Message is blank, please enter email message." )
	  form.emessage.focus();
	  return false;
     }
     
     return true;
}

function validateBasicInfo( form )
{
	if ( getCheckedValue(form.container) == "Yes" && form.containercnt.selectedIndex == 0 ) {
     	alert( "You indicated you want to ship at least one container, \nbut did not specify the number of containers.\nPlease select the number of containers you want to ship." );
		form.containercnt.focus();
	  	return false;
	}
	if ( getCheckedValue(form.roro) == "Yes" && form.rorocnt.selectedIndex == 0 ) {
     	alert( "You indicated you want to ship at least one RO-RO, \nbut did not specify the number of RO-ROs.\nPlease select the number of Ro-ROs you want to ship." );
		form.rorocnt.focus();
	  	return false;
	}
	if ( getCheckedValue(form.roro) == "No" && getCheckedValue(form.container) == "No" ) {
     	alert( "You indicated you neither want to ship a container nor a RO-RO, so there is no where to continue to." );		
	  	return false;
	}
	return true;
}

function validateQuoteInfo( form )
{
	if ( fieldIsBlank( form.quotenumber ) ) {
     	alert( "To continue with booking, you must provide a Quote number from Bonaberi Shipping.\nIf you did not receive a Quote number, please contact Bonaberi Shipping." );
		form.quotenumber.focus();
	  	return false;
	}
	return true;
}

function setRadioButton( field, form )
{
	var slctdfld = field.name;
	var buttonfld = slctdfld.substr(0, slctdfld.indexOf('cnt') )
	
	setCheckedValue( form.elements[buttonfld], ((field.selectedIndex == 0) ? "No" : "Yes") )
}

function calculateVolume( form, idx )
{
	var fields;
	var l, w, h;
	
	fields = new Array(5);
	for (i = 0; i < fields.length; ++i)
	{
		fields [i] = new Array(5);
		fields[i][1] = "length" + ( i + 1 );
		fields[i][2] = "width" + ( i + 1 );
		fields[i][3] = "height" + ( i + 1 );
		fields[i][4] = "volume" + ( i + 1 );
	}
	l = ( form.elements[fields[idx-1][1]].value == 0 ) ? 1 : form.elements[fields[idx-1][1]].value;
	w = ( form.elements[fields[idx-1][2]].value == 0 ) ? 1 : form.elements[fields[idx-1][2]].value;
	h = ( form.elements[fields[idx-1][3]].value == 0 ) ? 1 : form.elements[fields[idx-1][3]].value;
	form.elements[ fields[idx-1][4] ].value =  l * w * h;

}

function populatePorts( selctObj, form, whichPortSelect, whichDiv )
{
	var  destSelected = selctObj.selectedIndex;
	var  option = document.createElement('option');

	if ( destSelected == 1 )
	{
		//form.elements[whichPortSelect].options.length = 0;
		writeToDiv( "<select name=\"" + whichPortSelect + "\" style=\"width: 110px\"></select>", whichDiv );
		for(var i = 0; i < numEastPorts; i++) {			
			var  option = document.createElement('option');
			option.setAttribute('value',eastPorts[i]);
			option.appendChild(document.createTextNode(eastPorts[i]));
			form.elements[whichPortSelect].appendChild(option);
		}
	}
	else if ( destSelected == 2 )
	{
		writeToDiv( "<select name=\"" + whichPortSelect + "\" style=\"width: 110px\"></select>", whichDiv );
		for(var i = 0; i < numWestPorts; i++) {
			var  option = document.createElement('option');
			option.setAttribute('value',westPorts[i]);
			option.appendChild(document.createTextNode(westPorts[i]));
			form.elements[whichPortSelect].appendChild(option);
		}
	}
	else if ( destSelected == 3 )
	{
		writeToDiv( "<input type=\"text\" name=\"" + whichPortSelect + "\" value=\"\" style=\"width: 103px\">", whichDiv );
	}

}

function validateDetails( form )
{
	var numcontainers = form.containercnt.value;
	var numroros = form.rorocnt.value;
	
	if ( fieldIsBlank( form.name )  )
    {
	  alert( "Please enter customers's name." )
	  form.name.focus();
	  return false;
    }
	if ( fieldIsBlank( form.address )  )
    {
	  alert( "Please enter customers's address or the address of the loading location." )
	  form.address.focus();
	  return false;
    }
	if ( fieldIsBlank( form.telephone ) && fieldIsBlank( form.email ) )
    {
	  alert( "We will like to be able to conatact you, if we have questions or concerns.\nPlease enter a telephone number or email address, email address is preferred." )
	  form.telephone.focus();
	  return false;
    }
	if ( fieldIsBlank( form.email )  )
     {
	  noemail =  confirm( "Email is our preferred method of contacting custoners\nDo you wish to continue without it? Ok = Yes, Cancel = No." );
	  
	  if ( ! noemail ) {	
	  	 form.email.focus();
	  	 return false;
	  }
     }
     else {
     	if ( ! emailIsValid( form.email ) )
     		return false;
     }
	 // Check COntainer Information
	 for(var i = 0; i < numcontainers; i++) 
	 {
		 if ( form.elements["departure"+(i+1)].selectedIndex == 0 )
		 {
			 alert( "Please select a departure port for container number " + (i+1) + "." );
			 return false;
		 }		
		 if ( form.elements["destination"+(i+1)].selectedIndex == 0 )
		 {
			 alert( "Please select a destination for container number " + (i+1) + "." );
			 return false;
		 }
		 if ( form.elements["destination"+(i+1)].selectedIndex == 3 &&  fieldIsBlank( form.elements["port"+(i+1)] )  )
		 {
			 alert( "Please enter a destination port for container number " + (i+1) + "." );
			 return false;
		 }
		 else if ( form.elements["port"+(i+1)].selectedIndex == 0 )
		 {
		 	alert( "Please select a destination port for container number " + (i+1) + "." );
			return false;
		 }
		 if ( form.elements["containersize"+(i+1)].selectedIndex == 0 )
		 {
			 alert( "Please select a size for container number " + (i+1) + "." );
			 return false;
		 }
		 if ( form.elements["containertype"+(i+1)].selectedIndex == 0 )
		 {
			 alert( "Please select a type for container number " + (i+1) + "." );
			 return false;
		 }
	}
	// Check COntainer Information
	for(var i = 0; i < numroros; i++) 
	{
		if ( form.elements["rorodeparture"+(i+1)].selectedIndex == 0 )
		 {
			 alert( "Please select a departure port for RO-RO number " + (i+1) + "." );
			 return false;
		 }		
		 if ( form.elements["rorodestination"+(i+1)].selectedIndex == 0 )
		 {
			 alert( "Please select a destination for RO-RO number " + (i+1) + "." );
			 return false;
		 }
		 if ( form.elements["rorodestination"+(i+1)].selectedIndex == 3 &&  fieldIsBlank( form.elements["roroport"+(i+1)] )  )
		 {
			 alert( "Please enter a destination port for RO-RO number " + (i+1) + "." );
			 return false;
		 }
		 else if ( form.elements["roroport"+(i+1)].selectedIndex == 0 )
		 {
		 	alert( "Please select a destination port for RO-RO number " + (i+1) + "." );
			return false;
		 }
		 if ( fieldIsBlank( form.elements["length"+(i+1)] )  )
		 {
			 alert( "Please enter a length for RO-RO number " + (i+1) + "." );
			 return false;
		 }
		 if ( fieldIsBlank( form.elements["width"+(i+1)] )  )
		 {
			 alert( "Please enter a width for RO-RO number " + (i+1) + "." );
			 return false;
		 }
		 if ( fieldIsBlank( form.elements["height"+(i+1)] )  )
		 {
			 alert( "Please enter a height for RO-RO number " + (i+1) + "." );
			 return false;
		 }
	}
	return true;
}

//-->




