﻿//common function to have users select 3 or less checkboxes
function CheckRequiredCheckboxes_ClientValidate(source, args) {
  //debugger;
  var iFound = 0;   // number of checkboxes selected

  // get an array of all controls on the form
  var aControls = document.form1.elements;
  
  // iterate through array looking for checkboxes
  // with the ID of CheckBox starting with "chk" 
  for (var i=0; i < aControls.length; i++) {
    if (aControls[i].id.substring(0, 3) == 'chk') {

      // increment counter if checkbox is "ticked"
      if (aControls[i].checked) iFound++;
    }
  }
  //if no checkboxes are checked or more than 3 checkboxes checked then show the validation message  
  if ((iFound == 0) || (iFound > 3)) args.IsValid = false
  else {   
    args.IsValid = true;    
  }
                      
}

/* Causes validation if mouse is moved out of control with focus or its label (if there is a validator) */
		function BlurThenRefocus()
		{
    		if(document.activeElement == window.event.srcElement
			|| window.event.srcElement.nodeName == 'LABEL'
				&& document.activeElement.id == window.event.srcElement.htmlFor)
			{
				document.activeElement.blur();
				if(window.event.srcElement.nodeName == 'LABEL')
				{
					document.getElementById(window.event.srcElement.htmlFor).focus();
				}
				else
				{
					window.event.srcElement.focus();
				}
			}
		}
    
// common for contact forms
//common function to clear the fields
function btnClear_ClientClick() {
     document.form1.reset();          
     ResetValidators("Required")        
     } 
//common function to clear the validators
function ResetValidators(validationControlsPrefix){
        //debugger;
        var spans;       

        if (document.all) {
            spans = document.all.tags('span');        
        }
        else if (document.getElementsByTagName) {
            spans = document.getElementsByTagName('span');       
        }        
        if (spans) {
            for (var i = 0; i < spans.length; i++) {
                var prefixLength = "" + validationControlsPrefix.length;
                var currID = "" + spans[i].id
                if ((currID != '') && (prefixLength != '')) {
                    if (currID.substring(0,prefixLength) == validationControlsPrefix) {
                        if (spans[i].style.visibility.length > 0 && spans[i].style.display.length == 0){
                            spans[i].style.visibility = 'hidden';
                        }
                        else if (spans[i].style.display.length > 0 && spans[i].style.visibility.length == 0){
                            spans[i].style.display = 'none';
                        }                       
                    }
                }
            }
        }
    }

    // create the image rotator

    $(function () {
        var inv = setInterval("rotateImages()", 12500);
        $("img#next").click(function () {
            clearInterval(inv);
            rotateImages();
            inv = setInterval("rotateImages()", 12500);
        });
        $("img#previous").click(function () {
            clearInterval(inv);
            rotatePreviousImages();
            inv = setInterval("rotateImages()", 12500);
        });
    });

    function rotateImages() {
        var oCurPhoto = $('#photoGallery div.current');
        var oNxtPhoto = oCurPhoto.next();
        if (oNxtPhoto.length == 0)
            oNxtPhoto = $('#photoGallery div:first');

        oCurPhoto.removeClass('current').addClass('previous');
        oNxtPhoto.css({ opacity: 0.0 }).addClass('current').animate({ opacity: 1.0 }, 1000,
                function () {
                    oCurPhoto.removeClass('previous');
                    $("#photGalleryBox #caption").text(oNxtPhoto.find("img").attr("caption"));
                });
    }

    function rotatePreviousImages() {
        var oCurPhoto = $('#photoGallery div.current');
        var oNxtPhoto = oCurPhoto.prev();
        oCurPhoto.removeClass('current').addClass('previous');
        if (oNxtPhoto.length == 0)
            oNxtPhoto = $('#photoGallery div:last');


        oNxtPhoto.css({ opacity: 0.0 }).addClass('current').animate({ opacity: 1.0 }, 1000,
                function () {
                    oCurPhoto.removeClass('previous');
                    $("#photGalleryBox #caption").text(oNxtPhoto.find("img").attr("caption"));
                });
    }
