window.onload=alertSize;

function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }


  var content = document.getElementById('content');
  var dashboard = document.getElementById('dashboard');
  var dashboardright = document.getElementById('dashboardright');
  var mypage = document.getElementById('menumypage');
  var menujoin = document.getElementById('menujoin');  
  var x = dashboard.offsetLeft+125;
  //window.alert("offsets: " + x + " " +content.offsetLeft);
  if (typeof(dashboard)=="undefined") {}
  else {
    if (content.offsetLeft > x) {
      dashboard.style.display = "inline";
      dashboardright.style.display = "inline";
      /*
      if (typeof(mypage) != "undefined") {
        mypage.style.display="none";        
      }
      */
    }
  }
}

function loadXMLDoc(url)
{
  if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  }
  else
  {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.open("GET",url,false);
  xmlhttp.send(null);
  document.getElementById('test').innerHTML=xmlhttp.responseText;
}

function checkNewMemberForm(theForm)
{
    var reason = "";
    reason += validateDisplayName(theForm.displayname);
    reason += validateEmail(theForm.email);
    reason += validatePassword(theForm.password);    
    reason += validatePassword2(theForm.password2);
    reason += validatePrimaryJob(theForm.primaryjob);
    reason += validateTerms(theForm.terms);
    //reason += validateParent(theForm.parent);
    reason += validateZip(theForm.zip);
    if (reason != "")
    {
        alert("Fields need correction: \n" + reason);
        return false;
    }
    return true;
}

function changeSortOrder(imageid) {
    window.location.reload(true);
}
function searchSubject() {
  var subject=document.getElementById("subject").value;
  
  if (subject == 2) {
    document.getElementById("modelSearch").style.display = "inline";  
  }
  else {
    document.getElementById("modelSearch").style.display = "none";     
  }
}
function checkUserUpdate(theForm)
{
    var reason = "";
    reason += validateDisplayName(theForm.displayname);    
    reason += validateEmail(theForm.email);
    reason += validatePassword(theForm.password);    
    
    if (reason != "")
    {
        alert("Fields need correction: \n" + reason);
        return false;
    }
    else {
        return true;
    }
}
function validateTerms(fld) {
  var error = "";
  if (fld.checked==false)
  {
    fld.style.background = 'Yellow';
    error = "Terms must be checked\n";
  }
  return error;
}
function validatePrimaryJob(fld)
{
    var error = "";
    if (fld.value == "" || fld.value == "0")
    {
        fld.style.background = 'Yellow';
        error = "My Focus is required \n";
    }
    return error;
}
function validateFirstName(fld)
{
    var error = "";
    var value = trim(fld.value);
    var illegalChars = /\W/;
    if (value == "")
    {
        fld.style.background = 'Yellow';
        error = "First Name is empty \n";
    } else if ((value.length > 100))
    {
        fld.style.background = 'Yellow';
        error = "First Name illegal length \n";        
    } else if (illegalChars.test(value))
    {   
        fld.style.background = 'Yellow';
        error = "First Name contains illegal characters \n";
    }
    return error;
}
function validateZip(fld)
{
    var error = "";
    var value = trim(fld.value);
    var illegalChars = /\W/;
    if (value == "")
    {
        fld.style.background = 'Yellow';
        error = "Zip/Postal is empty \n";
    } else if ((value.length > 100))
    {
        fld.style.background = 'Yellow';
        error = "Zip/Postal illegal length \n";        
    } else if (illegalChars.test(value))
    {   
        fld.style.background = 'Yellow';
        error = "Zip/Postal contains illegal characters \n";
    }
    return error;  
}
function validateDisplayName(fld)
{
    var error = "";
    var value = trim(fld.value);
    var illegalChars = /\W|\s/; //not used
    if (value == "")
    {
        fld.style.background = 'Yellow';
        error = "Display Name is empty \n";
    } else if ((value.length > 100))
    {
        fld.style.background = 'Yellow';
        error = "Display Name illegal length \n";        
    } 
    return error;
}
function validateParent(fld)
{
    alert(fld.value);
    var error = "";    
    if (fld.ischecked == false)
    {
        fld.style.background = 'Yellow';
        error = "Please Confirm Age Checkbox \n";
    } 

    return error;
}

function validateLastName(fld)
{
    var error = "";
    var value = trim(fld.value);    
    var illegalChars = /\W/;
    if (value == "")
    {
        fld.style.background = 'Yellow';
        error = "Last Name is empty \n";
    } else if ((value.length > 100))
    {
        fld.style.background = 'Yellow';
        error = "Last Name illegal length \n";        
    } else if (illegalChars.test(value))
    {   
        fld.style.background = 'Yellow';
        error = "Last Name contains illegal characters \n";
    }
    return error;
}

function validatePassword(fld) {
    var error = "";
    var value = trim(fld.value);       
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (value == "") {
        fld.style.background = 'Yellow';
        error = "Password is empty\n";
    } else if ( (value.length > 50)) {
        error = "The password is the wrong length \n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(value)) {
        error = "The password contains illegal characters \n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
   return error;
}
function validatePassword2(fld) {
    var error = "";
    var value = trim(fld.value);     
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (value == "") {
        fld.style.background = 'Yellow';
        error = "Confirm Password is empty\n";
    } else if ( (value.length > 50)) {
        error = "Confirm password is the wrong length \n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(value)) {
        error = "Confirm password contains illegal characters \n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
   return error;
}
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Email address is empty\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
//============================================
