var nextImg, prevImg, subCatID;

subCatID = 0;

var pageLoaded;
pageLoaded = false;

var isLightbox;
isLightbox = false;

var mode;
mode = "";

var currentSubCatNum;
currentSubCatNum=0;

var keys = { 37:'Arrow Left', 38:'Arrow Up', 39:'Arrow Right', 40:'Arrow Down' };

function keyCheck( e ) {
	var e = e || event ;
	if (nextImg && prevImg && ((subCatID && !isLightbox) || (!subCatID && isLightbox))) {
		if (keys[e.keyCode] == 'Arrow Left') {
			if (isLightbox) {
				goLightboxImage(prevImg);
			}
			else if (mode == "client" && subCatID > 0) {
				goClientImage(prevImg, subCatID);
			}
			else {
				goSubCategoryImage(prevImg, subCatID);
			}
		}
		else if (keys[e.keyCode] == 'Arrow Right') {
			if (isLightbox) {
				goLightboxImage(nextImg);
			}
			else if (mode == "client" && subCatID > 0) {
				goClientImage(nextImg, subCatID);
			}
			else {
				goSubCategoryImage(nextImg, subCatID);
			}
		}
	}
}

function openWindow(URL, xDim, yDim, windowName) {
	var popWin;
	popWin = window.open(URL,windowName,"titlebar=1,toolbar=0,location=0,menubar=0,scrollbars=1,resizable=1,channelmode=0,directories=0,status=1,width=" + xDim + ",height=" +yDim);
	popWin.focus();
}

function clearField(fld) {
	if (fld.value=='Enter Keyword here') {
		fld.value='';
	}
}

function fPageLoaded() {
	pageLoaded = true;
}

function setOpacity(selObj, value) {
	selObj.style.opacity = value/10;
	selObj.style.filter = 'alpha(opacity=' + value*10 + ')';
}

function goGallery() {
	if (pageLoaded==true) {
		document.location.href='gallery.asp';
	}
}

function goPage(pageName) {
	var l;
	for (l=1; l<=999; l++) {
		if (document.getElementById("cat_" + l)) {
			document.getElementById("cat_" + l).style.color="";
		}
		else {
			break;	
		}
	}
	
	document.getElementById("topDivider").style.display="none";
	document.getElementById("bottomDivider").style.display="none";
	document.getElementById("subCatImage").innerHTML = "";
	document.getElementById("subCatThumbs").innerHTML = "";
	document.getElementById("lnk_home").style.color="";
	document.getElementById("lnk_contact").style.color="";
	document.getElementById("lnk_bio").style.color="";
	document.getElementById("lnk_lightbox").style.color="";
	document.getElementById("categoryNames").style.display="none";
	document.getElementById("clientName").style.display="none";
	document.getElementById("lightboxMailer").style.display="none";
	document.getElementById("lineSpacer").style.display="";
	document.getElementById("downloadImagesDisplay").innerHTML = "";
	
	if (pageName=="") {
		document.getElementById("categoryNames").style.display="";
	}
	else if (pageName=="contact") {
		document.getElementById("dataFrame").src = "contact.asp";
	}
	else if (pageName=="lightbox") {
		document.getElementById("dataFrame").src = "lightbox.asp";
		document.getElementById("topDivider").style.display="";
		document.getElementById("bottomDivider").style.display="";
		document.getElementById("lightboxMailer").style.display="";
	}
	else if (pageName=="bio") {
		document.getElementById("dataFrame").src = "bio.asp";
	}
	else if (pageName!="") {
		if (pageName.indexOf("$CLIENT$_")>=0) {
			pageName = pageName.substr(9, pageName.length);
			document.getElementById("dataFrame").src = "lightbox.asp?mode=client&origin=" + pageName;
		}
		else {
			document.getElementById("dataFrame").src = "lightbox.asp?origin=" + pageName;
		}
		document.getElementById("topDivider").style.display="";
		document.getElementById("bottomDivider").style.display="";
		document.getElementById("clientName").style.display="";
	}
	
	if (document.getElementById("lnk_" + pageName)) {
		document.getElementById("lnk_" + pageName).style.color="#CFCFCF";
	}
	
	currentSubCatNum=0;
}

