// Copyright © 2001 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.

// email

// check to see if form value is checked

function checkDate(strng){
var error="";
var stringA=strng.split("/");
if( ( stringA[0].length == 2 && returnNumeric(stringA[0]) ) && ( stringA[1].length == 2 && returnNumeric(stringA[1]) ) && ( stringA[2].length == 4 && returnNumeric(stringA[2]) ) ){
}
else{
error = "Please ensure the date is in format MM/DD/YYYY.\n";
}
return error;

}

function returnNumeric(strng){
var error = "";
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < strng.length && IsNumber == true; i++) 
      { 
      Char = strng.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         return false;
         }
      }
   return true;  
}

function checkZipLength(strng){
var error="";
if(!(strng.length == 5 && returnNumeric(strng))){
error = "Please ensure the Zip Code is 5 Numeric characters.";
}
return error;
}

function checkOverX(strng,count,errorMsg){
var error="";
if(strng.length > count){
error = errorMsg;
}
return error;
}

function checkStringLength(strng,count)
{
var error="";
if(strng.length != count){
error = "Please ensure the year is " + count + " characters.\n";
}
return error;
}

function areOldNewPassword(strng,strng2){
var error="";
if ( ( strng == "" && strng2 != "" ) || ( strng != "" && strng2 == "" ) ){
  error="If you wish to change your password, please enter your old and new password.\n"
} 
return error;
}

function checkCFP(strng,strng2){
var error="";

if( ( strng == "1" ) && ( strng2 == "-1") )
{
  error = "Please select which CFP if this poster is a CFP type.\n"
} 


return error;
}

function checkOver500(strng)
{
var error="";
if(strng.length > 500){
error = "Please ensure the description is less than 500 characters.\n";
}
return error;
}

function checkStringLength(strng,count)
{
var error="";
if(strng.length != count){
error = "Please ensure the year is " + count + " characters.\n";
}
return error;
}

function checkNewUpload(obj,strng1,strng2){
var error="";
  if (obj.checked)
  {
  if( ( strng1.length == 0 ) || ( strng2.length == 0 ) ){
    var error="Please provide a thumbnail and pdf file if the update files box is checked.\n";
    return error;
    }
  return error;  
  }
  else
  {
    return error;
  }
  return error;  
}

function filterFileType(field, ext) {
if (field.value.indexOf('.' + ext) == -1) {
return false;
}
return true;
}

function checkFileType(obj,obj2,strng){
var error="";
if (obj.checked){
if (!filterFileType(obj2, strng)) {
error="Please use only PDF for Poster and JPG for Thumbnail.\n";
}
}
return error;
}


function isChecked(obj)
{
var error="";
if (obj.checked)
{
return error;
}
else
{
var error="In order to submit a poster, you must agree to the Creative Commons and intellectual property and copyright agreement.\n";
return error;
} 
}

function checkEmail (strng) {
var error="";
if (strng == "") {
   error = "You didn't enter an email address.\n";
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "The email address contains illegal characters.\n";
       }
    }
return error;    
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
var error = "";


var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (!(stripped.length == 10) ) {
	error = "The phone number is the wrong length. Make sure you included an area code.\n";
    } 
    
    else if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters.";
  
    }


return error;
}


// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (strng) {
var error = "";

    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if (illegalChars.test(strng)) {
      error = "The password contains illegal characters.\n";
    } 
 
return error;    
}    


// username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a username.\n";
}


    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 4) || (strng.length > 10)) {
       error = "The username is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
    error = "The username contains illegal characters.\n";
    } 
return error;
}       


// non-empty textbox

function isEmpty(strng) {
var error = "";
  if (strng.length == 0) {
     error = "The mandatory text area has not been filled in.\n"
  }
return error;	  
}

function isNumeric(strng){
var error = "";
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < strng.length && IsNumber == true; i++) 
      { 
      Char = strng.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         error = "Please ensure the Year created field is Numeric.\n"
         }
      }
   return error;
   
}

// was textbox altered

function isDifferent(strng) {
var error = ""; 
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n";
  }
return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button.\n";
    }
return error;
}

// valid selector from dropdown list

function checkDropdown(choice) {
var error = "";
    if (choice == 0) {
    error = "You didn't choose an option from the drop-down list.\n";
    }    
return error;
}    


function validateFileExtension(fld,action) {

    var error = "";
    
    if(action == 'jpg'){

      if(!/(\.jpg|\.jpeg)$/i.test(fld)) {
      
          error = "Please select a .jpg or .jpeg file for the thumbnail field.\n";
      
      }
      
    }else if(action == 'pdf'){
    
      if(!/(\.pdf)$/i.test(fld)) {
      
          error = "Please select a .pdf file for the poster field.\n";
      
      }    
    
    }


  
  
  return error;

}

