
/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}

 function isNumeric(str)
// returns true if str is numeric
// that is it contains only the digits 0-9
// returns false otherwise
// returns false if empty
{
  var len= str.length;
  if (len==0)
    return false;
  //else
  var p=0;
  var ok= true;
  var ch= "";
  while (ok && p<len)
  {
    ch= str.charAt(p);
    if (('0'<=ch && ch<='9')||ch=="."||ch=="-")
      p++;
    else
      ok= false;
  }
  return ok;
}

/*
===================================================================
formatFilter(form, format, len, name)

form 	= document.formName.elementName
format 	= Use '#' to represent numbers, any char for separators. (EG. (###)###-#### or ###-##-#### )
len		= number of DIGITS that are required (Do not count separators or grouping symbols)
name	= The 'name' of the element. This is for human eyes ... not the ELEMENT name.

Recommended usage:
<input type="text" name="PHONE_NUM" onBlur="javascript:formatFilter(this, '(###)###-####', 10, Phone Number);">
===================================================================
*/
function formatFilter(form, format, len, name) {

	var input = form.value;

	if(input.length > 0) { //do not perform if empty input

		var numbers = ""; //store all the numbers here

		//process to remove non-numbers and spaces
		for(var i = 0; i < input.length; i++) {
			var c_char = input.charAt(i);
			if(!(isNaN(c_char) || c_char == " ")) numbers += c_char;
		}

		if (numbers.length < len)
		{
			alert(name+" must be "+len+" digits");
			form.focus();
		}
		
		var output = ""; //assign numbers here

		//assign numbers to chosen format
		var n = 0, i = 0;
		while(i < format.length && n < numbers.length) {
			var c_char = format.charAt(i);
			if(c_char == "#") {
				output += numbers.charAt(n++)
			} else {
				output += c_char;
			}
			i++;
		}

		form.value = output; //output to form
	}
}

/*
===================================================================
open_popup(path)

path	= the virtual path and filename of the file to pop

Recommended usage:
<input type="text" name="PHONE_NUM" onBlur="javascript:open_popup("/directory/direc/page.html");">
===================================================================
*/
			
function open_popup(path, hgt, wdth)
{
	var opWinHandle;
	//alert( path);
	//opWinHandle = window.open(path,"op_popup","height=500,width=450,status=no,toolbar=no,scrollbars=yes,resizable=no,menubar=no");
	//opWinHandle.focus();
	argArray = new Array(path, window)
	mdRetVal = window.showModalDialog("/formbox/pops/modal_frame.php",argArray,"dialogHeight: "+hgt+"px; dialogWidth: "+wdth+"px; dialogTop: 150px; dialogLeft: 150px; edge: Raised; center: Yes; help: No; resizable: Yes; status: No;");
	if (!isNaN(mdRetVal))
	{
		window.location.reload();
	}
	
}

/*--------Cross browser modals---------------*/
var winModalWindow
 
function IgnoreEvents(e)
{
  return false;
}
 
function ShowWindow(w_path, w_width, w_height)
{
  /*
  if (window.showModalDialog)
  {
  	w_width = parseInt(w_width)+10;
	w_height = parseInt(w_height)+25;
  	argArray = new Array(w_path, window);
    window.showModalDialog("/console/popper/modal_frame.php",argArray,
    "dialogWidth="+w_width+"px;dialogHeight="+w_height+"px; dialogTop: 150px; dialogLeft: 150px; edge: Raised; center: Yes; help: No; resizable: Yes; status: No; scroll: No;");
  }
  else
  {
 */
 	alert('helo');
	window.top.captureEvents (Event.CLICK|Event.FOCUS)
    window.top.onclick=IgnoreEvents
    window.top.onfocus=HandleFocus 
    winModalWindow = window.open (w_path,"ModalChild","height="+w_height+",width="+w_width+",left=150, top=150, status=no,toolbar=yes,scrollbars=yes,menubar=no");
    winModalWindow.focus();
  //}
}
function addToCart(id){
	document.product_add.product_id.value=id;
	document.product_add.submit();
}
 
function HandleFocus()
{
  if (winModalWindow)
  {
    if (!winModalWindow.closed)
    {
      winModalWindow.focus();
    }
    else
    {
      window.top.releaseEvents (Event.CLICK|Event.FOCUS);
      window.top.onclick = "";
    }
  }
  return false
} 

function validatePwd(fieldname) {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Copyright April 2004 Sani, A. I. (MCSE, MCSA, CCNA)
//sanijean@yahoo.co.uk
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
 
//Initialise variables
var errorMsg = "";
var space  = " ";

fieldvalue  = fieldname.value; 
fieldlength = fieldvalue.length; 
 
//It must not contain a space
if (fieldvalue.indexOf(space) > -1) {
     errorMsg += "\nPasswords cannot include a space.\n";
}     
 
//It must contain at least one number character
if (!(fieldvalue.match(/\d/))) {
     errorMsg += "\nStrong passwords must include at least one number.\n";
}
//It must start with at least one letter     
if (!(fieldvalue.match(/^[a-zA-Z]+/))) {
     errorMsg += "\nStrong passwords must start with at least one letter.\n";
}
//It must contain at least one upper case character     
if (!(fieldvalue.match(/[A-Z]/))) {
     errorMsg += "\nStrong passwords must include at least one uppercase letter.\n";
}
//It must contain at least one lower case character
if (!(fieldvalue.match(/[a-z]/))) {
     errorMsg += "\nStrong passwords must include one or more lowercase letters.\n";
}
//It must contain at least one special character
//if (!(fieldvalue.match(/\W+/))) {
if(false){
     errorMsg += "\nStrong passwords must include at least one special character - #,@,%,!\n";
}
//It must be at least 7 characters long.
if (!(fieldlength >= 7)) {
     errorMsg += "\nStrong passwords must be at least 7 characters long.\n";
}
//If there is aproblem with the form then display an error
     if (errorMsg != ""){
          msg = "______________________________________________________\n\n";
          msg += "Please correct the problem(s) with your password and try it again.\n";
          msg += "______________________________________________________\n";
          errorMsg += alert(msg + errorMsg + "\n\n");
          fieldname.focus();
          return false;
     }
     
     return true;
}