﻿function stringCheck(form, name, required, max, prettyName)
{
  var v = getValue(form,name);
  if ( !required && v == "" )
  {
      return true;
  }
  if ( required && v == "" )
  {
      alert("Please a enter value for " + prettyName);
      form[name].focus();
      return false;
  }
  if ( v.length > max )
  { 
      alert("Maximum value for " + prettyName + " is " + max + " characters.");
      form[name].focus();
      return false;
  }
  return true;
}

function checkEmail(form, name, required, max, prettyName)
{
  var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
  var v = getValue(form,name);
  if ( !required && v == "" )
  {
      return true;
  }
  //stringCheck makes sure if required then it's there
  if ( !stringCheck(form,name, required, max, prettyName))
  {
     return false;
  }
  if (!filter.test(v))
  {
       alert(v + " does not appear to be a valid email address.")
       form[name].focus();
       return false;
  }
  return true;
}
/*use null for length if don't want it checked*/
function checkPhone(form, name, required, length, prettyName)
{
  var v = getValue(form,name);
  //if we dont need one...
  if ( !required && v == "" )
  {
      return true;
  }
  //stringCheck makes sure if required then it's there
  if ( !stringCheck(form,name, required, 50, prettyName))//we assume numbers not bigger than 50
  {
     return false;
  }
  
  var stripped = v.replace(/[\(\)\.\-\ ]/g, '');
  //strip out acceptable non-numeric characters
  if (isNaN(parseInt(stripped))) 
  {
      alert(prettyName + " appears to contain invalid characters.");
      form[name].focus();
      return false;
  }
  if ( length!=null )
  {
      if ( stripped.length<length )
      {
      	alert("The " + prettyName + " is not long enough.  Is the area code missing?");
        form[name].focus();
        return false;
      }
  }
  return true;
}

/*use null for length if don't want it checked*/
function checkZip(form, name, required, length, prettyName)
{
  var v = getValue(form,name);
  //if we dont need one...
  if ( !required && v == "" )
  {
      return true;
  }
  //stringCheck makes sure if required then it's there
  if ( !stringCheck(form,name, required, 15, prettyName))//we assume numbers not bigger than 50
  {
     return false;
  }
  
  var stripped = v.replace(/[\(\)\.\-\ ]/g, '');
  //strip out acceptable non-numeric characters
  if (isNaN(parseInt(stripped))) 
  {
      alert(prettyName + " appears to contain invalid characters.");
      form[name].focus();
      return false;
  }
  if ( length!=null )
  {
      if ( stripped.length<length )
      {
      	alert("The " + prettyName + " is not long enough.");
        form[name].focus();
        return false;
      }
  }
  return true;
}

function checkDropdown(form, name, prettyName) {
    
    if (form[name].selectedIndex == 0) 
    {
      	alert("Please make selection for " + prettyName);
        form[name].focus();
        return false;
    }    
    return true;
} 

function checkRadio(form, name, prettyName)
{
    checked = false;
    for ( var j = 0; j< form[name].length; j++)
    {
        if (form[name][j].checked)
            checked = true;
    }
    if ( checked == false )
    {
      	alert("Please make selection for " + prettyName);
        //form[name].focus();
        return false;
    }
    return true;
}

function checkLength(form, name, max, prettyName)
{
  if ( form[name].value.length > max )
  {
      alert(prettyName + " can contain at most " + max + " characters.");
      return false;
  }
  return true;
}

function getValue(form, name)
{
    var v = form[name].value;
    if ( v==null )
    {
      alert(name + " does not appear to be form variable.  See admin.");
      return v;
    }
    else
      return trim(v);
}

function trim(v)
{
  return v.replace(/^\s*|\s*$/g,"");
}

// JScript File

function createXMLHttpRequest(){

    try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {}

    try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}

    try { return new XMLHttpRequest(); } catch(e) {}

    return null;//check for it

}

// ====================================================================
//       URLEncode and URLDecode functions
//
// Copyright Albion Research Ltd. 2002
// http://www.albionresearch.com/
//
// You may copy these functions providing that 
// (a) you leave this copyright notice intact, and 
// (b) if you use these functions on a publicly accessible
//     web site you include a credit somewhere on the web site 
//     with a link back to http://www.albionresarch.com/
//
// If you find or fix any bugs, please let us know at albionresearch.com
//
// SpecialThanks to Neelesh Thakur for being the first to
// report a bug in URLDecode() - now fixed 2003-02-19.
// ====================================================================
function URLEncode(ss)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = ss;// document.URLForm.F1.value;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	//document.URLForm.F2.value = encoded;
	//return false;
	return encoded;
};



