// ********************************
// ********************************
// ALL JAVASCRIPT FUNCTIONS IN HERE
// ********************************
// ********************************

// ***********************************************************
// Function to set focus to the first form element of the page
function focusFormElement()
	{
	if (document.forms.length > 0)
		{
		if ( (document.forms[0].elements[0].type == "text") || (document.forms[0].elements[0].type == "select-one") )
			{
			document.forms[0].elements[0].focus();
			}
		}
	}
// End function loadFormElement()
// ******************************


function doCancel()
	{
	var myConfirm = confirm("Are you sure you want to cancel and return to the previous page?");
	if (myConfirm)
		{
		history.back();
		}
	}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


// *****************************************
// Functions to check/uncheck all checkboxes 
// source: http://evolt.jeffhowden.com/jeff/code/checkbox_check_all.cfm
// usage: for the "main" checkbox, add <... onclick="toggleChecked(this)">
// for all the other checkboxes, add <... onclick="toggleIndeterminate(this)">
// ******************************************

function toggleChecked(oElement) 
	{ 
	oForm = oElement.form; 
	oElement = oForm.elements[oElement.name]; 
	if(oElement.length) 
		{ 
		bChecked = oElement[0].checked; 
  	for(i = 1; i < oElement.length; i++) 
		oElement[i].checked = bChecked; 
		} 
	} 

function toggleIndeterminate(oElement) 
	{ 
	oForm = oElement.form; 
	oElement = oForm.elements[oElement.name]; 
	if(oElement.length) 
		{
		bIndeterminate = false; 
		bChecked = true; 
		nChecked = 0; 
		for(i = 1; i < oElement.length; i++) 
		if(oElement[i].checked) 
			nChecked++; 
		if(nChecked < oElement.length - 1) 
  		{ 
			if(nChecked) 
    		bIndeterminate = true; 
	    else 
				{ 
				bIndeterminate = false; 
				bChecked = false; 
				} 
			} 
		else 
  		{ 
			bIndeterminate = false; 
			} 
		oElement[0].indeterminate = bIndeterminate; 
		oElement[0].checked = bChecked; 
		} 
	} 


// ***************************************************************
// Function to navigate to a URL when user selects a dropdown menu
function doNavigate(f_form_name, f_select_name) 
	{
	var myForm = f_form_name;
	var mySelect = f_select_name;
	var URL = eval ("document. " + myForm + "." + mySelect + ".options[document." + myForm + "." + mySelect + ".selectedIndex].value");
	window.location.href = URL;
	}
// End function doNavigate()
// *************************


// ************************************************************
// Function to open a pop-up window WITH scrollbars
function openWindow(url,name,w,h)
	{
	str="height="+h+",width="+w+",scrollbars=1,status=1";
	if (parseInt(navigator.appVersion)>3)
		{
		str+=",left="+(screen.width-w)/2+",top="+parseInt((screen.height-h)/3);
		win=window.open(url,name,str);
		}
	}
// End function openWindow()
// *************************


// ************************************************************
// Function to open a pop-up window WITHOUT scrollbars
function openWindow2(url,name,w,h)
	{
	str="height="+h+",width="+w+",menubar=0,scrollbars=0,status=1,resizable=1";
	if (parseInt(navigator.appVersion)>3)
		{
		str+=",left="+(screen.width-w)/2+",top="+parseInt((screen.height-h)/3);
		win=window.open(url,name,str);
		}
	}
// End function openWindow2()
// *************************


// ****************************************************
// Function to convert a string from textbox to capital
// Arguments: form name, text field name, text input value
// Usage: onBlur = doUpperCase(this.form.name, this.name, this.value)
// Sample: content/delivery_new_show.php
function doUpperCase(f_form_name, f_text_name, f_text_value)
	{
	eval("document." + f_form_name + ".elements[\"" + f_text_name + "\"].value = f_text_value.toUpperCase()");
	}
// End function doUpperCase()
// **************************


// *********************************************
// Function to check if a text input is a number
function checkNumber(f_form_name, f_text_name, f_text_value)
	{
	if (isNaN(f_text_value))
		{
		alert ("Please enter numbers only.");
		eval ("document." + f_form_name + ".elements[\"" + f_text_name + "\"].focus()");
		eval ("document." + f_form_name + ".elements[\"" + f_text_name + "\"].select()");
		}
	}
// End function checkNumber()
// **************************


