var ViewerImages = new Array();
				
function PreloadViewer(imgArray){
	//counter
	var i = 0;
	
	// start preloading
    for(i=0; i<imgArray.length; i++) 
    {   
		
		// create object
    	var imageObj = new Image();
		imageObj.src=imgArray[i];
    }
}


function imageChange(imageName,directory,msg) {
	var strImgPath = '/images/viewer/';
	if(directory != ""){
		strImgPath += directory + '/';	
	}
	document.images['displayImage'].src = strImgPath + imageName;
	
	
	//document.getElementById("caption").innerHTML = msg
}
function imageChange2(imageURL,imageWidth,imageHeight,imageDiv,imageID) {
	var objImgDiv = document.getElementById(imageDiv);
	
	objImgDiv.innerHTML = "<img src=\"" + imageURL + "\" width=\"" + imageWidth + "\" height=\"" + imageHeight + "\" alt=\"\" id=\"" + imageID + "\" />";
	
}
function imageChange3(imageURL) {	
	document.images['displayImage'].src = imageURL;	
}



function stopAutoChange(){
}


// Cross Browser DOM Functions used to create objects and get top / left coords
// copyright Stephen Chapman, 4th Jan 2005
// you may copy this code but please keep the copyright notice as well
var aDOM = 0, ieDOM = 0, nsDOM = 0
var stdDOM = document.getElementById;
if (stdDOM) aDOM = 1; else {
  ieDOM = document.all;
  if (ieDOM) aDOM = 1; else {
    var nsDOM = (
      (navigator.appName.indexOf('Netscape') != -1)
      && (parseInt(navigator.appVersion) ==4));
    if (nsDOM) aDOM = 1;
  }
}
function xDOM(objectId, withStyle) {
var st = ''; if (withStyle) st = '.style';
if (stdDOM) return (eval('document.getElementById(objectId)'+st));
if (ieDOM) return (eval('document.all[objectId]'+st));
if (nsDOM) return (document.layers[objectId]);
}

/// Form validation functions
function strTrim(strText) {
		for (i=0; i<strText.length; ++i) {
			if (strText.charAt(i) != ' ') break;
		}
		if (i >= strText.length) return ('');
		if (i > 0) strText = strText.substring(i, strText.length);
		for (j=strText.length-1; j > i; --j) {
			if (strText.charAt(j) != ' ') break;
		}
		if (j < strText.length -1)
			strText = strText.substring(0, j+1);
		return strText;

}

/*
function ValidEmail(strEmail)
{
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	
	if (filter.test(strEmail))
		return true
	else
		return false
}
*/
function ValidEmail(email)
{
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  if (theStr.indexOf(" ") > 0 )  return false;  
  if (theStr.indexOf("@", index + 1) > 0 ) return false;
  return result;
}

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}


function ValidText(strText, intMin)
{
	if (strText == "" || strText.length < intMin)
		return false;
	else
		return true;
}

function TextStartNumber(strText)
{   
	// Check if first character is a number
	if ((strText.charAt(0) < "0") || (strText.charAt(0) > "9"))
	{	
		return false;
	}
	else
	{
		return true;
	}
}	

function ValidChecked(objField) {
// This can be used for both checkboxes and radio buttons
	if(objField.length) {
		for (var i=0 ; i<objField.length ; i++) {
			if (objField[i].checked)
				return true;
		}
	} else {
		//only one item
		if(objField.checked)
			return true;
	}
}

function IsValidDate(intDay, strMonth, intYear) {
	var blnLeapYear = false;
	var blnRet = false;

	if ((intDay>0) && (intDay<32) && (intYear>1900)) {
		switch (strMonth.toLowerCase()) {
		case "4":
		case "6":
		case "9":
		case "11":
		case "apr":
		case "jun":
		case "sep":
		case "nov":
			if (intDay < 31) blnRet = true;
			break;
		case "2":
		case "feb":
			if (intYear % 100 == 0) {
				if (intYear % 400 == 0)
					blnLeapYear = true;
			}
			else {
				if (intYear % 4 == 0)
					blnLeapYear = true;
			}

			if (blnLeapYear) {
				if (intDay < 30)
					blnRet = true;
			}
			else {
				if (intDay < 29)
					blnRet = true;
			}
			break;
		default:
			blnRet = true;
		}
	}
	return blnRet
}

