function createFriend(number, frm) {
    var fset = document.createElement("fieldset");
    fset.className = "fancy refAFriend cnf";
    
        var div = document.createElement("div");
        div.className = "inner cnf";
        
            var label = document.createElement("label");
            label.innerHTML = "Friend "+number;
        div.appendChild(label);
        
            var dl = document.createElement("dl");
            dl.className = "cnf";
            
                var dt = document.createElement("dt");
                dt.innerHTML = "Name";
            dl.appendChild(dt);
                
                var dd = document.createElement("dd");
                    var input = createNamedElement("input", "friendName"+number);
                    input.className = "txt";
                    input.name = "friendName"+number;
                    input.setAttribute("type", "text");
                    input.onfocus = function() {
                        if (this.name == 'friendName'+__FCOUNT__ ) {
                            addFriend(frm);
                        }
                    }
                dd.appendChild(input);
            dl.appendChild(dd);
            
                dt = document.createElement("dt");
                dt.innerHTML = "Email";
            dl.appendChild(dt);
                
                dd = document.createElement("dd");
                    var input = createNamedElement("input", "friendEmail"+number);
                    input.className = "txt";
                    input.setAttribute("type", "text");
                dd.appendChild(input);
                
                    var inputCaption = createNamedElement("input", "CAPTIONfriendEmail"+number);
                    inputCaption.value = "Friend " + number + " Email";
                    inputCaption.setAttribute("type", "hidden");
                dd.appendChild(inputCaption);
                
                    var inputValidator = createNamedElement("input", "VALIDATORfriendEmail"+number);
                    inputValidator.name = "VALIDATORfriendEmail"+number;
                    inputValidator.value = "TEXT_NO|EMAIL";
                    inputValidator.setAttribute("type", "hidden");
                dd.appendChild(inputValidator);
                
            dl.appendChild(dd);
            
        div.appendChild(dl);
    
    fset.appendChild(div);
    
    applyFMCorners(fset, 'images/corners_forms.png', 598, 10, {});
    
    return fset;
    
}


function createEmailField(number, frm) {
    var fset = document.createElement("fieldset");
    fset.className = "fancy refAFriend cnf";
    
        var div = document.createElement("div");
        div.className = "inner cnf";
        
            var label = document.createElement("label");
            label.innerHTML = "Email Address ";
        div.appendChild(label);
                   
            var input = createNamedElement("input", "emailAddresses");
            input.className = "txt";
            input.name = "emailAddresses";
            input.setAttribute("type", "text");
            input.onfocus = function() {
                if (this.name == 'emailAddresses') {
                    addEmail(frm);
                }
            }
        div.appendChild(input);

    fset.appendChild(div);
    
    applyFMCorners(fset, 'images/corners_forms.png', 598, 10, {});
    
    return fset;
}

function addEmail(frm) {
    __FCOUNT__ ++;
    document.getElementById("emails").appendChild(createEmailField(__FCOUNT__, frm));
    //frm.referCount.value = __FCOUNT__;
}


function createNamedElement(type, name) {
  var element = null;
  // Try the IE way; this fails on standards-compliant browsers
  try {
    element = document.createElement('<'+type+' name="'+name+'">');
  } catch (e) {
  }
  if (!element || element.nodeName != type.toUpperCase()) {
    // Non-IE browser; use canonical method to create named element
    element = document.createElement(type);
    element.name = name;
  }
  return element;
}

var __FCOUNT__ = 0;

function addFriend(frm) {
    __FCOUNT__ ++;
    document.getElementById("referals").appendChild(createFriend(__FCOUNT__, frm));
    frm.referCount.value = __FCOUNT__;
}

function registrationValidate(frm) {  
  if (frm.email.value != frm.email2.value) {
    alert('You must enter the following fields correctly before proceeding:\n\nYour supplied email addresses do not match.');
    return false;
  }
  
  if (frm.toteId.value.length < 3) {
    //alert('You must enter the following fields correctly before proceeding:\n\nBetting Account # must be at least 3 digits long.');
    //return false;
  }
  
  return validateForm(frm);
}

function updateValidate(frm) {  
  if (frm.email.value != frm.email2.value) {
    alert('You must enter the following fields correctly before proceeding:\n\nYour supplied email addresses do not match.');
    return false;
  }
  
  if (frm.password.value != frm.password2.value) {
    alert('You must enter the following fields correctly before proceeding:\n\nYour supplied passwords do not match.');
    return false;
  }
  
  return validateForm(frm);
}

function fireRoundMarking() {
  if (confirm('You are about to trigger the round marking process, have you have set the correct answer for each question?')) {
    document.getElementById('hideWhenMarking').style.display = 'none';
    if (document.getElementById('hideWhenEmailing') != null) {
      document.getElementById('hideWhenEmailing').style.display = 'none';
    }
    document.getElementById('showWhenMarking').style.display = 'block';
    return true;
  } else {
    return false;
  }
}

function fireBulkEmail(frm) {
  if (confirm('You are about to send a large number of emails, have you checked the content that is going to be sent?')) {
    /*
    document.getElementById('hideWhenEmailing').style.display = 'none';
    if (document.getElementById('hideWhenMarking') != null) {
      document.getElementById('hideWhenMarking').style.display = 'none';
    }
    document.getElementById('showWhenEmailing').style.display = 'block';
    */
    frm.submit();
    return true;
  } else {
    return false;
  }
}

function adminEditUserValidate(frm) {  
  if (frm.username.value != '' && frm.username.value != frm.username2.value) {
    alert('You must enter the following fields correctly before proceeding:\n\nYour supplied usernames do not match.');
    return false;
  }
  
  if (frm.password.value != '' && frm.password.value != frm.password.value) {
    alert('You must enter the following fields correctly before proceeding:\n\nYour supplied passwords do not match.');
    return false;
  }
  
  if (frm.email.value != '' && frm.email.value != frm.email2.value) {
    alert('You must enter the following fields correctly before proceeding:\n\nYour supplied email addresses do not match.');
    return false;
  }
  
  if (frm.toteId.value != '' && frm.toteId.value != frm.toteId2.value) {
    alert('You must enter the following fields correctly before proceeding:\n\nYour supplied tote ID#s do not match.');
    return false;
  }
  
  if (frm.phone.value != '' && frm.phone.value != frm.phone2.value) {
    alert('You must enter the following fields correctly before proceeding:\n\nYour supplied phone numbers do not match.');
    return false;
  }
  
  if (frm.toteId.value != '' && frm.toteId.value.length < 3) {
    alert('You must enter the following fields correctly before proceeding:\n\nBetting Account # must be at least 3 digits long.');
    return false;
  }
  
  return validateForm(frm);
}
