function validateContact(){
	with (document.frmContact){
		var problem = 0
		var message = "";
		//var message = "Your message could not be submitted for the following reasons:\n"
		if (txtName.value==""){
			problem++
			message+="\nPlease enter your name."
		}
		if (!verifyEmail(txtEmail.value)){
			problem++
			message+="\nPlease enter your valid email address."
		}
		if (txtMessage.value==""){
			problem++
			message+="\nPlease enter your message."
		}
		if (problem > 0){
			alert(message);
			return false;
		}
		else{
			return true;
		}
	}
}

function setSearchIndex(paramCategoryID){
	if (paramCategoryID==4){
		with(document.main){
			if(category2ID.options[category2ID.selectedIndex].text.toLowerCase() == "board games" || 
			category2ID.options[category2ID.selectedIndex].text.toLowerCase() == "jigsaws"){
				searchIndex.value = "Toys"
			}
			else{
				searchIndex.value = "VideoGames"
			}
			platformText.value = category2ID.options[category2ID.selectedIndex].text
		}
	}
}

function validateQuickList(){
	var problem = 0
	var message = ""
	with(document.main){
		if (Title.value==""){
			problem++
			message+="\nTitle is required."
		}		
		if(document.main.Author){
			if (Author.value==""){
				problem++
				message+="\nAuthor is required."
			}
		}
		if(document.main.Binding){
			if (Binding.options[Binding.selectedIndex].value=="0"){
				problem++
				message+="\nFormat is required."
			}
		}
		if(document.main.category2ID){
			if (category2ID.options[category2ID.selectedIndex].value=="0"){
				problem++
				message+="\nPlatform is required."
			}
		}
	}
	if (problem > 0){
		alert(message);
		return false;
	}
	else{
		return true;
	}
}

function showHideSubCat3Selection(paramCat3Selection){
	//alert("func reached")
	var cat2SelectedID, x, k;
	var lvboolDisplay = false;
	var lvboolSelected = false;
	j = 0;
	k = 1; //The zero option is already used for the "Select..."
	with (document.main){
		cat2SelectedID = category2ID.options[category2ID.selectedIndex].value;
		
		for (x=0; x < gvarrCategory3.length; x++){			
			if (gvarrCategory3[x][2]==cat2SelectedID.toString()){
				//alert("Match found")
				//The parentCategoryID of category 3 matches the ID of category 2
				// so populate the 3rd sub category selection box and display it.
				lvboolDisplay = true;
				//if(gvarrCategory3[x][0] == paramCat3Selection){
					//Current 3rd cat ID matches the previously selected ID (for listing mods)
					//lvboolSelected = true;					
					category3ID.options[k] = new Option(gvarrCategory3[x][1],gvarrCategory3[x][0]);					
					//category3ID.selectedIndex = k; //Set selected index explicitly
				//}
				//else{
					//category3ID.options[k] = new Option(gvarrCategory3[x][1],gvarrCategory3[x][0]);
				//}
				if(gvarrCategory3[x][0] == paramCat3Selection){
					//Current 3rd cat ID matches the previously selected ID (for listing mods)
					category3ID.selectedIndex = k; //Set selected index explicitly
				}
				//lvboolSelected = false;
				k++;
			}
		}		
		if (lvboolDisplay){
			document.getElementById("rowCategory3").style.display = "";
		}
		else{
			document.getElementById("rowCategory3").style.display = "none";
			category3ID.selectedIndex = 0 //Nullify any selection already made.
		}		
		//alert("category3ID.selectedIndex: " + category3ID.selectedIndex + "\ncategory3ID.options[category3ID.selectedIndex].value: " + category3ID.options[category3ID.selectedIndex].value + "\ncategory3ID.options[category3ID.selectedIndex].text" + category3ID.options[category3ID.selectedIndex].text)
	}	
}

function showHideSubCatGaming(){
	with(document.main){
		if(category2ID.options[category2ID.selectedIndex].text.toLowerCase() == "board games" || 
		category2ID.options[category2ID.selectedIndex].text.toLowerCase() == "jigsaws"){
			document.getElementById("subCatGaming").style.display = "none"
		}
		else{
			document.getElementById("subCatGaming").style.display = ""
		}
	}
}