function goCategory(catID, catNum, totalCats) {
	document.getElementById("subCatImage").innerHTML = "";
	document.getElementById("topDivider").style.display="";
	document.getElementById("bottomDivider").style.display="";
	document.getElementById("lnk_home").style.color="";
	document.getElementById("lnk_contact").style.color="";
	document.getElementById("lnk_bio").style.color="";
	document.getElementById("lnk_lightbox").style.color="";
	document.getElementById("downloadImagesDisplay").innerHTML = "";
	
	var l;
	for (l=1; l<=totalCats; l++) {
		document.getElementById("cat_" + l).style.color="";
	}
	
	document.getElementById("cat_" + catNum).style.color="#CFCFCF";
	
	document.getElementById("dataFrame").src = "subCatsDisplay.asp?parentCatID=" + catID;
	
	currentSubCatNum=0;
}

function goSubCategory(subCatID, subCatNum, totalSubCats) {
	//document.getElementById("subCatImage").innerHTML = "";
	
	currentSubCatNum=subCatNum;
	
	var l;
	for (l=1; l<=totalSubCats; l++) {
		if (document.getElementById("subCat_" + l)) {
			setOpacity(document.getElementById("subCatImg_" + l), 4);
		}
	}
	
	setOpacity(document.getElementById("subCatImg_" + subCatNum), 10);
	
	document.getElementById("dataFrame").src = "subCatImagesDisplay.asp?subCatID=" + subCatID;
}

function goSubCategoryImage(subCatImgID, subCatID) {
	document.getElementById("dataFrame").src = "loadSubCatImage.asp?subCatID=" + subCatID + "&imageID=" + subCatImgID;
}

function goLightboxImage(lbImgID) {
	document.getElementById("dataFrame").src = "loadSubCatImage.asp?lbImgID=" + lbImgID;
}

function goClientImage(imageID, subCatID) {
	document.getElementById("dataFrame").src = "loadSubCatImage.asp?mode=client&subCatID=" + subCatID + "&imageID=" + imageID;
}

function addImgToLB(imageID) {
	document.getElementById("dataFrame").src = "addLBImg.asp?imageID=" + imageID;
}

function remImgFromLB(imageID) {
	document.getElementById("dataFrame").src = "delLBImg.asp?imageID=" + imageID;
}

function refreshLightboxWhenInLightbox() {
	if (isLightbox) {
		goPage("lightbox");
	}
}

function replace(string,text,by) {
	// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

function validateLoginForm(frm) {
	if (frm.username.value.length<3) {
		alert("Invalid username");
		return false;
	}
	if (frm.password.value.length<3) {
		alert("Invalid password");
		return false;
	}
	return true;
}

function sendLBEmail(frm) {
	if (frm.recipientEmail.value.length<6) {
		alert("Invalid recipient\'s email address (Minimum characters: 6)");
		frm.recipientEmail.focus();
		return false;
	}
	if (!echeck(frm.recipientEmail.value)) {	
		frm.recipientEmail.focus();
		return false;
	}
		
	frm.submit();
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail address")
		    return false
		 }

 		 return true					
}

function toggleCatRow(newsID) {
	if (document.all['editRow_' + newsID].style.display=='none') {
		document.all['editRow_' + newsID].style.display='';
	}
	else {
		document.all['editRow_' + newsID].style.display='none';
	}
}

function toggleImgRow(imageID) {
	if (document.all['editRow1_' + imageID].style.display=='none') {
		document.all['editRow1_' + imageID].style.display='';
		document.all['editRow2_' + imageID].style.display='';
		document.all['editRow3_' + imageID].style.display='';
	}
	else {
		document.all['editRow1_' + imageID].style.display='none';
		document.all['editRow2_' + imageID].style.display='none';
		document.all['editRow3_' + imageID].style.display='none';
	}
}

function deleteCatItem(catID) {
	document.catlist.catID.value=catID;
	document.catlist.action="deleteCatItem.asp";
	document.catlist.submit();
}

function deleteSubCatItem(catID) {
	document.catlist.catID.value=catID;
	document.catlist.action="deleteSubCatItem.asp";
	document.catlist.submit();
}

function deleteImageItem(imgID) {
	document.imagelist.imageID.value=imgID;
	document.imagelist.action="deleteImgItem.asp";
	document.imagelist.submit();
}

function updateCatItem(catID) {
	if (document.catlist.elements['catName_' + catID].value.length>0) {
		document.catlist.catID.value=catID;
		document.catlist.active.value=document.catlist.elements['chk_' + catID].checked;
		document.catlist.catOrder.value=document.catlist.elements['catOrderNum_' + catID][document.catlist.elements['catOrderNum_' + catID].selectedIndex].value;
		document.catlist.catName.value=document.catlist.elements['catName_' + catID].value;
		document.catlist.submit();
	}
	else {
		alert("You must specify a category name before updating the category");
		document.catlist.elements['catName_' + catID].focus();
	}
}

