var NS4 = (document.layers) ? true : false;

function openWindow(URL, xDim, yDim) {
	var popWin;
	popWin = window.open(URL,"popTerms","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 openWindowName(URL, xDim, yDim, winName) {
	var popWin;
	popWin = window.open(URL,winName,"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='';
	}
	else if (fld.value=='Enter an address here') {
		fld.value='';
	}
}

function addLinkItem() {
	if (document.linkslist.linkURL.value.length>0 && document.linkslist.link_title.value.length>0) {
		document.linkslist.action="addLinks.asp";
		document.linkslist.submit();
	}
	else {
		alert("You must write a link title and URL before adding it to the list");
		document.linkslist.link_title.focus();
	}
}

function deleteLinkItem(linkID) {
	document.linkslist.linkID.value=linkID;
	document.linkslist.action="deleteLinksItem.asp";
	document.linkslist.submit();
}

function updateLinkItem(linkID) {
	if (document.linkslist.elements['link_url_' + linkID].value.length>0 && document.linkslist.elements['link_title_' + linkID].value.length>0) {
		document.linkslist.linkID.value=linkID;
		document.linkslist.title.value=document.linkslist.elements['link_title_' + linkID].value;
		document.linkslist.url.value=document.linkslist.elements['link_url_' + linkID].value;
		document.linkslist.catID.value=document.linkslist.elements['linkCatID_' + linkID].options[document.linkslist.elements['linkCatID_' + linkID].selectedIndex].value;
		document.linkslist.isPublished.value=document.linkslist.elements['chk_' + linkID].checked;
		document.linkslist.submit();
	}
	else {
		alert("You must write a link title and URL before updating");
		document.linkslist.elements['link_title_' + linkID].focus();
	}
}

function buyImg(priceListID) {
	var frm;
	frm = document.imageViewerForm;
	frm.item_name.value=frm["item_name_" + priceListID].value;
	frm.item_number.value=frm["item_number_" + priceListID].value;
	frm.amount.value=frm["amount_" + priceListID].value;
	frm.os0.value=frm["os0_" + priceListID].value;
	//frm.action="https://www.sandbox.paypal.com/cgi-bin/webscr";
	frm.action="https://www.paypal.com/cgi-bin/webscr";
	frm.submit();
}

function buySub(btn) {
	var frm;
	frm = btn.form;
	frm.item_name.value="Image download access package: " + frm.subTypes.options[frm.subTypes.selectedIndex].text;
	frm.item_number.value= "SUB_" + frm.subTypes.options[frm.subTypes.selectedIndex].value;
	frm.amount.value=frm["amount_" + frm.subTypes.options[frm.subTypes.selectedIndex].value].value;
	frm.action="https://www.paypal.com/cgi-bin/webscr";
	//frm.action="https://www.sandbox.paypal.com/cgi-bin/webscr";
	frm.submit();
}

function addImgFileTypeItem() {
	if (document.catlist.newCatName.value.length>0) {
		document.catlist.catName.value = document.catlist.newCatName.value;
		document.catlist.action="addImgFileType.asp";
		document.catlist.submit();
	}
	else {
		alert("You must specify an Image File Type before adding it to the list");
		document.catlist.newCatName.focus();
	}
}

function updateImgFileTypeItem(catID) {
	if (document.catlist.elements['catName_' + catID].value.length>0) {
		document.catlist.catID.value=catID;
		document.catlist.catName.value=document.catlist.elements['catName_' + catID].value;
		document.catlist.submit();
	}
	else {
		alert("You must specify an Image File Type before updating the list");
		document.catlist.elements['catName_' + catID].focus();
	}
}

function deleteImgFileTypeItem(catID) {
	document.catlist.catID.value=catID;
	document.catlist.catName.value=document.catlist.elements['catName_' + catID].value;
	document.catlist.action="deleteImageFileTypeItem.asp";
	document.catlist.submit();
}

function addLinkCatItem() {
	if (document.catlist.newCatName.value.length>0) {
		document.catlist.catName.value = document.catlist.newCatName.value;
		document.catlist.action="addLinkCat.asp";
		document.catlist.submit();
	}
	else {
		alert("You must specify a category name before adding it to the list");
		document.catlist.newCatName.focus();
	}
}

function updateLinkCatItem(catID) {
	if (document.catlist.elements['catName_' + catID].value.length>0) {
		document.catlist.catID.value=catID;
		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 deleteLinkCatItem(catID) {
	document.catlist.catID.value=catID;
	document.catlist.action="deleteLinkCatItem.asp";
	document.catlist.submit();
}

function toggleAdvancedSearch(advBut) {
	if (advBut.value=="Advanced >>") {
		advBut.value="<< Advanced";
		document.getElementById("advancedSearchTab").style.display = "";
		document.getElementById("shortSupportColumn").style.display = "";
		document.getElementById("longSupportColumn").style.display = "none";
		document.cookie="advsToggle=1; expires=Fri, 1 Jan 2010 20:47:11 UTC;";
	}
	else {
		advBut.value="Advanced >>";
		document.getElementById("advancedSearchTab").style.display = "none";
		document.getElementById("shortSupportColumn").style.display = "none";
		document.getElementById("longSupportColumn").style.display = "";
		document.cookie="advsToggle=0; expires=Fri, 1 Jan 2010 20:47:11 UTC;";
	}
}

function toggleRenameLB(renLBBut, lightBoxID) {
	if (renLBBut.value=="Rename current Lightbox >>") {
		renLBBut.value="<< Rename current Lightbox";
		document.getElementById("lbNamePanel").style.display = "";
		document.getElementById("lbName").focus();
	}
	else {
		renLBBut.value="Rename current Lightbox >>";
		document.getElementById("lbNamePanel").style.display = "none";
	}
}

function popIMGDLInstr() {
	var OSName="Unknown OS";
	if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
	if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
	if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
	if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";

	if (OSName=="Windows") {
		alert("To download a comp of this image, right-click on the image on screen\nand choose \"Save Picture As\" or \"Save Image As\"");
	}
	else if (OSName=="MacOS") {
		alert("To download a comp of this image, press CTRL and click on the image on screen,\nthen choose \"Download Image to Disk\"");
	}
	else {
		alert("To download a comp of this image, right-click on the image on screen\nand choose \"Save Picture As\" or \"Save Image As\"");
	}
}

function renameLB(renLBBut) {
	var frm;
	if (document.frames)
	{
		if (document.frames.length>0) {
			frm=renLBBut.document.frames['LightboxThumbs'].lightboxForm;
		}
		else {
			if (renLBBut.window) {
				frm=renLBBut.window.document.lightboxForm;
			}
			else {
				frm=document.lightboxForm;
			}
		}
	}
	else if (document.getElementById("LightboxThumbs")!=null) {
		if (LightboxThumbs.document) {
			if (LightboxThumbs.document.lightboxForm) {
				frm = LightboxThumbs.document.lightboxForm;
			}
		}
	}
	else {
		if (window.document.lightboxForm) {
			frm = window.document.lightboxForm;
		}
	}
	frm.newLBName.value = document.getElementById("lbName").value;
	frm.submit();
}

function toggleAdvSearchKwd(advKwd) {
	if (advKwd.value == "black and white" && advKwd.form.elements["kwdColour"].checked==true) {
		advKwd.form.elements["kwdColour"].click();
	}
	else if (advKwd.value == "colour" && advKwd.form.elements["kwdBW"].checked==true) {
		advKwd.form.elements["kwdBW"].click();
	}
	
	if (advKwd.value == "vertical" && advKwd.form.elements["kwdHorizontal"].checked==true) {
		advKwd.form.elements["kwdHorizontal"].click();
	}
	else if (advKwd.value == "horizontal" && advKwd.form.elements["kwdVertical"].checked==true) {
		advKwd.form.elements["kwdVertical"].click();
	}

	document.getElementById("searchkwd").value = replace(document.getElementById("searchkwd").value, "Enter Keyword here", "");
	
	if (advKwd.checked==true && document.getElementById("searchkwd").value.indexOf(advKwd.value)<0) {
		document.getElementById("searchkwd").value = document.getElementById("searchkwd").value + ' ' + advKwd.value;
		document.cookie="advKwd" + advKwd.value + "=1; expires=Fri, 1 Jan 2010 20:47:11 UTC;";
	}
	else if (advKwd.checked==false && document.getElementById("searchkwd").value.indexOf(advKwd.value)>=0) {
		document.getElementById("searchkwd").value = replace(document.getElementById("searchkwd").value,advKwd.value,"");
		document.getElementById("searchkwd").value = replace(document.getElementById("searchkwd").value, "  ", " ");
		document.cookie="advKwd" + advKwd.value + "=0; expires=Fri, 1 Jan 2010 20:47:11 UTC;";
	}
	document.getElementById("searchkwd").value = trim(document.getElementById("searchkwd").value);
	document.getElementById("fldsearchkwd").focus();
}

function checkEnter(event, frm)
{ 	
	var code = 0;
	
	if (NS4)
		code = event.which;
	else
		code = event.keyCode;
	if (code==13) {
		if (frm.form.name=="searchform") {
			searchKwd(frm);
		}
		else if (frm.form.name=="lightboxCreationForm") {
			sendLBEmail(frm);
		}
	}
}

function checkFVFiles(frm) {
	var fileFound = 0;
	for (i=1; i<=5; i++) {
		if (frm.elements["uplFile" + i].value!="") {
			fileFound = 1;
			break;
		}
	}

	if (!fileFound) {
		alert("Please choose at least one file to upload!");
		return false;
	}
	else {
		return confirm("Click OK to start uploading or Cancel to abort.\n(Please be patient. Uploading might take a few moments to complete.)");
	}
}

function getMultiQuote(frm) {
	if (document.frames)
		if (document.frames.length>0)
			frm=frm.document.frames['LightboxThumbs'].lightboxForm;
		else
			frm=frm.window.document.lightboxForm;
	else
		frm=frm.window.document.lightboxForm;
		
	if (frm.imageList.value.length>2) {
		frm.action="get_quote.asp";
		frm.imageID.value = frm.imageList.value;
		frm.submit();
	}
}

function sendImgEmail(frm) {
	if (document.frames)
		if (document.frames.length>0)
			frm=frm.document.frames['LightboxThumbs'].lightboxForm;
		else
			frm=frm.window.document.lightboxForm;
	else
		frm=frm.window.document.lightboxForm;
		
	if (frm.imageList.value.length>2) {
		frm.action="sendImgEmail.asp";
		frm.imageID.value = frm.imageList.value;
		frm.submit();
	}
}

function sendLBEmail(frm) {
	frm = frm.form;

	if (frm.email.value.length<6) {
		alert("Invalid email address (Minimum characters: 6)");
		frm.email.focus();
		return false;
	}
	if (!echeck(frm.email.value)) {	
		frm.email.focus();
		return false;
	}
		
	frm.action="sendLBEmail.asp";
	frm.submit();
}

function saveAsLB(frm) {
	frm = frm.form;
	frm.action="saveAsLB.asp";
	frm.submit();
}

function addIMGtoLB(frm, lightboxID) {
	frm=frm.form;
	frm.lightboxID.value=lightboxID;
	frm.action="addIMG.asp";
	frm.submit();	
}

function removeIMG(frm, imageID) {
	frm.imageID.value=imageID;
	frm.action="removeIMG.asp";
	frm.submit();
}

function setDefaultLB(lbID, lbName) {
	document.cookie="defLBID="+ lbID + "; expires=Fri, 1 Jan 2010 20:47:11 UTC;";
	document.cookie="defLBName=" + lbName + "; expires=Fri, 1 Jan 2010 20:47:11 UTC;";
}

function multiIMGDL(frm) {
	var imageList;
	var imageListArr;
	var i;
	
	if (document.frames)
		if (document.frames.length>0)
			frm=frm.document.frames['LightboxThumbs'].lightboxForm;
		else
			frm=frm.window.document.lightboxForm;
	else
		frm=frm.window.document.lightboxForm;
	
	imageList = frm.imageList.value;

	if (imageList.length>2) {
		frm.action="downloadIMG.asp";
		frm.target="_blank";	
			
		imageList = replace(imageList,'$','');
		imageListArr = imageList.split(',');

		for (i=0; i<imageListArr.length-1; i++) {
       		if (navigator.appVersion.indexOf("Win")!=-1) {
				frm.imageID.value = imageListArr[i];
				frm.submit();
			}
			else {
				openWindowName(frm.action + "?imageID=" + imageListArr[i], 625, 450, "popWin" + i);
			}
   		}
		
		frm.target="_top";
	}
}

function selectAllImgs() {
	var doc;
	var i;
	
	if (document.frames)
		if (document.frames.length>0)
			doc = document.frames['LightboxThumbs'].document;
		else
			doc = window.document;
	else
		doc = window.document;

	for (i=0; i<doc.links.length; i++) {

		if (doc.links[i].name!="") {
			if (doc.images["imageSelector" + doc.links[i].name].src.indexOf("_deselect")>-1) {
				selImage(doc.images['imageSelector' + doc.links[i].name], doc.links[i].id);
			}
		}
	}
}

function deSelectAllImgs() {
	var doc;
	var i;
	
	if (document.frames)
		if (document.frames.length>0)
			doc = document.frames['LightboxThumbs'].document;
		else
			doc = window.document;
	else
		doc = window.document;

	for (i=0; i<doc.links.length; i++) {

		if (doc.links[i].name!="") {
			if (doc.images["imageSelector" + doc.links[i].name].src.indexOf("_select")>-1) {
				selImage(doc.images['imageSelector' + doc.links[i].name], doc.links[i].id);
			}
		}
	}
}

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 trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
}

function goToCat(frm) {
	frm = frm.form;
	frm.searchkwd.value="";
	frm.searchType.value="cat";
	frm.submit();
}

function goToResPage(frm, pageNum) {
	frm = frm.form;
	frm.curPage.value=pageNum;
	frm.action="search_results.asp";
	frm.submit();
}

function goToLBResPage(frm, pageNum) {
	frm = frm.form;
	frm.curPage.value=pageNum;
	frm.action="lightbox.asp";
	frm.submit();
}

function selImage(imgTick, imageID) {
	var imageList;
	var entryPos;
	var entryValue;
	
	if (document.frames)
		if (document.frames.length>0)
			imageList = document.frames['LightboxThumbs'].lightboxForm.imageList.value;
		else
			imageList = window.document.forms['lightboxForm'].imageList.value;
	else
		imageList = window.document.forms['lightboxForm'].imageList.value;

	entryValue = "$" + String(imageID) + "$";

	if (imgTick.src.indexOf('deselect')>=0) {
		imgTick.src='images/but_select_image.gif';
		if (imageList.indexOf(entryValue)<0) {
			imageList=imageList + entryValue + ",";
		}
	}
	else {
		imgTick.src='images/but_deselect_image.gif';
		if (imageList.indexOf(entryValue)>=0) {
			entryPos=imageList.indexOf(entryValue);
			imageList=imageList.substring(0,entryPos) + imageList.substring(entryPos+entryValue.length+1,imageList.length);
		}
	}
		
	if (document.frames)
		if (document.frames.length>0)
			document.frames['LightboxThumbs'].lightboxForm.imageList.value=imageList;
		else	
			window.document.lightboxForm.imageList.value=imageList;
	else
		window.document.lightboxForm.imageList.value=imageList;
}

function makeNewLB(frm, userid) {
	frm.form.action="createLB.asp";
	frm.form.submit();
}

function deleteLB(frm, lightboxID) {
	frm.form.lightboxID.value=lightboxID;
	frm.form.action="deleteLB.asp";
	frm.form.submit();
}

function viewLBox(lightboxID) {
	var frm;
	var frmExists;
	frmExists = 0;

	if (document.frames) {
		if (document.frames.length>0) {
			if (document.frames['LightboxThumbs']) {
				frm = document.frames['LightboxThumbs'].lightboxForm;
				frmExists = 1;
			}
		}
		else {
			if (window.document.lightboxForm) {
				frm = window.document.lightboxForm;
				frmExists = 1;
			}
			else if (window.document.lightboxselector) {
				frm = window.document.lightboxselector;
				frmExists = 1;
			}
		}
	}
	else if (document.getElementById("LightboxThumbs")!=null) {
		if (LightboxThumbs.document) {
			if (LightboxThumbs.document.lightboxForm) {
				frm = LightboxThumbs.document.lightboxForm;
				frmExists = 1;
			}
		}
	}
	else {
		if (window.document.lightboxForm) {
			frm = window.document.lightboxForm;
			frmExists = 1;
		}
	}

	if (frmExists == 1) {
		frm.lightboxID.value=lightboxID;
		frm.action="lightbox.asp";
		frm.target="_top";
		frm.curPage.value=1;
		frm.submit();
	}
}

function enlargeImage(frm, imageID) {
	frm = frm.form;
	frm.imageID.value=imageID;
	frm.action="imageViewer.asp";
	frm.submit();
}

function backToSearch(frm) {
	frm = frm.form;
	frm.curPage.value = frm.curResultsPage.value;
	frm.action="search_results.asp";
	frm.submit();
}

function searchKwd(frm) {
	frm = frm.form;
	if (frm.fldsearchkwd.value.length>=2 && frm.fldsearchkwd.value!='Enter Keyword here') {
		frm.catID.value="";
		
		if (frm.searchkwd.value.length==0) {
			frm.searchkwd.value = frm.fldsearchkwd.value;
		}
		else {
			frm.searchkwd.value = frm.searchkwd.value + ' ' + frm.fldsearchkwd.value;
		}
		frm.submit();
	}
	else
		alert("Please type in valid search keywords (longer than 1 character)");
}

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 validateUpdRegForm(frm) {
	if (frm.username.value.length<3) {
		alert("Invalid username (Minimum characters: 3)");
		return false;
	}
	if (frm.password.value.length>0 && frm.password.value.length<3) {
		alert("Invalid password (Minimum characters: 3)");
		return false;
	}
	if (frm.firstname.value.length<2) {
		alert("Invalid first name (Minimum characters: 2)");
		return false;
	}
	if (frm.surname.value.length<2) {
		alert("Invalid surname (Minimum characters: 2)");
		return false;
	}
	if (frm.address1.value.length<2) {
		alert("Invalid address (Minimum characters: 2)");
		return false;
	}
	if (frm.city.value.length<2) {
		alert("Invalid city name (Minimum characters: 2)");
		return false;
	}
	if (frm.postcode.value.length<2) {
		alert("Invalid post code (Minimum characters: 2)");
		return false;
	}
	if (frm.country.value.length<2) {
		alert("Invalid country name (Minimum characters: 2)");
		return false;
	}
	if (frm.businesstype.value.length<2) {
		alert("Invalid business type (Minimum characters: 2)");
		return false;
	}
	if (frm.email.value.length<6) {
		alert("Invalid email address (Minimum characters: 6)");
		return false;
	}
	if (!echeck(frm.email.value)) {	
		return false;
	}
	if (frm.telephone.value.length<2) {
		alert("Missing telephone number");
		return false;
	}
	return true;
}

function validateUpdUserAccountFormAdmin(btn, userID) {
	var frm;
	frm = btn.form;
	
	if (frm["password_" + userID].value.length<3) {
		alert("Invalid password (Minimum characters: 3)");
		return false;
	}
	if (frm["firstname_" + userID].value.length<2) {
		alert("Invalid first name (Minimum characters: 2)");
		return false;
	}
	if (frm["surname_" + userID].value.length<2) {
		alert("Invalid surname (Minimum characters: 2)");
		return false;
	}
	if (frm["address1_" + userID].value.length<2) {
		alert("Invalid address (Minimum characters: 2)");
		return false;
	}
	if (frm["city_" + userID].value.length<2) {
		alert("Invalid city name (Minimum characters: 2)");
		return false;
	}
	if (frm["postcode_" + userID].value.length<2) {
		alert("Invalid post code (Minimum characters: 2)");
		return false;
	}
	if (frm["country_" + userID].value.length<2) {
		alert("Invalid country name (Minimum characters: 2)");
		return false;
	}
	if (frm["businesstype_" + userID].value.length<2) {
		alert("Invalid business type (Minimum characters: 2)");
		return false;
	}
	if (frm["email_" + userID].value.length<6) {
		alert("Invalid email address (Minimum characters: 6)");
		return false;
	}
	if (!echeck(frm["email_" + userID].value)) {	
		return false;
	}
	if (frm["telephone_" + userID].value.length<2) {
		alert("Missing telephone number");
		return false;
	}
	
	frm.userID.value=userID;
	frm.submit();
}

function validateSiteSettings(frm) {
	if (frm.username.value.length<3) {
		alert("Invalid username (Minimum characters: 3)");
		return false;
	}
	if (frm.password.value.length>0 && frm.password.value.length<3) {
		alert("Invalid password (Minimum characters: 3)");
		return false;
	}
	if (frm.firstName.value.length<2) {
		alert("Invalid first name (Minimum characters: 2)");
		return false;
	}
	if (frm.surname.value.length<2) {
		alert("Invalid surname (Minimum characters: 2)");
		return false;
	}
	if (frm.siteName.value.length<1) {
		alert("Invalid Site Title (Minimum characters: 1)");
		return false;
	}
	if (frm.ownersName.value.length<2) {
		alert("Invalid Site Owner's name (Minimum characters: 2)");
		return false;
	}
	if (frm.SMTPHost.value.length<3) {
		alert("Invalid SMTP Host (Minimum characters: 3)");
		return false;
	}
	if (frm.contactEmail.value.length<6) {
		alert("Invalid email address (Minimum characters: 6)");
		return false;
	}
	if (!echeck(frm.contactEmail.value)) {	
		return false;
	}
	return true;
}

function validateRegForm(frm) {
	if (frm.username.value.length<3) {
		alert("Invalid username (Minimum characters: 3)");
		return false;
	}
	if (frm.password.value.length<3) {
		alert("Invalid password (Minimum characters: 3)");
		return false;
	}
	if (frm.firstname.value.length<2) {
		alert("Invalid first name (Minimum characters: 2)");
		return false;
	}
	if (frm.surname.value.length<2) {
		alert("Invalid surname (Minimum characters: 2)");
		return false;
	}
	if (frm.address1.value.length<2) {
		alert("Invalid address (Minimum characters: 2)");
		return false;
	}
		if (frm.city.value.length<2) {
		alert("Invalid city name (Minimum characters: 2)");
		return false;
	}
	if (frm.postcode.value.length<2) {
		alert("Invalid post code (Minimum characters: 2)");
		return false;
	}
	if (frm.country.value.length<2) {
		alert("Invalid country name (Minimum characters: 2)");
		return false;
	}
	if (frm.businesstype.value.length<2) {
		alert("Invalid business type (Minimum characters: 2)");
		return false;
	}
	if (frm.email.value.length<6) {
		alert("Invalid email address (Minimum characters: 6)");
		return false;
	}
	if (!echeck(frm.email.value)) {	
		return false;
	}
	if (frm.telephone.value.length<2) {
		alert("Missing telephone number");
		return false;
	}
	return true;
}

function addSubscription(btn, subsID) {
	var frm;
	frm = btn.form;
	
	frm.subsID.value=subsID;
	
	if (frm["subName_" + subsID].value.length<2) {
		alert("Missing Subscription's name (Min. length: 2 characters)");
		return false;
	}
	if (frm["accessDurationUnit_" + subsID].value=="0" && frm["accessDuration_" + subsID].value!="0") {
		alert("If you specify a subscription duration, you must also set the Time Unit to a value other than None");
		return false;
	}
	if (frm["accessDurationUnit_" + subsID].value!="0" && frm["accessDuration_" + subsID].value=="0") {
		alert("You must either specify a subscription duration or set the Time Unit to None");
		return false;
	}
	if (frm["allowedDownloads_" + subsID].value=="0" && frm["accessDuration_" + subsID].value=="0") {
		alert("Subscriptions can not have no duration AND no download credits specified");
		return false;
	}
	
	frm.submit();
}

function confirmLBDelete(frm, lightBoxID, lightBoxNR, lightBoxName)
{
	var agree=confirm("Are you sure you want to delete Lightbox " + lightBoxNR + " \"" + lightBoxName + "\"?");
	if (agree)
		deleteLB(frm, lightBoxID);
	else
		return false ;
}

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 getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function toggleUsersRow(userID) {
	if (document.getElementById('user_' + userID).style.display=='none') {
		document.getElementById('user_' + userID).style.display='';
	}
	else {
		document.getElementById('user_' + userID).style.display='none';
	}
}

function toggleNewsRow(newsID) {
	if (document.getElementById('editRow_' + newsID).style.display=='none') {
		document.getElementById('editRow_' + newsID).style.display='';
	}
	else {
		document.getElementById('editRow_' + newsID).style.display='none';
	}
}

function toggleCatRow(newsID) {
	if (document.getElementById('editRow_' + newsID).style.display=='none') {
		document.getElementById('editRow_' + newsID).style.display='';
	}
	else {
		document.getElementById('editRow_' + newsID).style.display='none';
	}
}

function deleteNewsItem(newsID) {
	document.newslist.newsID.value=newsID;
	document.newslist.action="deleteFPTagItem.asp";
	document.newslist.submit();
}

function updateNewsItem(newsID) {
	if (document.newslist.elements['copy_' + newsID].value.length>0) {
		document.newslist.newsID.value=newsID;
		document.newslist.active.value=document.newslist.elements['chk_' + newsID].checked;
		document.newslist.url.value=document.newslist.elements['url_' + newsID].value;
		document.newslist.copy.value=document.newslist.elements['copy_' + newsID].value;
		document.newslist.submit();
	}
	else {
		alert("You must write some text before updating");
		document.newslist.elements['copy_' + newsID].focus();
	}
}

function addNewsItem() {
	if (document.newslist.copy_news.value.length>0) {
		document.newslist.action="addFPTag.asp";
		document.newslist.submit();
	}
	else {
		alert("You must write some text before adding it to the list");
		document.newslist.copy_news.focus();
	}
}

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 updateCatItem(catID) {
	if (document.catlist.elements['catName_' + catID].value.length>0) {
		document.catlist.catID.value=catID;
		document.catlist.catOrder.value=document.catlist.elements['catOrderNum_' + catID].checked;
		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 deleteCatItem(catID) {
	document.catlist.catID.value=catID;
	document.catlist.action="deleteCatItem.asp";
	document.catlist.submit();
}

function searchLiveKeywords(fld) {
	var frm;
	var listBox;
	var listBoxSize;
	var searchLength;
	searchLength = fld.value.length;
	frm = fld.form;
	listBox = frm.kwdList;
	listBoxSize = listBox.options.length;

	if (searchLength>0) {
		for (var i=0; i<listBoxSize-1; i++) {
			if (listBox.options[i].value.substring(0, searchLength)==fld.value) {
				listBox.selectedIndex=i;
				break;
			}
		}
	}
	else {
		listBox.selectedIndex=-1;
	}
}

function addKwdToImg(btn) {
	var frm;
	var listBox;
	var listBoxSize;
	var imgKwdList;
	frm = btn.form;
	listBox = frm.kwdList;
	listBoxSize = listBox.options.length;

	imgKwdListFrm = window.parent.document.getElementById("imagelist");
	imgKwdList = imgKwdListFrm["keywords_" + frm.imgId.value];
	
	for (var i=0; i<listBoxSize-1; i++) {
		if (listBox.options[i].selected==true) {
			if (trim(imgKwdList.value)=='') {
				imgKwdList.value = listBox.options[i].value;
			}
			else
			{
				imgKwdList.value = imgKwdList.value + ','+ listBox.options[i].value;
			}
		}
	}
	listBox.selectedIndex=-1;
	frm.searchField.value="";
}

//CATCH ENTER
 function kH(e) {
     var code;
     
     if (!e) var e = window.event
      if (e.keyCode) code = e.keyCode;
      else if (e.which) code = e.which;
     
     if(code==13)
     {
          e.cancelBubble = true;
          if (e.stopPropagation) e.stopPropagation();
          return false;
      }else{
        return true;
      }
}
 
function catchEnter (evt) {
    return kH(evt);
}

//DHTML Window script

var dragapproved=false;
var minrestore=0;
var initialwidth,initialheight;
var ie5=document.all&&document.getElementById;
var ns6=document.getElementById&&!document.all;

function iecompattest(){
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function drag_drop(e){
	if (ie5&&dragapproved&&event.button==1){
		document.getElementById("dwindow").style.left=tempx+event.clientX-offsetx+"px";
		document.getElementById("dwindow").style.top=tempy+event.clientY-offsety+"px";
	}
	else if (ns6&&dragapproved)
	{
		document.getElementById("dwindow").style.left=tempx+e.clientX-offsetx+"px";
		document.getElementById("dwindow").style.top=tempy+e.clientY-offsety+"px";
	}
}

function initializedrag(e){
	offsetx=ie5? event.clientX : e.clientX;
	offsety=ie5? event.clientY : e.clientY;
	document.getElementById("dwindowcontent").style.display="none"; //extra
	tempx=parseInt(document.getElementById("dwindow").style.left);
	tempy=parseInt(document.getElementById("dwindow").style.top);
	
	dragapproved=true;
	document.getElementById("dwindow").onmousemove=drag_drop;
}

function loadwindow(url,width,height,imgId){
	window.frames["keywordFrame"].document.keywordList.imgId.value=imgId;

	if (!ie5&&!ns6) {
		window.open(url,"","width=width,height=height,scrollbars=1");
	}
	else
	{
		document.getElementById("dwindow").style.display='';
		document.getElementById("dwindow").style.width=initialwidth=width+"px";
		document.getElementById("dwindow").style.height=initialheight=height+"px";
		document.getElementById("dwindow").style.left="30px";
		document.getElementById("dwindow").style.top=ns6? window.pageYOffset*1+30+"px" : iecompattest().scrollTop*1+30+"px";
	}
}

function closeit() {
	document.getElementById("dwindow").style.display="none";
}

function stopdrag(){
	dragapproved=false;
	document.getElementById("dwindow").onmousemove=null;
	document.getElementById("dwindowcontent").style.display=""; //extra
}

function addImageItem(btn, imgId) {
	var frm;
	frm = btn.form;
	var listsSelected;
	listsSelected=0;
	var catList;
	catList="";
	
	for (var i=0; i<frm["categories_" + imgId].options.length-1; i++) {
		if (frm["categories_" + imgId].options[i].selected==true) {
			listsSelected=1;
			catList = catList + ',' + frm["categories_" + imgId].options[i].value;
		}
	}
	
	if (frm["imgFile_" + imgId].value.length==0 && imgId==0) {
		alert("You must specify an image filename before adding it to the list");
		frm["imgFile_" + imgId].focus();
	}
	else {
		frm.imageID.value=imgId;
		frm.catList.value=catList;
		frm.submit();
	}
}