function validatePostal(){
	with (document.postalDetails){
		var problem = 0
		var message = "Your postal details could not be submitted for the following reasons:\n"
		if (street.value==""){
			problem++
			message+="\nStreet address or PO Box is required."
		}
		if (city.value==""){
			problem++
			message+="\nCity is required."
		}
		if (postCode.value==""){
			problem++
			message+="\nPost code is required."
		}				
		if (problem > 0){
			alert(message);
			return false;
		}
		else{
			return true;
		}
	}
}

function premiumOnly(){
	if(confirm('The feature you have selected is reserved for Premium Members.\n\nDo you want to upgrade to Premium Membership now?')){
		location.href='membership-premium.asp';
		return false;
	}
	else{
		return false;
	}
}

function validateWishlist(){
	with (document.Main){
		var problem = 0
		var messageText = ""		
		if (categoryID.options[categoryID.selectedIndex].value == "4" || categoryID.options[categoryID.selectedIndex].value == "5"){
			if (ItemTitle.value==""){
				problem++
				messageText+="Title is required."
			}		
		}
		else{
			if (categoryID.options[categoryID.selectedIndex].value == "0")  {
				problem++
				messageText+="Please select an item type."
			}
			else{
				if (ItemTitle.value=="" && ItemAuthor.value==""){
					problem++
					messageText+="Title and/or "+gvstrAuthorLabel+" is required."
				}	
			}
		}
		
		if (problem > 0){
			alert(messageText);
			return false;
		}
		else{
			return true;
		}
	}
}

var gvstrAuthorLabel = ""
function setWishlistAddLabels(){
	//
	document.getElementById("xyzorange").style.display = ''
	with (document.Main){
		switch(categoryID.options[categoryID.selectedIndex].value){
				case "1":
					document.getElementById("labelItemTitle").innerHTML = "Title:";
					document.getElementById("labelItemAuthor").innerHTML = "Author:";
					gvstrAuthorLabel = "Author"
					break;
					
				case "2":
					document.getElementById("labelItemTitle").innerHTML = "Title:";
					document.getElementById("labelItemAuthor").innerHTML = "Artist:";
					gvstrAuthorLabel = "Artist"
					break;		
					
				case "3":
					document.getElementById("labelItemTitle").innerHTML = "Title:";
					document.getElementById("labelItemAuthor").innerHTML = "Director:";
					gvstrAuthorLabel = "Director"
					break;		
				
				case "4":
					document.getElementById("labelItemTitle").innerHTML = "Title:";
					document.getElementById("labelItemAuthor").innerHTML = "Publisher:";
					document.getElementById("xyzorange").style.display = 'none'
					break;		
				
				case "5":
					document.getElementById("labelItemTitle").innerHTML = "Title:";
					document.getElementById("labelItemAuthor").innerHTML = "Publisher:";
					document.getElementById("xyzorange").style.display = 'none'
					break;		
				
		}
		
	}
}

function addToWatchList(paramForm){
	paramForm.action='code/doaction.asp';	
}

function listSameItem(paramForm, paramCategoryID){
	var lvstrAction = ""
	paramForm.doaction.value = "listSameItem";	
	with (document.requestItem){
		switch(paramCategoryID){
			case 1:
				lvstrAction = "list-book-relist.asp";
				break;
				
			case 2:
				lvstrAction = "list-music-relist.asp";
				break;		
				
			case 3:
				lvstrAction = "list-movie-relist.asp";
				break;		
			
			case 4:
				lvstrAction = "list-game-relist.asp";
				break;		
			
			case 5:
				lvstrAction = "list-magazine-relist.asp";
				break;				
		}		
	}
	paramForm.action=lvstrAction;	
}

function validateForumPost(){
	with (document.Main){
		var problem = 0
		var messageText = ""		
		if (title.value==""){
			problem++
			messageText+="A title is required."
		}		
		if (problem > 0){
			alert(messageText);
			return false;
		}
		else{
			return true;
		}
	}
}

function limitText(limitField, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	}
}

