var goodEmailRegex = /^([^@ \f\n\r\t\v]+@)(([a-z0-9]+(-*[a-z0-9])*)+(\.([a-z]+(-*[a-z0-9])*)+)*)(\.[a-z]{2,4})$/gi;
var goodPostalcodeRegex = /^[a-z]\d[a-z]\s*\d[a-z]\d$/gi;
var goodPhoneNumRegex = /^\d{3}(\s*|-)?\d{4}$/g;
var goodAreaCodeRegex = /^\d{3}$/g;
var goodPhoneRegex = /^(\(?\d{3}\)?)\s*-?\s*\d{3}\s*-?\s*\d{4}$/g;

var resetRolls = new Object(); /* Used for Reset button script */

function getValue(field)
{
  switch(field.type)
   {
      case "text" :
      case "textarea" :
      case "password" :
      case "hidden" :
         return field.value;

      case "select-one" :
         var i = field.selectedIndex;
         if (i == -1)   return "";
         else   return (field.options[i].value == "") ? field.options[i].text : field.options[i].value;

      case "select-multiple" :
         var allChecked = new Array();
         for(i = 0; i < field.options.length; i++)
            if(field.options[i].selected)
               allChecked[allChecked.length] = (field.options[i].value == "") ? field.options[i].text : field.options[i].value;
         return allChecked;

      case "button" :
      case "reset" :
      case "submit" :
         return "";

      case "radio" :
      case "checkbox" :
         if (field.checked) { return field.value; } else { return ""; }
      default :
         if(field[0].type == "radio")
         {
            for (i = 0; i < field.length; i++)
               if (field[i].checked)
                  return field[i].value;

            return "";
         }
         else if(field[0].type == "checkbox")
         {
            var allChecked = new Array();
            for(i = 0; i < field.length; i++)
               if(field[i].checked)
                  allChecked[allChecked.length] = field[i].value;

            return allChecked;
         }
         else
            var str = "";
            for (x in field) { str += x + "\n"; }
            alert("I couldn't figure out what type this field is...\n\n" + field.name + ": ???\n\n\n" + str + "\n\nlength = " + field.length);
         break;
   }
   
   return "";
}

function getRadioValue(field)
{
  if (!field.length) {
    //field = field.form.elements[field.name];
    field = getFields(field.name, field.form);
  }

  if (!field.length) {
    if (field.checked) return field.value;
    else return null;
  }

  for (var i=0; i<field.length; i++) {
    if (field[i].checked) {
      return field[i].value;
    }
  }

  return null;
}

function getCheckboxValue(field)
{
  if (!field.length) {
    //field = field.form.elements[field.name];
    field = getFields(field.name, field.form);
  }

  if (!field.length) {
    if (field.checked) return field.value;
    else return null;
  }

  var val = ";";
  for (var i=0; i<field.length; i++) {
    if (field[i].checked) {
      val += field[i].value + ";";
    }
  }

  if (val == ";") return null;
  else return val;
}

function getSelectoneValue(field)
{
  return field.options[field.selectedIndex].value;
}

function getSelectoneText(field)
{
  return field.options[field.selectedIndex].text;
}

function getSelectmultiValue(field)
{
  var myOptions = field.options;
  var val = ";";
  for (var i=0; i<myOptions.length; i++) {
    if (myOptions[i].selected) {
      val += myOptions[i].value + ";";
    }
  }

  if (val == ";") return null;
  else return val;
}

function getFields(name, form)
{
  var fields = new Array();
  var e = form.elements;
  for (var i=0; i<e.length; i++) {
    if (e[i].name == name) fields[fields.length] = e[i];
  }

  return fields;
}

function getCookie(cookieName) {
  cookieName = cookieName + "=";
  var cookies = document.cookie.split(";");
  for (var i=0; i<cookies.length; i++) {
    if (startsWith(cookies[i], cookieName)) {
      return cookies[i].substring((cookieName).length);
    }
  }
  return null;
}

