/*Generic pop up code - please use this only unless you really need something different*/
function popUpPage(url, parameters, name)
{
	var day = new Date();
	var pageName = name ? name : day.getTime()

	eval("MFG"+pageName+" = window.open('"+url+"','"+pageName+"','"+parameters+"')");

	if (eval("MFG"+pageName) && window.focus) eval("MFG"+pageName).focus();
}


function getPlatform()
{
	var myUserAgent;
	myUserAgent = navigator.userAgent.toLowerCase();

	if ((myUserAgent.indexOf("win") != -1) ||  (myUserAgent.indexOf("16bit") != -1))
	{
		return "win";
	}
	
	if (myUserAgent.indexOf("mac") != -1)
	{
		return "mac";
	}  
	
	if (myUserAgent.indexOf("x11") != -1)
	{
		return "unx";
	}  
	
	return "other";
}

function getBrowserType()
{
	var myUserAgent;

	var myMajor;
	myUserAgent= navigator.userAgent.toLowerCase();
	myMajor= parseInt(navigator.appVersion);
	if( (myUserAgent.indexOf('mozilla')!= -1) &&(myUserAgent.indexOf('spoofer')== -1) &&(myUserAgent.indexOf('compatible') == -1) &&(myUserAgent.indexOf('opera') == -1) &&(myUserAgent.indexOf('webtv')  == -1) )
	{  
		if (myMajor > 3)
		{
			return "nav4";
		}
	
		return "nav";
	}
	
	if (myUserAgent.indexOf("msie")!= -1)
	{  
	
		if (myMajor > 3)
		{
			return "ie4";
  		} 
	
		return "ie";
	}
// dom compliant browsers are allowed
	if(document.body.firstChild) return "domCompliant";
	return "other";
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function openwindow(URL)
{
var how="toolbar=no,location=no,directories=no,menubar=no,status=no,scrollbars=yes,resizable=yes,height=600,width=600,top=10,left=10'";
aWin=window.open("","aWin",how);    
aWin.location=URL;
aWin.focus();
}

function hide(divId)
    {
    if (document.layers)
         { document.layers[divId].visibility = 'hide'; }
         
    else if (document.all)
         { document.all[divId].style.visibility = 'hidden'; }
         
    else if (document.getElementById)
         { document.getElementById(divId).style.visibility = 'hidden'; }
    }

//shows corresponding div
    
function show(divId)
    {
    if (document.layers)
         { document.layers[divId].visibility = 'show'; }
         
    else if (document.all)
         { document.all[divId].style.visibility = 'visible'; }
         
    else if (document.getElementById)
         { document.getElementById(divId).style.visibility = 'visible'; }
    } 

//Validation Rules
function isEmpty(string_to_check, error_div_id) { 

	if (string_to_check.length == 0)
	{
		//Show error message
		show(error_div_id)
		return false;
	}
	else
	{
		//Hide error message
		hide(error_div_id)
		return true;
	}
} 

function isChecked(check_box, error_div_id) {
	if (check_box.checked == false) {
		//Show error message
		show(error_div_id)
		return false;
	} else {
		//Hide error message
		hide(error_div_id)
		return true;
	}
}

   function validateInteger(str){
      str = strip(' \n\r\t',str);
      //remove leading zeros, if any
      while(str.length > 1 && str.substring(0,1) == '0'){
         str = str.substring(1,str.length);
      }
      var val = parseInt(str);
      if(isNaN(val))
         return false;
      else
         return true;
   }
   
   function validateFloat(str){
      str = strip(' \n\r\t',str);
      //remove leading zeros, if any
      while(str.length > 1 && str.substring(0,1) == '0'){
         str = str.substring(1,str.length);
      }
      var val = parseFloat(str);
      if(isNaN(val))
         return false;
      else
         return true;
   }

  function validateDate(str){
      var dateVar = new Date(str);
      if(isNaN(dateVar.valueOf()) || 
         (dateVar.valueOf() ==0))
         return false;
      else
         return true;
   }
   
function validateEMail(str){
      str = strip(" \n\r\t",str);
      if(str.indexOf("@") > -1 && str.indexOf(".") > -1)
         return true;
      else
         return false;
   }
   
   //End Validation Rules

//Formatting functions
   function formatPhone(str){
      str = strip("*() -./_\n\r\t\\",str);
      if(str.length==10)
         return reformat(str,"(",3,") ",3,"-",4);
      if(str.length==7)
         return reformat(str,"",3,"-",4);
   }
function formatDate(str,style){
      var dateVar = new Date(str);
      var year = dateVar.getYear();
      if(year<10)
         year += 2000;
      if(year<100)
         year += 1900;
      switch(style){
         case "MM/DD/YY":
            return (dateVar.getMonth() + 1) + "/" + 
              dateVar.getDate() + "/" + year;
            break;
         case "DD/MM/YY":
            return dateVar.getDate() + "/" + 
              (dateVar.getMonth() + 1) + "/" + year;
            break;
         case "Month Day, Year":
            return getMonthName(dateVar) + " " + 
              dateVar.getDate() + ", " + year;
            break;
         case "Day, Month Day, Year":
            return getDayName(dateVar) + ", " + 
              getMonthName(dateVar) + " " + 
              dateVar.getDate() + ", " + year;
            break;
         default:
            return (dateVar.getMonth() + 1) + "/" + 
              dateVar.getDate() + "/" + year;
            break;
      }
   }

function checkPostCode (toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkstuw]";                                // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
  
  // Array holds the regular expressions for the valid postcodes
  var pcexp = new Array ();

  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Expression for postcodes: ANA NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

  // Expression for postcodes: AANA  NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);
  
  // Standard BFPO numbers
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
  
  // c/o BFPO numbers
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);
  
  // Overseas Territories
  pcexp.push (/^([A-Z]{4})(\s*)(1ZZ)$/i);

  // Load up the string to check
  var postCode = toCheck;

  // Assume we're not going to find a valid postcode
  var valid = false;
  
  // Check the string against the types of post codes
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      // The post code is valid - split the post code into component parts
      pcexp[i].exec(postCode);
      
      // Copy it back into the original string, converting it to uppercase and
      // inserting a space between the inward and outward codes
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      // If it is a BFPO c/o type postcode, tidy up the "c/o" part
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      
      // Load new postcode back into the form element
      valid = true;
      
      // Remember that we have found that the code is valid and break from loop
      break;
    }
  }
  
  // Return with either the reformatted valid postcode or the original invalid 
  // postcode
  if (valid) {return postCode;} else return false;
}
   //End Formatting Functions