function showHideSubCatLinks(paramParentCategoryID){
	var lobjLinksTable
	if(document.getElementById("subCatLinks"+paramParentCategoryID)){
		lobjLinksTable = document.getElementById("subCatLinks"+paramParentCategoryID)
		if(lobjLinksTable.style.display=="none"){
			lobjLinksTable.style.display="";
			document.getElementById("subCatNavTree"+paramParentCategoryID).src="Images/minus.gif";
			createCookie("subCatNavTreeView"+paramParentCategoryID,"on",90)
			location.href="category-view.asp?level=1&categoryid=" + paramParentCategoryID
			//alert(readCookie("subCatNavTreeView"+paramParentCategoryID))
		}
		else{
			lobjLinksTable.style.display="none";
			document.getElementById("subCatNavTree"+paramParentCategoryID).src="Images/plus.gif";
			createCookie("subCatNavTreeView"+paramParentCategoryID,"off",90)
			//alert(readCookie("subCatNavTreeView"+paramParentCategoryID))
		}
	}
}

function populateSelect()
{
	//if (!optionTest) return;
	var box = document.main.categoryID;
	var number = box.options[box.selectedIndex].value - 1;
	//alert(number)
	//alert(gvarrCategory2[number].length)
	if (!number) return;
	var list = gvarrCategory2[number];
	var box2 = document.main.category2ID;
	box2.options.length = 0;
	for(i=0;i<gvarrCategory2[number].length;i++)
	{
		box2.options[i] = new Option(gvarrCategory2[number][i][1],gvarrCategory2[number][i][0]);
	}
}

function setFormElementValue(paramFormName,paramElementName,paramValue){
	//alert("document."+paramFormName+"."+paramElementName);
		var lvobjFormElement = eval("document."+paramFormName+"."+paramElementName);
		lvobjFormElement.value = paramValue;
}

function displaySubCategory(paramElement){
	var lvobjElement = paramElement;
	var lvintCategoryIndex = right(lvobjElement.name,1);
	if (!IsNumeric(lvintCategoryIndex)){
		lvintCategoryIndex=1
	}
	lvintCategoryIndex++
	if(lvobjElement.options[lvobjElement.selectedIndex].value!=0){
		document.getElementById("rowCategory"+lvintCategoryIndex.toString()).style.display="block";	
	}
	else{
		document.getElementById("rowCategory"+lvintCategoryIndex.toString()).style.display="none";	
	}
}

function validateList(paramTopCategory){
	with (document.main){
		var problem = 0
		var message = ""
		if (category2ID.options[category2ID.selectedIndex].value=="0"){
			problem++
			if(paramTopCategory=='gaming'){
					message+="\nPlease select a platform."
			}
			else{
				message+="\nPlease select a sub-category."
			}
		}
		if(document.main.category3ID){
			if (document.getElementById("rowCategory3").style.display=="" && category3ID.options[category3ID.selectedIndex].value=="0"){
			//If the row containing the 3rd level selection isn't hidden and no selection has been made...
			problem++				
			message+="\nPlease select a second sub-category."
		}
		}
		if (Title.value==""){
			problem++
			message+="\nTitle is required."
		}		
		if(paramTopCategory!='gaming' && paramTopCategory!='magazine'){ //Gaming/mags has no author required, otherwise...
			if (Author.value==""){
				problem++
				if(paramTopCategory=='music'){
						message+="\nArtist is required."
				}
				if(paramTopCategory=='movie'){
						message+="\nDirector is required."
				}
				else{
					message+="\nAuthor is required."
				}
			}
		}
		if(paramTopCategory == 'magazine'){
			if(publicationYear.options[publicationYear.selectedIndex].value=="0"){
				problem++
				message+="\nPublication year is required."				
			}
		}
		if (conditionID.options[conditionID.selectedIndex].value=="0"){
			problem++
			message+="\nPlease select a condition description."
		}
		if (Binding.options[Binding.selectedIndex].value=="0"  && 
		category2ID.options[category2ID.selectedIndex].text.toLowerCase() != "board games" && 
		category2ID.options[category2ID.selectedIndex].text.toLowerCase() != "jigsaws"){
			if(paramTopCategory != 'magazine'){
				problem++
				if(paramTopCategory=='music'){
						message+="\nPlease select a format."
				}
				else if(paramTopCategory=='movie'){
						message+="\nPlease select a format."
				}
				else if(paramTopCategory=='gaming'){											
					message+="\nPlease select a sub-category."
				}			
				else{
					message+="\nPlease select a binding type."
				}
			}
			
		}
		if (Description.value==""){
			problem++
			message+="\nA description is required."
		}
		if (problem > 0){
			alert(message);
			return false;
		}
		else{
			return true;
		}
	}
}