function updateSubCatItem(catID) {
	if (document.catlist.elements['alias_' + catID]) {
		if (document.catlist.elements['alias_' + catID].value.length==0 && document.catlist.elements['parentCatID_' + catID][document.catlist.elements['parentCatID_' + catID].selectedIndex].value==0) {
			alert("You must specify an alias name if this subcategory belongs to a client's project");
			document.catlist.elements['alias_' + catID].focus();
			return false;
		}
	}
	
	if (document.catlist.elements['catName_' + catID].value.length==0) {
		alert("You must specify a category name before updating the category");
		document.catlist.elements['catName_' + catID].focus();
	}
	else {
		document.catlist.catID.value=catID;
		document.catlist.catName.value = document.catlist.elements['catName_' + catID].value;
		
		if (document.catlist.elements['alias_' + catID]) {
			document.catlist.alias.value = document.catlist.elements['alias_' + catID].value;
			document.catlist.client.value = document.catlist.elements['client_' + catID].value;
		}
		
		document.catlist.parentCatID.value = document.catlist.elements['parentCatID_' + catID][document.catlist.elements['parentCatID_' + catID].selectedIndex].value;
		document.catlist.active.value=document.catlist.elements['chk_' + catID].checked;
		document.catlist.catOrder.value=document.catlist.elements['catOrderNum_' + catID][document.catlist.elements['catOrderNum_' + catID].selectedIndex].value;
		document.catlist.action="updateSubCats.asp";
		document.catlist.submit();
	}
}

function addCatItem() {
	if (document.catlist.newCatName.value.length>0) {
		document.catlist.catName.value = document.catlist.newCatName.value;
		document.catlist.action="addCat.asp";
		document.catlist.submit();
	}
	else {
		alert("You must specify a category name before adding it to the list");
		document.catlist.newCatName.focus();
	}
}

function addSubCatItem() {
	if (document.catlist.newAlias) {
		if (document.catlist.newAlias.value.length==0 && document.catlist.newParentCatID[document.catlist.newParentCatID.selectedIndex].value==0) {
			alert("You must specify an alias name if this subcategory belongs to a client's project");
			document.catlist.newAlias.focus();
			return false;
		}
	}

	if (document.catlist.newCatName.value.length==0) {
		alert("You must specify a category name before adding it to the list");
		document.catlist.newCatName.focus();
	}
	else {
		document.catlist.catName.value = document.catlist.newCatName.value;
		
		if (document.catlist.newAlias) {
			document.catlist.alias.value = document.catlist.newAlias.value;
			document.catlist.client.value = document.catlist.newClient.value;
		}
		
		document.catlist.parentCatID.value = document.catlist.newParentCatID[document.catlist.newParentCatID.selectedIndex].value;
		document.catlist.action="addSubCat.asp";
		document.catlist.submit();
	}
}

function addImageItem(imgID) {
	if (document.imagelist["imgFile_" + imgID].value.length==0 && imgID==0) {
		alert("You must specify a large image filename before adding it to the list");
		document.imagelist.imgFile.focus();
	}
	else if (document.imagelist["subCatID_" + imgID].options.length==0 || document.imagelist["subCatID_" + imgID][document.imagelist["subCatID_" + imgID].selectedIndex].value=="0" || document.imagelist["subCatID_" + imgID][document.imagelist["subCatID_" + imgID].selectedIndex].value==0) {
		alert("You can not add an image to an empty Sub-Category");
	}
	else {
		document.imagelist.imageID.value = imgID;
		document.imagelist.submit();
	}
}

function removeAllOptions(selectbox)
{
	var i;
	for(i=selectbox.options.length-1;i>=0;i--)
	{
		selectbox.remove(i);
	}
}

function addOption(selectbox, value, text)
{
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;

	if (navigator.vendor) {
		if (navigator.vendor.indexOf("Apple")>=0) {
			selectbox.add(optn);
		}
		else {
			selectbox.options.add(optn);
		}
	}
	else {
		selectbox.options.add(optn);
	}
}

