function onloadFileUploadPage () {
   alert("ok");
   SI.Files.stylizeAll();

}


function validateFileExtension(field) {   
   
   if(!/(\.bmp|\.gif|\.jpg|\.jpeg|\.BMP|\.GIF|\.JPG|\.JPEG)$/i.test(field.value)) {
      //alert("Invalid image file type.");
      //field.form.reset();
      //field.focus();
      return false;
   }
   return true;
}

function validateEmail(field, allowEmpty) {

   if(field == null || trim(field) == "") {
      if(allowEmpty == true) {
         return true;
      }
      else {
         return false;      
      }
   }
   
   
   with (field) {
      apos=value.indexOf("@");
      dotpos=value.lastIndexOf(".");
      if (apos<1||dotpos-apos<2) {
         
         return false;
      }
      else {
         return true;
      }
   }
} 


function validateTextField (field) {

   if(field == null) {
      return false;
   }
   if (trim(field) == "") {
      return false;
   }
   return true;
}


function validateCommentTextField (field) {
/*   
   This first if statement is added because if an authenticated user posts comments, 
   then, the username, email, url fields are not present. Instead they are replaced 
   by hidden, system, obscurely named, input fields. Therefore the non-present fields do
   not need validation

*/   
   if(field == null) {
      return true;
   }
   if (trim(field) == "") {
      return false;
   }
   return true;
}


function trim(field) {
   with (field) {
      var str = value;
//      alert ("in here -" + value + "-");   
      while(''+str.charAt(0)==' ') {
         str=str.substring(1,str.length);
      }
      while(''+str.charAt(str.length-1)==' ') {
         str=str.substring(0,str.length-1);
      }
   }
   
//   alert ("in here2 -" + str + "-");      
   return str;

}

function validateInspirationForm(formObject) {
   var validate = validateTextField(document.getElementById('field_id_24'));

   if (validate == false) {
      alert ("The name field is required");
      return false;
   }
   
   validate = validateTextField(document.getElementById('field_id_26'));
   if (validate == false) {
      alert("The display name field is required");
      return false;
   }   
   
   validate = validateEmail(document.getElementById('field_id_25'), false);
   if (validate == false) {
      alert("The value in the email field is invalid");
      return false;
   }      

   
   validate = validateTextField(document.getElementById('field_id_18_img[]'));
   //alert("val 1: " + validate);
   if (validate == false) {
      alert("Image 1 field is required");
      return false;
   }  
   
   validate = validateFileExtension(document.getElementById('field_id_18_img[]'));
   //alert("val 2: " + validate);
   if (validate == false) {
      alert("Image 1 filetype is invalid. Valid file types are: jpg, jpeg, gif and bmp");
      return false;
   }     
   

   validate = validateTextField(document.getElementById('field_id_19_img[]'));
   //alert("val 3: " + validate);
   if (validate == true) {
      //alert("trim: " + trim(document.getElementById('field_id_19_img[]')));   
      validate = validateFileExtension(document.getElementById('field_id_19_img[]'));
      //alert(validate);
      if (validate == false) {
         alert("Image 2 filetype is invalid. Valid file types are: jpg, jpeg, gif and bmp");
         return false;
      }  
   }  
    

   validate = validateTextField(document.getElementById('field_id_20_img[]'));
   if (validate == true) {
      validate = validateFileExtension(document.getElementById('field_id_20_img[]'));
      if (validate == false) {
         alert("Image 3 filetype is invalid. Valid file types are: jpg, jpeg, gif and bmp");
         return false;
      }  
   }    
   
   formObject.submit();
   return true;
   
   
}

function validateCommentForm(formObject) {
/*
   var captcha = document.getElementById('captcha');
   
   
   if ((trim (captcha) != "faking captcha") )  {
   
     alert ('Please enter "faking captcha" in the Security field below');
     return false;
   }
*/   
   
   var validate = validateCommentTextField(document.getElementById('name'));

   
   if (validate == false) {
      alert ("The name field is required");
      return false;
   }
   
   validate = validateEmail(document.getElementById('email'), true);
   if (validate == false) {
      alert("The value in the email field is invalid");
      return false;
   }   
        
   
   validate = validateCommentTextField(document.getElementById('comment'));

   if (validate == false) {
      alert ("The comment field is required");
      return false;
   }  
   formObject.submit();
   return true;
}



function validateAndSendContactForm(formObject, previewDivName) {
 
   
   var validate = validateTextField(document.getElementById('name'));
   
   if (validate == false) {
      alert ("The name field is required");
      return false;
   }
   
   validate = validateEmail(document.getElementById('email'), false);
   if (validate == false) {
      alert("The value in the email field is invalid");
      return false;
   }   
        
   
   validate = validateTextField(document.getElementById('comment'));

   if (validate == false) {
      alert ("The comment field is required");
      return false;
   }  
   
   var previewDivObject = document.getElementById(previewDivName);
   previewDivObject.innerHTML = '';

   contactUs(formObject);

   return true;
}


function reloadTextDiv(previewDivName, dynamicTextObject) {

   //var newText = document.getElementById(dynamicTextObject).value;
   var newText = dynamicTextObject.value;
   newText = newText.replace(/\n/g, '<br />');
   var previewDivObject = document.getElementById(previewDivName);
   previewDivObject.innerHTML = newText;
}