// ************************************************************
// Function to format user input for prices into proper currency formats, 
// ie. xxx.xx | If input is not a number, it will automatically display 0.00
// Arguments: form name, text field name, text input value
// Usage: onChange = formatCurrency('form_name', 'text_field_name', this.value)
// Sample: content/quotation_new_2_show.php
function formatCurrency(f_form_name, f_text_name, f_text_value) 
	{
  // Capture the negative "-" sign and save it as $symbol
  // so we can display it back again. Otherwise,
  // the system will show only the figures without the "-" sign

	symbol_position = (f_text_value.indexOf("-"));
  if (symbol_position != -1)
    {
    symbol = "-";
    }
  else
    {
    symbol = "";
    }

	f_text_value = f_text_value.toString().replace(/\$|\,/g,'');
	if (f_text_value == '')
		{ // do nothing, leave blank
		}
	else if (isNaN(f_text_value))
		{
		// We need to put both focus and select because there's a little bug:
		// If the user clicks on a select menu after the text input, 
		// BOTH the text input and select menu will be "activated"
		// and the user cannot change anything on the text field.
		// Hence, we set focus on the text field first :-)
		alert ("Please enter numbers only.");
		eval ("document." + f_form_name + ".elements[\"" + f_text_name + "\"].value = ''");
		eval ("document." + f_form_name + ".elements[\"" + f_text_name + "\"].focus()");
		eval ("document." + f_form_name + ".elements[\"" + f_text_name + "\"].select()");
		}
	else
		{
		sign = (f_text_value == (f_text_value = Math.abs(f_text_value)));
		f_text_value = Math.floor(f_text_value * 100 + 0.50000000001);
		cents = f_text_value % 100;
		f_text_value = Math.floor(f_text_value/100).toString();
		if (cents < 10)
			{
			cents = "0" + cents;
			}
		f_text_value = symbol + "" + f_text_value + "." + cents;
		eval ("document." + f_form_name + ".elements[\"" + f_text_name + "\"].value = '" + f_text_value + "'");
		}

	}
// End function formatCurrency()
// *****************************


// *****************************************
// Function to prompt user to confirm delete 
// when they click on the garbage bin after ticking the checkboxes
// Arguments: none
// Usage: <form method="post" action="content/user_delete.php" onSubmit="return confirmDelete()">...</form>
// Sample: content/user_show.php
function confirmDelete(f_user_type)
	{

	// If the user is not an administrator, he cannot delete 
	if (f_user_type != 1)
		{
		alert ("Only the system administrator can perform this function.");
		return false;
		}
	else
		{
		// Check if the user has ticked any checkboxes
		// [insert codes here]

		if (confirm('Are you sure you want to delete the checked record(s)?'))
			{
			return true;
			}
		else
			{
			return false;
			}
		}

	}
// End function confirmDelete()
// ****************************


// ************************************************************
// Function to disable right mouse click Script
// By Maximus (maximus@nsimail.com) w/ mods by DynamicDrive
// For full source code, visit http://www.dynamicdrive.com

var message="Function Disabled!";

///////////////////////////////////
function clickIE4(){
if (event.button==2){
alert(message);
return false;
}
}

function clickNS4(e){
if (document.layers||document.getElementById&&!document.all){
if (e.which==2||e.which==3){
alert(message);
return false;
}
}
}

if (document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById){
document.onmousedown=clickIE4;
}

// document.oncontextmenu=new Function("alert(message);return false")

// **********************************************************************


// ***********************************
// Function to show the next table row
// Arguments: target <tr> id,
// Usage: onChange = doRow('row_x')
// Note: make sure that <tr> of rows 2 and more are set to hide beforehand
// eg: <tr id="row_2" style="display:none;"></tr>
// Sample: content/quotation_new_2_show.php
function doRow(f_row_id)
	{
	// Check if there is such a row_id in the page,
	// so that the function will not run at the last row_id
	if ( eval("document.getElementById(\"" + f_row_id + "\")") )
		{
    currentDisplay = eval("document.getElementById(\"" + f_row_id + "\").style.display");

    if (currentDisplay != '')
      {
      // Show
      eval("document.getElementById(\"" + f_row_id + "\").style.display = ''");
      }
    else
      {
      // Hide
      eval("document.getElementById(\"" + f_row_id + "\").style.display = 'none'");
      }
    
		}
	}
// end function doRow()
// ********************


// **********************************************************************
// Function to fetch matching names from the dropdown menu "list"
function filterList(pattern, list)
  {
  /*
  if the dropdown list passed in hasn't
  already been backed up, we'll do that now
  */
  if (!list.bak)
    {
    /*
    We're going to attach an array to the select object
    where we'll keep a backup of the original dropdown list
    */
    list.bak = new Array();
    for (n=0;n<list.length;n++)
      {
      list.bak[list.bak.length] = new Array(list[n].value, list[n].text);
      }
    } // End if (!list.bak)

  /*
  We're going to iterate through the backed up dropdown
  list. If an item matches, it is added to the list of
  matches. If not, then it is added to the list of non matches.
  */
  match = new Array();
  nomatch = new Array();
  for (n=0;n<list.bak.length;n++)
    {
    if(list.bak[n][1].toLowerCase().indexOf(pattern.toLowerCase())!=-1)
      {
      match[match.length] = new Array(list.bak[n][0], list.bak[n][1]);
      }
    else
      {
      nomatch[nomatch.length] = new Array(list.bak[n][0], list.bak[n][1]);
      }
    }

  /*
  Now we completely rewrite the dropdown list.
  First we write in the matches, then we write
  in the non matches
  */
  for (n=0;n<match.length;n++)
    {
    list[n].value = match[n][0];
    list[n].text = match[n][1];
    }
  for (n=0;n<nomatch.length;n++)
    {
    list[n+match.length].value = nomatch[n][0];
    list[n+match.length].text = nomatch[n][1];
    }

  /*
  Finally, we make the 1st item selected - this
  makes sure that the matching options are
  immediately apparent
  */
  list.selectedIndex=0;
  }
// End function filterList()
// **************************************************

