var currentLightState = "off";

function toggleLight(currDocument, pageName) {

   if(currentLightState == "off") {

      turnLightOn(currDocument, pageName);
      
   }
   else {
      turnLightOff(currDocument, pageName);
   }
}


function turnLightOn(currDocument, pageName) {
   currDocument.body.style.background = "#ffffff";
   currDocument.body.style.color= "#333"
   
   changecss(currDocument, ".largeImage", "border", "10px solid #ececec");
   changecss(currDocument, ".mediumImage", "border", "10px solid #ececec");
   changecss(currDocument, ".smallImage", "border", "10px solid #ececec");
   changecss(currDocument, ".thumbImage", "border", "5px solid #ececec");   
   
   changecss(currDocument, ".jdGalleryContainer", "border", "10px solid #ececec");
   changecss(currDocument, ".slideElement", "backgroundColor", "#ececec");
   
   changecss(currDocument, ".captionAndToggleContainer", "border", "10px solid #ececec");
   changecss(currDocument, ".captionAndToggleContainer", "background", "#ececec");
   
   currentLightState = "on";
   
   changeText (currDocument, "toggleLight", "TEST Turn Light off");
}


function turnLightOff(currDocument, pageName) {

   currDocument.body.style.background = "#393939";
   currDocument.body.style.color= "#ececec";
   
   
   changecss(currDocument, ".largeImage", "border", "10px solid #000000");
   changecss(currDocument, ".mediumImage", "border", "10px solid #000000");
   changecss(currDocument, ".smallImage", "border", "10px solid #000000");
   changecss(currDocument, ".thumbImage", "border", "5px solid #000000");
   
   changecss(currDocument, ".jdGalleryContainer", "border", "10px solid #000000");
   changecss(currDocument, ".slideElement", "backgroundColor", "#000000");
   
   changecss(currDocument, ".captionAndToggleContainer", "border", "10px solid #000000");
   changecss(currDocument, ".captionAndToggleContainer", "background", "#000000");
   
   
   if (pageName === "galleryDetail") {
   
   
   }
   
   currentLightState = "off";
   
   changeText (currDocument, "toggleLight", "Turn THE Light on");   
}




function changecss(currDocument, theClass,element,value) {

   var cssRules;
   if (currDocument.all) {
      cssRules = 'rules';
   }
   else if (currDocument.getElementById) {
      cssRules = 'cssRules';
   }
   
   var added = false;
   for (var S = 0; S < currDocument.styleSheets.length; S++){
      for (var R = 0; R < currDocument.styleSheets[S][cssRules].length; R++) {
         if (currDocument.styleSheets[S][cssRules][R].selectorText == theClass) {
            if(currDocument.styleSheets[S][cssRules][R].style[element]) {
               currDocument.styleSheets[S][cssRules][R].style[element] = value;
               added=true;
               break;
            }
         }
      }

      if(!added){
         if(currDocument.styleSheets[S].insertRule){
            currDocument.styleSheets[S].insertRule(theClass+' { '+element+': '+value+'; }',currDocument.styleSheets[S][cssRules].length);
         } 
         else if (currDocument.styleSheets[S].addRule) {
            currDocument.styleSheets[S].addRule(theClass,element+': '+value+';');
         }
      }
   }
}

function changeText(currDocument, objectId, newText){
   if (document.getElementById(objectId) !== undefined ) {
      document.getElementById(objectId).innerHTML = newText;
   }
}



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 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'), false);
   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 newWindow(newContent)
	 {
	  winContent = window.open(newContent, 'nextWin', 'right=0, top=20,width=840,height=640, toolbar=no,scrollbars=auto, resizable=no')         
	 }	