function listPubliclyConfirm(paramFormName){
	var lvobjForm = eval("document."+paramFormName)
	var lvstrWarningText = ""
	lvstrWarningText += "Are you sure you want to make this item available for swapping?"
	if(confirm(lvstrWarningText)){
		lvobjForm.action="code/doaction.asp?doaction=listPublicly";
		lvobjForm.submit();
	}
}

function deleteItemConfirm(paramFormName,paramCancelsFreePoints){
	var lvobjForm = eval("document."+paramFormName)
	var lvstrWarningText = ""
	if (paramCancelsFreePoints){
		lvstrWarningText = "WARNING!\n\nDeleting this item will cancel the free points you earned for listing items.\n\n"
	}
	lvstrWarningText += "Are you sure you want to permanently delete this item?"
	if(confirm(lvstrWarningText)){
		lvobjForm.action="code/doaction.asp?doaction=deleteItem";
		lvobjForm.submit();
	}
}


function withdrawListing(paramFormName,paramCancelsFreePoints){
	var lvobjForm = eval("document."+paramFormName)
	var lvstrWarningText = ""
	if (paramCancelsFreePoints){
		lvstrWarningText = "WARNING!\n\nMoving this item to your shelf will cancel the free points you earned for listing items.\n\n"
	}
	lvstrWarningText += "Are you sure you want to move this item to your shelf?\n\nYou can change it back to a public listing later."
	if(confirm(lvstrWarningText)){
		lvobjForm.action="code/doaction.asp?doaction=withdrawListing";
		lvobjForm.submit();
	}
}

function autoLoginWarning(){
	var lvstrWarning="Ticking this box means that you will no longer need to log in\n"+
	"when visting Swapclub from this computer.\n\n"+
	"You can cancel this setting at any time by logging out.\n\n"+
	"Avoid using this option unless you are the only person\n"+
	"who has access to this computer.\n\n"+
	"IMPORTANT:\n"+"Using this option means that you will be liable for the \n"+
	"actions of anyone who uses your login to access Swapclub."  
	if(document.login.persistLogin.checked){
		if (!confirm(lvstrWarning)){
			document.login.persistLogin.checked=false;
		}
	}
}

function validateRegistrationConfirmation(){
	if(!document.main.terms.checked){
		alert("Please indicate your acceptance of the terms and conditions to proceed.");
	}
	else{
		document.main.submit()
	}
}

function setCitySelection(paramCountry){
	//alert("setCitySelection FUNCTION");
	if(paramCountry=="New Zealand"){
			document.getElementById("NZSwap_Towns").style.display="";			
			document.getElementById("CityGeneric").style.display="none";
	}
	else{
		document.getElementById("NZSwap_Towns").style.display="none";
		document.getElementById("CityGeneric").style.display="";
	}
	//alert("document.getElementById(NZSwap_Towns).style.display: "+document.getElementById("NZSwap_Towns").style.display);
}

