var loc = window.location.search.substring(1);
var str = loc.toString();

if(str == "module=lightbox")
	lightBox_b  = true;
	
else if(str != "module=lightbox")
	lightBox_b  = false;


function hideSignIn(){
	
	var div = document.getElementById("signIn");
	div.style.display = "none";	
	
}

function showSignIn(){
	
	var div = document.getElementById("signIn");
	div.style.display = "block";
	document.signInForm.email.focus();
	
}

function showSearch(){
	
	var div = document.getElementById("searchForm");
	div.style.display = "block";
	document.advSearch.advKeywords.focus();
	document.advSearch.advKeywords.value = document.search.keywords.value;
	//input value should be the same as the input value in the main search panel
	
	var sDiv = document.getElementById("signIn");
	if(sDiv)
		if(sDiv.style.display == "block") sDiv.style.display = "none";
		
	var aDiv = document.getElementById("addForm");
	if(aDiv)
		if(aDiv.style.display == "block") aDiv.style.display = "none";
	
}

function hideSearch(){
	
	var div = document.getElementById("searchForm");
	div.style.display = "none";	
	
	//var linkDiv = document.getElementById("signInLink");
	//linkDiv.style.display = "block";
}

function setInitialLightbox(id){
	//alert(id);
	document.cookie = "userLightbox=" + id;
	
}

function createHeader() {
	
	var r_num = Math.random()
	var num = Math.round((9) * r_num);
	
	var filepath = "assets/graphics/images_site_header_"+ num +".jpg";
	document.write("<img src='" + filepath + "' alt='NatureSmart Images' style='border:0; float:left' />");
}

//Email Checker

function checkForm (form) {
	var field_ar = ["name", "email", "subject", "comments"];
	var msg_ar = ["Please enter your name.", "Please enter your email address.", "Please enter a subject.", "Please enter a message."];
	var error_ar = new Array();
	for (var i =0; i <field_ar.length; ++i) {
		var field = field_ar[i];
		if(form[field].value == "" || form[field].value == null)
			error_ar.push(i);
	}
	
	
	
	if (error_ar.length !=0){
		var val = error_ar[0];
		var tempField = field_ar[val];
		form[tempField].focus();
		alert(msg_ar[val]);
		return false;
	}
	else
		var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		str = form.email.value;
		if(str.match(emailRegEx))
			return true;
		
		else {
			alert("Please enter a valid email address.");
			form.email.focus();
			return false;
		}
}


//Lightbox funtions

var lightBox = [];
var lightBox_b;
var id;

function setLightboxID(value){
	id = value;	
}

function updateLightboxID(value){
	//alert(value);
	document.cookie = "userLightbox=" + value;
}

function createLightbox(value){
	id = value;
	//alert("COOKIE: " + id);
	//var expire = expireDate();
	//document.cookie = "lightbox=" + id + ";expires=" + expire.toGMTString();
}

function expireDate(){
	
	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0) nDays=1;
	expire.setTime(today.getTime() + 3600000*24*365);
	//document.cookie = cookieName+"="+escape(cookieValue)
	return expire;
}

function setList(str) {
	lightBox = [];

	if(str.length > 0){
		var temp = str.split(", ");
		for (var i = 0; i < temp.length; ++i){
			lightBox.push(temp[i]);
		}
	}
}


function checkLightbox(num) {
	//
	var found_b = false;
	for (var i=0; i <lightBox.length; ++i) {
		
		if(lightBox[i] == num){
			
			found_b = true;
		}
	}
	if(found_b){
		
		document.write("<p><input name=\"" + num + "\" type=\"checkbox\" value=\" \" onClick=updateLightbox(" + num + ") checked /> Lightbox</p>");
	}
	else 
		document.write("<p><input name=\"" + num + "\" type=\"checkbox\" value=\"selected\" onClick=updateLightbox(" + num + ") /> Lightbox</p>");
}



function updateLightbox(num) {

	var found_b = false;
	var val;
	var found_b = false;
	var str = "";
	
	for (var i=0; i <lightBox.length; ++i) {
		
		if(lightBox[i] == num){
			//alert("FOUND: " + num);
			var num = lightBox[i];
			lightBox.splice(i,1);
			if(lightBox_b) removeImage(num);
			found_b = true;
		}
	}
	if(!found_b){
		if(lightBox.length < 25){
	 		lightBox.push(num);
			setLightbox();
		}
		else 
			alert("Your lightbox is full. You'll need to remove an image before adding one.");
	}
	setLightbox();
}

