/*
Sean Daudelin
commons/jsfunctions.js
*/

function UpdateForumUser()
{
  var strErrors = "";
 
  if(document.usermod.elements["passwd"].value != document.usermod.elements["passwd2"].value) 
  {
    strErrors += "The two password fields must match\n";
  }


  if(document.usermod.elements["email"].value == "") 
  {
    strErrors += "Email is required\n";
  }else
  {
    strErrors += checkemail(document.usermod.elements["email"].value);
  }
  
  
  var tempfile = "" + document.usermod.elements["thumbnail"].value;
  var filetype = new Array("jpg","jpeg","gif");
  if(tempfile != "")
  {

    var tempfile=tempfile.split(".");
    var tempfile = tempfile[tempfile.length - 1];

    var somebool = false;

    for(var i = 0 ; i < filetype.length; i++)
    {
      if(tempfile == filetype[i])
	    somebool = true;
  
    }
  
    if(!somebool)
      strErrors +=  "Thumbnails must be of type .jpg, .jpeg or .gif\n";

  }


  
  
  if(strErrors != "")
  {
    alert("Please correct the following problem(s):\n\n" + strErrors);
  }else
  {
    document.usermod.submit();
  }
}




function checkemail(strString){

  var strSubstring = "";
  

  if(strString == ""){
    return true;
  }
  
  // remove a leading space if it is there
  strSubstring = strString.substr(0,1);
  while( strSubstring == " "){
   strString = strString.substr(1);
   strSubstring = strString.substr(0,1);
  }


  // remove an ending space if it is there
  strSubstring = strString.substr(strString.length - 1,1);
  while ( strSubstring == " "){
  if(strSubstring == " ")
    strString = strString.substr(0, strString.length - 1);
    strSubstring = strString.substr(strString.length - 1,1);
  }
  
  //if thers are still spaces, then input is wrong
  if( -1 != strString.search(/ /)){
    return "Invalid email address - Spaces not allowed\n";
  }

  // fixing the user's input
  document.usermod.email.value = strString;

  strSubstring = strString.substr(0,1);
  strString = strString.substr(1);
   
  //if first character was a @, then input was wrong
  if( strSubstring == "@"){
    return "Invalid email address - There is nothing before the @\n";
  }

  //going through the loop untill the @ is found
  while( (strSubstring != "@") &&(strString.length != 0)){
    strSubstring = strString.substr(0,1);
    strString = strString.substr(1);
  }
  
  //if strString.length = 0, we are out of string to parse
  if(strString.length == 0){
    return "Invalid email address - End of address not entered\n";
  }

  // if there are any more @'s then input is wrong
  strSubstring = strString.replace(/@/, "");
  if(strSubstring != strString){
    return "Invalid email address - Only one @ allowed\n";
  }
  
  // at least one letter should be found before the .
  strSubstring = strString.substr(0,1);
  strString = strString.substr(1);

  if(strSubstring == "."){
    return "Invlid email address - There is nothing between the @ and the .\n";
  }
  
  //removing bits of the email until after the . is left
  while( (strSubstring != ".") &&(strString.length != 0)){
    strSubstring = strString.substr(0,1);
    strString = strString.substr(1);
  } 
  
   
  //if strString.length = 0, we are out of string to parse
  if(strString.length == 0){
    return "Invalid email address - End of address not entered\n";
  }
  
  // strString should now contain something to the effect of com net org     etc.
  if((strString != "com") && (strString != "org") && (strString != "net") && 
    (strString != "us") && (strString != "ja") && (strString != "au") &&
    (strString != "ca") && (strString != "gov") && (strString != "uk") &&
    (strString != "edu") && (strString != "biz") && (strString != "info") &&
    (strString != "tv") && (strString != "ws") && (strString != "cc") &&
	(strString != "tk")){
      if(window.confirm("Unknown email extension, are you sure this is correct?"))
        return "";
      else{
        return "Please Provide a valid email extension\n";
      }
  }

  
  return "";
}


function Post()
{
 var hidediv = document.getElementById('hidediv');

 if(hidediv.style.display == 'none')
 {
   hidediv.style.display = 'block';
   document.forumpost.elements["send"].value = 'Post!';
 }
 else
 {
   if((document.forumpost.name.value == '')||(document.forumpost.comments.value == ''))
     alert('Both Name and Comments are required.');
   else{
     document.forumpost.send.disabled = true;
     document.forumpost.submit();
   }
 }
 
}


function PostVerify()
{
 var hidediv = document.getElementById('hidediv');

 if(hidediv.style.display == 'none')
 {
   hidediv.style.display = 'block';
   document.forumpost.elements["send"].value = 'Post!';
 }
 else
 {
   if((document.forumpost.name.value == '')||(document.forumpost.comments.value == '')||(document.forumpost.verifystring.value == ''))
     alert('Name, Comments and Verification Sting are required.');
   else{
     document.forumpost.send.disabled = true;
     document.forumpost.submit();
   }
 }
 
}

