/* 
 * NAME:    forms.js 
 * PURPOSE: javascript used for all forms, for tibetanolympics.com
 *      These are all functions which provide some kind of 
 *      display or checking to make things easier for the
 *      website user. They do not do anything vital to the
 *      website, as javascript is not secure, and also, can
 *      be turned off by the user.
 * STATUS:  14 apr 2007 jw - works.
 */

// changeInputClass()
//   - change the class name of an input form element
//     (example - hide submit button after submittal)
  function changeInputClass(styleChange, item) {
    item.className = styleChange;
  }


// checkEmailSyntax()
//   - checks email syntax.
// 19 jul 2003 - needs more work - it tells true for dot
//   if the dot is in the portion before the @.
  function checkEmailSyntax(checkString) {

    if (checkString) {
	    var newstr = "";
	    var at = false;
	    var dot = false;
	
	    // Do some preliminary checks on the data:
	
	    // If email address has a '@' character:
	    if (checkString.indexOf("@") != -1) {
	      at = true;
	
	    // If email address has a '.' character:
	    } else if (checkString.indexOf(".") != -1) {
	      dot = true;
	    }
	    // Parse remainder of string:
	    for (var i = 0; i < checkString.length; i++) {
	        ch = checkString.substring(i, i + 1)
	        if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z")
	                || (ch == "@") || (ch == ".") || (ch == "_")
	                || (ch == "-") || (ch >= "0" && ch <= "9")) {
	                newstr += ch;
	                if (ch == "@") {
	                    at=true;
	                }
	                if (ch == ".") {
	                    dot=true;
	                }
	        }
	    }
	
	// alert(dot);
	// return false;
	
	    if ((at == false) || (dot == false)) {
	      // Display error message:
	      return ("* The email address you entered doesn't look like an email address.\n");
	    } else {
	        return ("");
	    }
    } else {
        return ("");
    }
  }

// checkEmailMatch()
//   - make sure that two typed emails are the same
  function checkEmailMatch(theForm) {
    email1 = theForm.email_address1.value;
    email2 = theForm.email_address2.value;
    if (email1 != email2) {
      return("These email addresses don't match!");
    }
  }


// checkFormFields()
//   - save time for the uer, by checking for 
//     empty fields, email syntax, etc.
//     before form input goes to the script.
// note: The same checks *must* be made again in the
//       server-side script, as javascript is not secure.
  function checkFormFields(form, tag) {
alert(form);
  }

function checkContactFormFields(theForm) {

// debug:
// alert(theForm.message.value);

    var why = "";
    why += checkEmailSyntax(theForm.user_email.value);
    why += checkMessage(theForm.user_message.value);


    if (why != "") {
        var intro = "Sorry, there was a problem:\n";
        var msg = intro + why;
        // msg += "\n=== We need blah blah ... ===";
        alert(msg);
        return false;
    }

    // debug - to keep from processing the form when testing the javascript
    //   and everything is ok:
    //  else {
    //   alert("no problem!");
    //   return false;
    //}

}

function checkMessage(message) {

  var msg = "";
  // message text existence
  if (message=="") {
    msg += '* Please tell us something!\n';
  } 


  // message text length
  //  figuring as 5 characters = 1 word.
  //  2000 is allowing 200 words.
  var maxchars = 2000;  // (400 * 5)
  var maxwords = 400;
  var msglength = message.length;
  var msgwords = msglength / 5;


  if (msglength > maxchars) {
    // if too long...trim it!
    // field.value = field.value.substring(0, maxchars);
    msg += '* Your message was about ' + msgwords + ' words long.\n';
    msg += '   Please have no more than ' + maxwords +  ' words\n    in your message.\n';
  }

  // now return whatever we got;
  return(msg);

}

function checkCounselFormFields(theForm) {

// debug:
// alert(theForm.message.value);

    var why = "";
    why += checkEmailSyntax(theForm.form_email.value);
    why += checkName(theForm.form_name.value);
    why += checkClass(theForm.form_class.value);
    why += checkInstitution(theForm.form_institution.value);
    why += checkQuestion(theForm.form_question.value);


    if (why != "") {
        var intro = "Sorry, there was a problem:\n";
        var msg = intro + why;
        // msg += "\n=== We need blah blah ... ===";
        alert(msg);
        return false;
    }

    // debug - to keep from processing the form when testing the javascript
    //   and everything is ok:
      /*
      else {
        alert("no problem!");
        return false;
      }
      */
}


function checkName(name) {

  var msg = "";
  // name text existence
  if (name=="") {
    msg += '* Please tell us your name!\n';
  } 

  // name text length
  //  figuring as 5 characters = 1 word.
  //  2000 is allowing 200 words.
  var maxchars = 50;   

  if (name.length > maxchars) {
    // if too long...trim it!
    // field.value = field.value.substring(0, maxchars);
    msg += '   Please have no more than ' + maxchars +  ' letters\n    in your name.\n';
  }

  // now return whatever we got;
  return(msg);

} // End checkName.


function checkEmal(email) {

  var msg = "";
  // email text existence
  if (email=="") {
    msg += '* Please tell us your email address!\n';
  } 

  // email text length
  var maxchars = 50;   

  if (email.length > maxchars) {
    // if too long...trim it!
    // field.value = field.value.substring(0, maxchars);
    msg += '   Please have no more than ' + maxchars +  ' letters\n    in your email.\n';
  }

  // now return whatever we got;
  return(msg);

} // End checkEmal.


// (Can't use 'class' as variable name - reserved word.)
function checkClass(cls) {

  var msg = "";
  // class text existence
  if (cls=="") {
    msg += '* Please tell us your class!\n';
  } 

  // class text length
  var maxchars = 5;   

  if (cls.length > maxchars) {
    // if too long...trim it!
    // field.value = field.value.substring(0, maxchars);
    msg += '   Please have no more than ' + maxchars +  ' characters\n    in your class.\n';
  }

  // now return whatever we got;
  return(msg);

} // End checkAge.


function checkInstitution(institution) {

  var msg = "";
  // institution text existence
  if (institution=="") {
    msg += '* Please tell us your school!\n';
  } 

  // email text length
  //  figuring as 5 characters = 1 word.
  //  2000 is allowing 200 words.
  var maxchars = 100;   

  if (institution.length > maxchars) {
    // if too long...trim it!
    // field.value = field.value.substring(0, maxchars);
    msg += '   Please have no more than ' + maxchars +  ' letters\n    in your school name.\n';
  }

  // now return whatever we got;
  return(msg);

} // End checkInstitution.


function checkQuestion(question) {

  var msg = "";
  // question text existence
  if (question=="") {
    msg += '* Please ask your question!\n';
  } 

  // question text length
  //  figuring as 5 characters = 1 word.
  //  2000 is allowing 200 words.
  var maxchars = 1250;   // (250 * 5)
  var maxwords = 250;
  var msglength = question.length;
  var msgwords = msglength / 5;


  if (msglength > maxchars) {
    // if too long...trim it!
    // field.value = field.value.substring(0, maxchars);
    msg += '* Your question was about ' + msgwords + ' words long.\n';
    msg += '   Please have no more than ' + maxwords +  ' words\n    in your question.\n';
  }

  // now return whatever we got;
  return(msg);

} // End checkQuestion.



/* e o f */