function removeImage(num){
	var img=document.getElementById(num);
	img.style.display = "none";
	
	if(lightBox.length == 0) {
		
		/*var nav = document.getElementById("lightboxNav");
		nav.style.display = "none";
		
		var nav = document.getElementById("empty_lightbox");
		nav.style.display = "none";
		
		var nav = document.getElementById("nav_empty");
		nav.style.display = "block";
		*/
		var nav = document.getElementById("empty_message");
		nav.style.display = "block";
	}
}

function emptyLightbox() {
	
		if(confirm('Are you sure you want to empty your Lightbox?')){
			var _max = lightBox.length;
			if(lightBox.length > 0){
				for (var i=0; i < _max; ++i) {
					removeImage(lightBox[i]);
					//lightBox.splice(i,1);
				}
			}
			var nav = document.getElementById("empty_message");
			nav.style.display = "block";
			lightBox = [];
			setLightbox();
		}
}


function setLightbox() {
	var temp = lightBox.toString();
	var str = "";
	var _max = lightBox.length;
	if(lightBox.length > 0){
		for (var i=0; i < _max; ++i) {
			str += lightBox[i];
			if(i < _max -1) str += ", ";
		}
	}
	else str = "empty";
	//alert("STRING: " + str);
	//var box = document.getElementById("iBox");
	document.all.iBox.src="assets/php/includes/lightbox_functions.php?id=" + id + "&list=" + str;
	
}

function hideAddLB(){
	
	var div = document.getElementById("addForm");
	div.style.display = "none";	
	
}

function showAddLB(){
	
	var div = document.getElementById("addForm");
	div.style.display = "block";
	document.addLBForm.title.focus();
	
	var sDiv = document.getElementById("searchForm");
	if(sDiv.style.display == "block") sDiv.style.display = "none";
}

function viewLightbox(which){
	//get string from php -->go to link
	
	var loc = window.location;
	var tempLink= which.value;
	document.cookie = "userLightbox=" + tempLink;

	window.location=loc;
}


function readCookie(cookieName) {
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return ""; 
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}


function removeLightbox () {
	
	if(confirm('Are you sure you want to remove your Current Lightbox?')){
		
		window.location="?module=removeLightbox";
	}
	
}

//LARGE IMAGE FUNCTIONS


//Results Per Page


function setResultsPerPage(obj) {

	this.results_num = obj.value;
	var vars = window.location.search.substring(1).split("&");
	
	loc = "?";
	var found = false;
	for (i = 0; i< vars.length; ++i){
		str = vars[i].split("=");
		if(str[0] == "results"){
			found = true;
			loc += "&results=" + obj.value;
		}
		else {
			if(i >0)loc += "&";
			loc +=  vars[i];
		}
	}
	
	if(!found)loc += "&results=" + obj.value;
	
	//var loc = vars[0];
	//var temp = "?" + loc + "&results=" + this.results_num ;
	window.location = loc;
}

function setViewType(obj){
	//alert(obj.value);
	//alert("SET PAGE");
	var vars = window.location.search.substring(1).split("&");
	//var loc = vars[0];
	loc = "?";
	var found = false;
	for (i = 0; i< vars.length; ++i){
		str = vars[i].split("=");
		if(str[0] == "viewType"){
			found = true;
			loc += "&viewType=" + obj.value;
		}
		else if(str[0] == "page"){
				
				loc += "&page=1";
			}
		else {
			
			if(i >0)loc += "&";
			loc +=  vars[i];
		}
	}
	
	if(!found)loc += "&viewType=" + obj.value;
	//var temp = "?"+ obj.value + "&" + loc ;
	window.location = loc;
}

function setImagesPerPage(obj) {

	this.results_num = obj.value;
	var vars = window.location.search.substring(1).split("&");
	var loc = vars[0];
	var temp = "admin.php?" + loc + "&imagesNum=" + this.results_num ;
	window.location = temp;
}


function changeSoundsView(obj){
	window.location = "?module=sounds&view=" + obj.value;
}


//Copyright


function addCopyright() {

	var currentYear =  new Date();
	var thisYear = currentYear.getFullYear();
	document.write("This site and all contents are Copyright &copy; " + thisYear + " by <a href=\"mailto:stan@naturesmart.com\"><span id=\"copyrightLink\">Stan Tekiela</span></a>. All images on this site are copyright protected and cannot be used without the written permission from NatureSmart Images. No part of this site is in the public domain. Site design, development and maintenance by <a href=\"http://toonotron.com\" target = \"_blank\"><span id=\"copyrightLink\">Toonotron Interactive Design.</span> </a>");
	
	
}