function populateSubFields(btn, subBtn, imgID, subCatID) {
	var frm;
	frm = btn.form;
	
	var selectBox, selectBoxIDData, selectBoxLabelData, i;
	
	var parentCatID;
	parentCatID = btn[btn.selectedIndex].value;
	
	selectBox = subBtn;
	removeAllOptions(selectBox);
	selectBox.disabled=false;
	
	selectBoxIDData = frm["subCatID_" + imgID + '_' + parentCatID].value;
	selectBoxIDData = selectBoxIDData.split('|');
	
	selectBoxLabelData = frm["subCatName_" + imgID + '_' + parentCatID].value;
	selectBoxLabelData = selectBoxLabelData.split('|');
	
	if (selectBoxIDData.length==0) {
		addOption(selectBox, 0, "N/A");
	}
	else {
		for (i=0; i<selectBoxIDData.length; i++) {
			addOption(selectBox, selectBoxIDData[i], selectBoxLabelData[i]);
			if (selectBoxIDData[i]==subCatID) {
				selectBox.options[i].selected=true;
				selectBox.selectedIndex = i;
			}
		}
	}
}

function initScrollLayers(showBar) {
 // arguments: id of layer containing scrolling layers (clipped layer), id of layer to scroll, 
  // if horizontal scrolling, id of element containing scrolling content (table?)
  var wndo = new dw_scrollObj('wn', 'lyr');
  
  // bSizeDragBar set true by default (explained at www.dyn-web.com/dhtml/scroll/ )
  // wndo.bSizeDragBar = false;
  
  // arguments: dragBar id, track id, axis ("v" or "h"), x offset, y offset
  // (x/y offsets of dragBar in track)

  if (showBar==true) {
	  wndo.setUpScrollbar("dragBar", "track", "v", 1, 1);
  }
  
  // pass id('s) of scroll area(s) if inside table(s)
  // i.e., if you have 3 (with id's wn1, wn2, wn3): dw_scrollObj.GeckoTableBugFix('wn1', 'wn2', 'wn3');
  dw_scrollObj.GeckoTableBugFix('wn'); 
  
  // sometimes ns6+ needs extra help to position correctly
  // dw_scrollObj.rePositionGecko(); 
  //dw_scrollObj.loadLayer('wn','lyr');
}

function initScrollLBLayers(showBar) {
 // arguments: id of layer containing scrolling layers (clipped layer), id of layer to scroll, 
  // if horizontal scrolling, id of element containing scrolling content (table?)
  var wndo = new dw_scrollObj('wnLB', 'lyr');
  
  // bSizeDragBar set true by default (explained at www.dyn-web.com/dhtml/scroll/ )
  // wndo.bSizeDragBar = false;
  
  // arguments: dragBar id, track id, axis ("v" or "h"), x offset, y offset
  // (x/y offsets of dragBar in track)

  if (showBar==true) {
	  wndo.setUpScrollbar("dragBar", "track", "v", 1, 1);
  }
  
  // pass id('s) of scroll area(s) if inside table(s)
  // i.e., if you have 3 (with id's wn1, wn2, wn3): dw_scrollObj.GeckoTableBugFix('wn1', 'wn2', 'wn3');
  dw_scrollObj.GeckoTableBugFix('wnLB'); 
  
  // sometimes ns6+ needs extra help to position correctly
  // dw_scrollObj.rePositionGecko(); 
  //dw_scrollObj.loadLayer('wn','lyr');
}

function initScrollSCLayers(showBar) {
 // arguments: id of layer containing scrolling layers (clipped layer), id of layer to scroll, 
  // if horizontal scrolling, id of element containing scrolling content (table?)
  var wndo = new dw_scrollObj('wnSC', 'lyrSC');
  
  // bSizeDragBar set true by default (explained at www.dyn-web.com/dhtml/scroll/ )
  // wndo.bSizeDragBar = false;
  
  // arguments: dragBar id, track id, axis ("v" or "h"), x offset, y offset
  // (x/y offsets of dragBar in track)

  if (showBar==true) {
	  wndo.setUpScrollbar("dragBarSC", "trackSC", "v", 1, 1);
  }
  
  // pass id('s) of scroll area(s) if inside table(s)
  // i.e., if you have 3 (with id's wn1, wn2, wn3): dw_scrollObj.GeckoTableBugFix('wn1', 'wn2', 'wn3');
  dw_scrollObj.GeckoTableBugFix('wnSC'); 
  
  // sometimes ns6+ needs extra help to position correctly
  // dw_scrollObj.rePositionGecko(); 
  //dw_scrollObj.loadLayer('wn','lyr');
}