function PostUser()
{
 var hidediv = document.getElementById('hidediv');
 
 if(hidediv.style.display == 'none')
 {
   hidediv.style.display = 'block';
   document.forumpost.elements["send"].value = 'Post!';
 }
 else
 {
   if(document.forumpost.comments.value == '')
     alert('Comments are required.');
   else{
     document.forumpost.send.disabled = true;
     document.forumpost.submit();
   }
 }
}


function PostNews()
{
 var hidediv = document.getElementById('hidediv');

 if(hidediv.style.display == 'none')
 {
   hidediv.style.display = 'block';
   document.forumpost.elements["send"].value = 'Post!';
 }
 else
 {
   if((document.forumpost.title.value == '')||(document.forumpost.comments.value == ''))
     alert('Both Title and Content are required.');
   else{
     document.forumpost.send.disabled = true;
     document.forumpost.submit();
   }
 }
 
}


function NewThread()
{
 var hidediv = document.getElementById('hidediv');
 
 if(hidediv.style.display == 'none')
 {
   hidediv.style.display = 'block';
   document.forumpost.elements["send"].value = 'Create Topic';
 }
 else
 {
   if((document.forumpost.threadname.value == '')||(document.forumpost.comments.value == ''))
     alert('All fields are required');
   else{
     document.forumpost.send.disabled = true;
     document.forumpost.submit();
   }
 }
}


function SetCheckBox(fieldnm)
{
  if(document.usermod.elements[fieldnm].checked)
    document.usermod.elements[fieldnm + "Value"].value = "1";
  else
    document.usermod.elements[fieldnm + "Value"].value = "0";
}



function RegisterForumUser()
{
  var strErrors = "";
  
  if(document.usermod.elements["usernm"].value == "") 
  {
    strErrors += "User name is required\n";
  }else if(document.usermod.elements["usernm"].value != document.usermod.elements["usernm2"].value)
  {
    strErrors += "The two user name fields must match\n";
  }

  
  if(document.usermod.elements["firstnm"].value == "") 
  {
    strErrors += "First name is required\n";
  }
  
  if(document.usermod.elements["lastnm"].value == "") 
  {
    strErrors += "Last name is required\n";
  }
  
  
  if(document.usermod.elements["passwd"].value == "") 
  {
    strErrors += "Password is required\n";
  }else if(document.usermod.elements["passwd"].value != document.usermod.elements["passwd2"].value) 
  {
    strErrors += "The two password fields must match\n";
  }
  
  
  if(document.usermod.elements["email"].value == "") 
  {
    strErrors += "Email is required\n";
  }else if(document.usermod.elements["email"].value != document.usermod.elements["email2"].value)
  {
    strErrors += "The two email fields must match\n";
  }else
  {
    strErrors += checkemail(document.usermod.elements["email"].value);
  }
  

  
  
  if(strErrors != "")
  {
    alert("Please correct the following problem(s):\n\n" + strErrors);
  }else
  {
    document.usermod.submit();
  }
}


 
function insertAtCursor(formnm, fieldnm, myValue) {

  //IE support
 if (document.selection) {
    document.forms[formnm].elements[fieldnm].focus();
    sel = document.selection.createRange();
    sel.text = myValue;
  }
  //MOZILLA/NETSCAPE support
  else if (document.forms[formnm].elements[fieldnm].selectionStart || document.forms[formnm].elements[fieldnm].selectionStart == '0') {
    var startPos = document.forms[formnm].elements[fieldnm].selectionStart;
    var endPos = document.forms[formnm].elements[fieldnm].selectionEnd;
    document.forms[formnm].elements[fieldnm].value = document.forms[formnm].elements[fieldnm].value.substring(0, startPos)
                   + myValue
                   + document.forms[formnm].elements[fieldnm].value.substring(endPos, document.forms[formnm].elements[fieldnm].value.length);
  } else {
    document.forms[formnm].elements[fieldnm].value += myValue;
  }
}


function GalleryPopUp(dir, filename)
{

  window.open('galleryenlarge.jsp?gallerynm=' + dir + '&imagenm=' +  filename,'','menubar=no,location=no,toolbar=no,scrollbars=yes,resizable=yes');

}

function DeleteForumItem(tableName, postid, forumnm, subid)
{
  if(confirm("Are you sure that you would like to delete this item?"))
    window.location = "deletepostPost.jsp?forumname=" + tableName + "&postid=" + postid + "&forumnm=" + forumnm + "&subid=" + subid ;
}

function DeleteItem(tableName, postid)
{
  if(confirm("Are you sure that you would like to delete this item?"))
    window.location = "deletepostPost.jsp?forumname=" + tableName + "&postid=" + postid ;
}

function ForumMemberSearch(usrid)
{
  var searchbox = document.getElementById('searchbox');
  location = "memberInfo.jsp?usrid=" + usrid + "&search=" + searchbox.value ;
}

function GoToPage(forumnm, subid, currPageNum)
{
  document.location.search = "forumnm=" + forumnm + "&subid=" + subid + "&page=" + currPageNum;
}