function killIllegalCharacters(val) {
	var mikExp = /[$\\@\\\#%\^\&\*\(\)\[\]\.\;\:\'\"\-\<\>\+\_\{\}\`\~\=\|]/;
	var strPass = val.value;
	var strLength = strPass.length;
	var lchar = val.value.charAt((strLength) - 1);
	if(lchar.search(mikExp) != -1) {
		alert("Sorry, character "+lchar+" is not allowed.")
		var tst = val.value.substring(0, (strLength) - 1);
		val.value = tst;
	}
}

function validateJoin(){
	with (document.main){
		var problem = 0
		var message = "Your registration could not be submitted for the following reasons:\n"
		if (FirstName.value==""){
			problem++
			message+="\nFirst name is required."
		}
		if (LastName.value==""){
			problem++
			message+="\nLast name is required."
		}
		if (!verifyEmail(Email.value)){
			problem++
			message+="\nA valid email address is required."
		}
		if (containsDoubleDash(Email.value)){
			problem++
			message+="\nEmail address cannot contain '--'."
		}
		if (Username.value.length<3){
			problem++
			message+="\nUsername must be at least 3 characters."
		}
		if (PWord.value.length<6){
			problem++
			message+="\nPassword must be at least 6 characters."
		}
		if (PWord.value != PWordConfirm.value){
			problem++;
			message=message + "\nThe password you entered does not match the confirmation you provided.";
		}
		if (document.getElementById("CityGeneric").style.display=="block" && CityOther.value==""){
			problem++;
			message=message + "\nCity/Town is required.";
		}
		if (problem > 0){
			alert(message);
			return false;
		}
		else{
			return true;
		}
	}
}

function containsDoubleDash(paramString){
	if (paramString.indexOf("--") > -1){
		return true;
	}
	else{
		return false;
	}
}

function validateProfileUpdate(){
	with (document.main){
		var problem = 0
		var message = "Your profile could not be updated for the following reasons:\n"
		if (FirstName.value==""){
			problem++
			message+="\nFirst name is required."
		}
		if (LastName.value==""){
			problem++
			message+="\nLast name is required."
		}
		if (!verifyEmail(Email.value)){
			problem++
			message+="\nA valid email address is required."
		}		
		if (PWord.value.length<6){
			problem++
			message+="\nPassword must be at least 6 characters."
		}
		if (PWord.value != PWordConfirm.value){
			problem++;
			message=message + "\nThe password you entered does not match the confirmation you provided.";
		}
		if (document.getElementById("CityGeneric").style.display=="" && CityOther.value==""){
			problem++;
			message=message + "\nCity/Town is required.";
		}
		if (problem > 0){
			alert(message);
			return false;
		}
		else{
			return true;
		}
	}
}

function validateItemImageUpload(){
	with (document.main){
		var problem = 0
		var message=""
		if (image1.value.length>0 && image1.value.length<5){
			problem++;
			message=message + "\nSelect a valid image file or leave this field blank if you don't have an image to upload.";
		}
		if (right(image1.value.toString().toLowerCase(),4)!=".jpg" && image1.value.length>=5){
			problem++;
			message=message + "\nYou may only upload .jpg image files.";
		}
		if (problem > 0){
			alert(message);
			return false;
		}
		else{
			if(image1.value.length>4){
				document.getElementById('message').innerHTML='Uploading image, please wait...';
			}
			return true;
		}
	}
}


function verifyEmail(addr) { 
    var atPos = addr.indexOf('@'); 
    var dotPos = addr.lastIndexOf('.') 
    if (atPos>0 && dotPos>atPos+1) return true; 
    else
    	return false;
} 

function left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

<!--
var popWin = null 
var winCount = 0
var winName = "popWin"
function openPopWin(winURL, winWidth, winHeight, winFeatures, winLeft, winTop){
  var d_winLeft = 100  // pixels from screen left to window left
  var d_winTop = 30   // pixels from screen top to window top
  winName = "popWin" + winCount++ 
  closePopWin()           // close any previously opened pop-up
  if (openPopWin.arguments.length >= 4)  
    winFeatures = "," + winFeatures
  else 
    winFeatures = "" 
  if (openPopWin.arguments.length == 6)  
    winFeatures += getLocation(winWidth, winHeight, winLeft, winTop)
  else
    winFeatures += getLocation(winWidth, winHeight, d_winLeft, d_winTop)
  popWin = window.open(winURL, winName, "width=" + winWidth 
           + ",height=" + winHeight + winFeatures)
  }
function closePopWin(){    // close pop-up window if open 
  if (navigator.appName != "Microsoft Internet Explorer" 
      || parseInt(navigator.appVersion) >=4) //don't close if early IE
    if(popWin != null) if(!popWin.closed) popWin.close() 
  }
function getLocation(winWidth, winHeight, winLeft, winTop){
  return ""
  }
//-->

// Netscape 4+ and IE 4+
<!--
function getLocation(winWidth, winHeight, winLeft, winTop){
  var winLocation = ""
  if (winLeft < 0)
    winLeft = screen.width - winWidth + winLeft
  if (winTop < 0)
    winTop = screen.height - winHeight + winTop
  if (winTop == "cen")
    winTop = (screen.height - winHeight)/2 - 20
  if (winLeft == "cen")
    winLeft = (screen.width - winWidth)/2
  if (winLeft>0 & winTop>0)
    winLocation =  ",screenX=" + winLeft + ",left=" + winLeft	
                + ",screenY=" + winTop + ",top=" + winTop
  else
    winLocation = ""
  return winLocation
  }
//-->

function IsNumeric(sText){
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char; 
   for (i = 0; i < sText.length && IsNumber == true; i++){ 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1){
         IsNumber = false;
      }
   }
   return IsNumber;
}



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 readCookie(name){
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name){
	createCookie(name,"",-1);
}