function ValidateAvailSearch(obj){
		var strAlert = "";
		strAlert = "______________________________________\n\nPlease\n\n";
		var isAlert = false;
	
		// trim the values of white space
		
		var strArrYear = obj.strArrYear.options[obj.strArrYear.selectedIndex].value;
		var strArrMonth = obj.strArrMonth.options[obj.strArrMonth.selectedIndex].value;
		var strArrDay = obj.strArrDay.options[obj.strArrDay.selectedIndex].value;
		var strNights = obj.strNights.options[obj.strNights.selectedIndex].value;
		var intUnitType = obj.intUnitType.options[obj.intUnitType.selectedIndex].value;
		
		var datRequestDate = new Date(strArrMonth + "/" + strArrDay + "/" + strArrYear);
		//alert(datRequestDate);
		var datCurrDate = new Date();
		
		if (datRequestDate - datCurrDate < 0){
			strAlert += "- Arrival date must be later than today's date.\n";
			isAlert = true;	
		}
		
		if (strNights == "0"){
			strAlert += "- Select the number of nights\n";
			isAlert = true;
		}
		if (intUnitType == ""){
			strAlert += "- Select a unit type\n";
			isAlert = true;
		}
		
		if (isAlert == true){
			strAlert += "\n\n______________________________________";
			alert (strAlert);
			return false;
		}else{
			return true;
		}
	}
	
function ValidateContactForm(obj){
		var strAlert = "";
		strAlert = "______________________________________\n\nPlease\n\n";
		var isAlert = false;
	
		// trim the values of white space
		var strFirstName = strTrim(obj.strFirstName.value);
		var strLastName = strTrim(obj.strLastName.value);
		var strEmail = strTrim(obj.strEmailAddress.value);
				
		if (!ValidText(strFirstName,1)){
			strAlert += "- Enter your first name\n";
			isAlert = true;
		}
		else if (isInteger(strFirstName))
		{
			strAlert += "- Your first name must contain alphabetic characters.\n";
			isAlert = true;
		}	
		if (!ValidText(strLastName,1)){
			strAlert += "- Enter your last name\n";
			isAlert = true;
		}
		else if (isInteger(strLastName))
		{
			strAlert += "- Your last name must contain alphabetic characters.\n";
			isAlert = true;
		}			
	
		if(strEmail == "") {
				strAlert += "- Enter an email address\n";
				isAlert = true;
			}		
		if (strEmail != ""){
			if (!ValidEmail(strEmail)){
				strAlert += "- Enter a valid email address\n";
				isAlert = true;
			}
		}
		
		if (isAlert == true){
			strAlert += "\n\n______________________________________";
			alert (strAlert);
			return false;
		}else{
			return true;
		}
	}

function advImgNext(imgID,numImgs){
	
	var nextImg = dispImg + 1;
	if (nextImg > numImgs){
		nextImg = 1;
	}		
	document.getElementById(imgID).src = advImages[nextImg].src;
	dispImg = nextImg;
 }	
	
window.onload = function(){
	if(ViewerImages){
		PreloadViewer(ViewerImages);
	}
}

// Banner Rotation Script

var arr
var currBnr = 0
$(document).ready(function() {

    //Create Home Banner array and Preload images
    if ($("#bannerimg").length) {
        //alert("preload")
        var count = 0
        arr = jQuery.makeArray($("#banner #bnrImg span"));
        for (var i = 0; i < (arr.length); i++) {
            $('<img />')
                .attr('src', arr[i].innerHTML)
                .attr('alt', "Photos by Rich Duncan  http://www.richphotography.ca")
                .attr('title', "Photos by Rich Duncan  http://www.richphotography.ca")
                .attr('width', "696")
                .attr('height', "170")
                .appendTo($('#bnrImg'))
                .load(function() {                    
                    count++;
                    if (count == arr.length) {
                        setInterval("ChangeBanner()", 5000);
                    }
                });
        }
    }
});

function ChangeBanner() {
    var nextBnr = currBnr + 1;
    if (currBnr >= arr.length) {
        nextBnr = 0;
    }
    //alert(nextBnr);
    var objCurrBnr = $("#bnrImg img:eq(" + currBnr + ")");
    var objNextBnr = $("#bnrImg img:eq(" + nextBnr + ")");

    $(objNextBnr).addClass("next");

    $(objCurrBnr).fadeOut("slow", function() {
        $(this).removeClass("top")
           .css("display","");
        $(objNextBnr).addClass("top");
        $(objNextBnr).removeClass("next");
        currBnr = nextBnr;        
    });    
}