function isGoodEmail(field)
{
  field.value = parseChar(field.value, " ");

  var em = field.value;

  if (em == "") return false;

  if (em.indexOf(">") != -1 ||
      em.indexOf(",") != -1 ||
      em.indexOf("<") != -1 ||
      em.indexOf(":") != -1 ||
      em.indexOf(";") != -1 ||
      //em.indexOf("'") != -1 ||
      em.indexOf('"') != -1 ||
      em.indexOf("/") != -1 ||
      em.indexOf("?") != -1) {
    return false;
  }

  var arr = field.value.split("@");
  if (arr.length != 2 || arr[0].length == 0) {
    return false;
  }

  arr = arr[1].split(".");
  if (arr.length < 2) {
    return false;
  }

  var index = 0;
  for (; index<arr.length-1; index++) {
    if (arr[index].length == 0) {
      return false;
    }
  }

  if (arr[index].length < 2 || arr[index].length > 4) {
    return false;
  }

  return true;
}

function checkDuplicate(me, myList)
{
  var myValue = me.options[me.selectedIndex].value;

  if (duplicateCheckField != null && duplicateCheckField != me) {
    duplicateCheckField.focus();
    return;
  }

  var other, otherValue;
  var e = me.form.elements;
  for (var i=0; i<myList.length; i++) {
    other = e[myList[i]];
    if (other == me) continue;

    otherValue = other.options[other.selectedIndex].value;
    if (otherValue == "") continue;

    if (myValue == otherValue) {
      duplicateCheckField = me;
      alert("Please specify a different choice than others.");
      me.focus();
      return;
    }
  }
  duplicateCheckField = null;
}

function trim(s)
{
  for (var i=0; i<s.length && s.charAt(i)==' '; i++);
  for (var j=s.length; j>0 && s.charAt(j-1)==' '; j--);
  if (i > j-1) return "";
  return s.substring(i,j);
}

function parseChar(code, chars)
{
  var newcode = "";

  for (var i=0; i<code.length; i++) {
    if (chars.indexOf(code.charAt(i)) == -1) newcode += code.charAt(i)
  }

  return newcode;
}

function startsWith(str, str0)
{
  return 0 == str.indexOf(str0);
}

function endsWith(str, str0)
{
  return -1 != str.indexOf(str0) && str.length == str.indexOf(str0) + str0.length;
}

function checkInt(field)
{
  var val = trim(field.value);
  field.value = val;

  if (new Number(parseInt(val, 10)).toString() != val) {
    alert("Please provide a valid integer");
    field.focus();
    field.select();
    return false;
  } else {
    return true;
  }
}

function checkPosInt(field)
{
  var isInt = checkInt(field);
  if (isInt && field.value <= 0) {
    alert("Please provide a valid positive integer");
    field.focus();
    field.select();
    return false;
  } else {
    return true;
  }
}

function checkNonNegInt(field)
{
  var isInt = checkInt(field);
  if (isInt && field.value < 0) {
    alert("Please provide a valid non-negative integer");
    field.focus();
    field.select();
    return false;
  } else {
    return true;
  }
}

function isDOB_Year(field)
{
  if (checkInt(field)) {

    var yr = parseChar(field.value, ".");
    if (yr.length == 2) yr = "19" + yr;
    field.value = yr;

    if (document.all) {
      year = new Date().getYear();
    } else {
      year = new Date().getYear() + 1900;
    }

    if (yr > year || yr < 1850) {
      alert("Please provide a valid year of your birthday in the form of \"XXXX\"!");
      field.focus();
      field.select();
      return false;
    } else {
      return true;
    }
  }
}

function isDOB_Month(field)
{
  var mon = trim(field.value);
  mon = parseChar(mon, ".");
  var posInt = !isNaN(mon) && parseInt(mon, 10) == mon && mon > 0;
  if (mon.length == 1) mon = "0" + mon;
  field.value = mon;
  if (mon.length != 2 || !posInt || mon > 12) {
    alert("Please provide a valid month of your birthday in the form of \"XX\"!");
    field.focus();
    field.select();
    return false;
  } else {
    return true;
  }
}

function isDOB_Day(field)
{
  var date = trim(field.value);
  date = parseChar(date, ".");
  var posInt = !isNaN(date) && parseInt(date, 10) == date && date > 0;
  if (date.length == 1) date = "0" + date;
  field.value = date;
  if (date.length != 2 || !posInt || date > 31) {
    alert("Please provide a valid date of your birthday in the form of \"XX\"!");
    field.focus();
    field.select();
    return false;
  } else {
    return true;
  }
}

/* Script used to replace reset button with an image */
function resetimage(src)
{
this.src=src;
this.confirm=true;
this.alt="Reset";
this.write=resetimage_write;
}

function resetimage_write()
{
document.write('<A ');
if (this.rollover)
    {
    if (! this.name)
        {
        alert('to create a rollover you must give the image a name');
        return;
        }

    resetRolls[this.name] = new Object();
    resetRolls[this.name].over = new Image();
    resetRolls[this.name].over.src=this.rollover;
    resetRolls[this.name].out = new Image();
    resetRolls[this.name].out.src=this.src;
    document.write(
        ' onMouseOver="if (document.images)document.images[\'' + 
        this.name + '\'].src=resetRolls[\'' + this.name + '\'].over.src"' + 
        ' onMouseOut="if (document.images)document.images[\'' + 
        this.name + '\'].src=resetRolls[\'' + this.name + '\'].out.src"'
        );
    }
document.write(' HREF="javascript:');
if (this.confirm)
    document.write('if(confirm(\'Are you sure you want to reset?\'))');
document.write(
    'document.forms[' + 
    (document.forms.length - 1) + '].reset();void(0);">');
document.write('<IMG SRC="' + this.src + '" ALT="' + this.alt + '"');
document.write(' BORDER=0');
if (this.name)document.write(' NAME="' + this.name + '"');
if (this.height)document.write(' HEIGHT=' + this.height);
if (this.width)document.write(' WIDTH=' + this.width);
if (this.otheratts)document.write(' '+ this.otheratts);
document.write('></A>');
}

// NAVIGATION -----------------------------------------------------------------------------------

function RemoveContent(d) {
document.getElementById(d).style.display = "none";
}

function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'none')
e.style.display = '';
else
e.style.display = 'none';
}

// GENERAL SCRIPTS -----------------------------------------------------------------------------------

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.01
  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 && d.getElementById) x=d.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];}
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
function checkEmail(field)
{
  field.value = parseChar(field.value, " ");

  var em = field.value;

  if (em == "") return false;

  if (em.indexOf(">") != -1 ||
      em.indexOf(",") != -1 ||
      em.indexOf("<") != -1 ||
      em.indexOf(":") != -1 ||
      em.indexOf(";") != -1 ||
      //em.indexOf("'") != -1 ||
      em.indexOf('"') != -1 ||
      em.indexOf("/") != -1 ||
      em.indexOf("?") != -1) {
    printAlert(field);
    return false;
  }

  var arr = field.value.split("@");
  if (arr.length != 2 || arr[0].length == 0) {
    printAlert(field);
    return false;
  }

  arr = arr[1].split(".");
  if (arr.length < 2) {
    printAlert(field);
    return false;
  }

  var index = 0;
  for (; index<arr.length-1; index++) {
    if (arr[index].length == 0) {
      printAlert(field);
      return false;
    }
  }

  if (arr[index].length < 2 || arr[index].length > 4) {
    printAlert(field);
    return false;
  }

  return true;
}
function checkBrokerList(isclient,selected_repid)
{
  isclient.value = getValue(isclient);
  selected_repid.value = getValue(selected_repid);

  if (isclient.value == 1 && selected_repid.value == 0) {
  alert("If you have an account with MF Global, please select your broker's name."); 
  return false;
  }
  
  return true;
}

function printAlert(field)
{
  alert("Please provide a valid email address!");
  field.focus();
  field.select();
}