window.onload = jsFnOnload;
window.onclose = jsFnSetSessionCookie;

function jsFnOnload()
{
	if (document.getElementById)
	{
		/* Open external links in new window */
		aLinks = document.getElementsByTagName("a");
		for (aIndex = 0; aIndex < aLinks.length; aIndex++)
		{
/*			if (aLinks[aIndex].href.indexOf('http://192.168.1.21/Equestrian%20Needs/www/') == -1)*/
			if (aLinks[aIndex].href.indexOf('http://www.equestrianneeds') == -1)
			{
				aLinks[aIndex].target = '_blank';
			}
		}
			  
		/* Associate logo's onclick event with home page */
		var logo = document.getElementById("logo");
 		if (logo != null) {
			logo.onclick = function() {
				window.location = 'index.php';
				return false;
			}
		}

		/* Open Login links in new window */
		var link;
		var links = document.getElementsByTagName("a");
		
		for (var i = 0; i < links.length; i++)
		{
			link = links[i];
			
			// Find all links with a class name of "loginlink"
			if (/\bloginlink\b/.exec(link.className))
			{
			 	
				link.onclick = openInNewWindow;
			}
		}

		/* Open Advert Image links in new window */
		var imagelink;
		var images = document.getElementsByTagName("a");		
		
		for (var j = 0; j < images.length; j++)
		{
			imagelink = images[j];
			
			// Find all links with a class name of "loginlink"
			if (/\bproductlink\b/.exec(imagelink.className))
			{
				imagelink.onclick = JSFnImageZoom;
			}
		}
						
		var menu = document.getElementById("topmenu");
 		if (menu != null) 
		{
			var lis = menu.getElementsByTagName("li");
			for(aliindex=0;aliindex<lis.length;aliindex++)
			{
				if(lis[aliindex].className=='level1')
				{
					lis[aliindex].onclick = JSFnMenuLiClick;
					
					lis[aliindex].onmouseover = JSFnMenuLiOver;
					lis[aliindex].onmouseout = JSFnMenuLiOut;
				}
			}
		}		
		
		var aContactForm = document.getElementById('contactform');
		if (aContactForm != null) aContactForm.onsubmit = JSFnValidateContactForm;

		var aEventForm = document.getElementById('eventform');
		if (aEventForm != null) aEventForm.onsubmit = JSFnValidateEventForm;
		
		var aAdvertContactForm = document.getElementById('advertcontactform');
		if (aAdvertContactForm != null) aAdvertContactForm.onsubmit = JSFnValidateAdvertContactForm;
				
		//var aImageZoom = document.getElementById('productlink');
		//if (aImageZoom != null) aImageZoom.onclick = JSFnImageZoom;
		
		var aCheckoutForm = document.getElementById('checkoutform');
		if (aCheckoutForm != null) aCheckoutForm.onsubmit = JSFnValidateCheckout;
		
		var aDeliveryForm = document.getElementById('deliveryinformation');
		if (aDeliveryForm != null) aDeliveryForm.onsubmit = JSFnValidateDelivery;
		
		var aAdvertDetails = document.getElementById('completeform');
		if (aAdvertDetails != null) aAdvertDetails.onsubmit = JSFnValidateAdvertDetails;
		
		var aContactDetails = document.getElementById('contactdetailsform');
		if (aContactDetails != null) aContactDetails.onsubmit = JSFnValidateContactDetails;
			
		var aContactDetails2 = document.getElementById('contactdetailsform2');
		if (aContactDetails2 != null) aContactDetails2.onsubmit = JSFnValidateContactDetails2;

		var aPhotos = document.getElementById('photosform');
		if (aPhotos != null) aPhotos.onsubmit = JSFnValidatePhotos;

		var aPreviewForm = document.getElementById('previewform');
		if (aPreviewForm != null) aPreviewForm.onsubmit = JSFnValidatePreviewForm;
										
		var aRefineButton = document.getElementById('RefineButton');
		if (aRefineButton != null) aRefineButton.style.display = 'none';
	
		var aRefineGroupPage = document.getElementById('CountryFilter');
		if (aRefineGroupPage != null) aRefineGroupPage.onchange = JSFnRefreshGroupPage;
	
		var aSearchForm = document.getElementById('fullsearchform');
		if (aSearchForm != null) aSearchForm.onsubmit = JSFnValidateSearchForm;
		
			
		// Sold Advert / End Advert Check
		var remove = document.getElementsByTagName("a");
 		if (remove != null) 
		{
			for(aremindex=0;aremindex<remove.length;aremindex++)
			{
				if(remove[aremindex].className=='sold')
				{
					remove[aremindex].onclick = JSFnSoldClick;
				}
				if(remove[aremindex].className=='end')
				{
					remove[aremindex].onclick = JSFnEndClick;
				}
				if(remove[aremindex].className=='remove')
				{
					remove[aremindex].onclick = JSFnRemoveClick;
				}								
			}
		}
	}
}

// this deletes the cookie when called
function jsFnSetSessionCookie()
{
	document.cookie = "PHPSESSID=0; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/";
	document.cookie = "loggedin=0; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/";
	document.cookie = "UserID=0; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/";
	document.cookie = "UserName=0; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/";
}

function JSFnSoldClick()
{
	var aSold = confirm("Are you sure you want to mark this advert as Sold?");

	return aSold;
}

function JSFnEndClick()
{
	var aEnd = confirm("Are you sure you want to end this advert?");

	return aEnd;
}

function JSFnRemoveClick()
{
	var aEnd = confirm("Are you sure you want to remove this advert?");

	return aEnd;
}

function JSFnRefreshGroupPage()
{
	var aRefineGroup = document.getElementById('refinegroup');
	if (aRefineGroup != null) aRefineGroup.submit();

}

function openInNewWindow()
{
	var newWindow = window.open(this.getAttribute('href'), '_blank');
	newWindow.focus();
	return false;
}

function JSFnMenuLiOver()
{
	this.className = 'level1 hover';
}

function JSFnMenuLiOut()
{
	this.className = 'level1';
}

function JSFnMenuLiClick()
{
	window.location = this.childNodes[0].href;
	//alert(this.childNodes[0]);
}

function JSFnGoBack()
{
	history.go(-1);
}

function JSFnImageZoom()
{
	var aWindow = window.open(this.href, 'productbig', 'scrollbars=yes, menubar=no, resizeable=no, toolbar=no, width=600px, height=600px')
	aWindow.focus();
	return false;
}

var aContactRequiredFields = new Array ("name","Please enter your name to continue",
										"email","Please enter your email address to continue",
										"subject","Please enter your message subject to continue",
										"comments","Please enter your message to continue"
										);

function JSFnValidateContactForm()
{
	return JSFnValidateForm(aContactRequiredFields);
}

var aEventRequiredFields = new Array ("YourName","Please enter your name to continue",
										"YourEmail","Please enter your email address to continue",
										"EventTitle","Please enter a event title to continue",
										"Description","Please enter a description to continue"
										);

function JSFnValidateEventForm()
{
	return JSFnValidateForm(aEventRequiredFields);
}

var aAdvertContactRequiredFields = new Array ("Name","Please enter your name to continue",
										"Email","Please enter your email address to continue",
										"Message","Please enter your message to continue"
										);

function JSFnValidateAdvertContactForm()
{
	return JSFnValidateForm(aAdvertContactRequiredFields);
}

var aAdvertDetailsRequiredFields = new Array ("Title","Please enter a Title to continue",
										"grouppl11","Please enter a Country to continue",
										"Height","Please enter a Height to continue",
										"Age","Please enter an Age to continue",
										"grouppl15","Please enter a Gender to continue",
										"grouppl62","Please select a Classified Advert type to continue"
										);

function JSFnValidateAdvertDetails()
{
	// ************** Find out how many checkboxes have been selected **************** //
		var inputs = window.document.getElementsByTagName('input');
		
		var aCheckedCount = 0;
		var aCheckboxesPresent = 0;
		
		// Loop through all input elements
		for(var i=0; i < inputs.length; i++)
		{
				if (inputs[i].type.toLowerCase() == 'checkbox')
				{ 
					if(inputs[i].checked) aCheckedCount++; //if the element is a checkbox
					
					aCheckboxesPresent = 1;
				}
		}
		
		if(aCheckboxesPresent == 1)
		{	
			if(aCheckedCount == 0)
			{
				alert("Please select at least 1 Activity");
				return false;
			}
			
			if(aCheckedCount > 5)
			{
				alert("You can only select a maximum of 5 Activities");
				return false;
			}
		}
		// ******************************************************************************* //
		
			
	// Validate Age
	var aAge = document.getElementById("Age");
	
	if(aAge != null)
	{
		if(aAge.value > 50 || aAge.value < 1)
		{
			alert("Age has to be greater than 0 and less than 50 years");
			
			return false;
		}
		
		if(IsNumeric(aAge.value) == false)
		{
			alert("Age must only consist of numbers.");
			
			return false;
		}		
	}
	
	// Validate Price
	var aPrice = document.getElementById("Price");
	
	if(aPrice != null)
	{
		if(aPrice.value == '')
		{
			//alert("Where the price has been left blank, the ad will be returned in the Over 10,000 category.");
			alert("We recommend you give a guide price to help prospective buyers.");
		}
		
// 		if(IsNumeric(aPrice.value) == false)
// 		{
// 			alert("Price must only consist of numbers.");
// 			
// 			return false;
// 		}		
	}

	// Validate Height
	var aHeight = document.getElementById("Height");
	
	if(aHeight != null)
	{	
		if(IsNumeric(aHeight.value) == false)
		{
			alert("Height must only consist of numbers (Decimal places are allowed).");
			
			return false;
		}
		else if(aHeight.value > 20)
		{
			alert("Height must be less that 20 hands.");
			
			return false;
		}
		else if(aHeight.value.indexOf('.') > 0)
		{
			var aHeightDecimalPoint = aHeight.value.split(".");
			
			if(aHeightDecimalPoint[1] != 0 && aHeightDecimalPoint[1] != 1 && aHeightDecimalPoint[1] != 2 && aHeightDecimalPoint[1] != 3)
			{			
				alert("Height decimal place can only be .0, .1, .2 or .3");
				
				return false;
			}
		}		
	}
	
	return JSFnValidateForm(aAdvertDetailsRequiredFields);
}

function IsNumeric(Text)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	
	for (i = 0; i < Text.length && IsNumber == true; i++) 
	{ 
		Char = Text.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}
   
//*** Validate Search Form ***//
var aSearchFormRequiredFields = new Array ("country","Please select a Country to continue");

function JSFnValidateSearchForm()
{
	var aQuickSearch = document.getElementById("QuickSearch");
	
	if(aQuickSearch.value == '')
	{	
		$aResult = JSFnValidateForm(aSearchFormRequiredFields);
		// ************** Find out how many checkboxes have been selected **************** //
		if($aResult == true)
		{
			var cbinputs = document.getElementsByTagName("input");
			
			// Count how many of the Activity checkboxes have been selected
			var aCheckedCount = 0;
	 		if (cbinputs != null) 
			{
				for(aremindex=0;aremindex<cbinputs.length;aremindex++)
				{
					if(cbinputs[aremindex].className=='cb22')
					{
						if(cbinputs[aremindex].checked) aCheckedCount++; //if the element is a checkbox
					}
				}
			}
			
			// Count how many of the Advert Type checkboxes have been selected
			var aCheckedCountType = 0;
	 		if (cbinputs != null) 
			{
				for(atypeindex=0;atypeindex<cbinputs.length;atypeindex++)
				{
					if(cbinputs[atypeindex].className=='cb')
					{
						if(cbinputs[atypeindex].checked) aCheckedCountType++; //if the element is a checkbox
					}
				}
			}
								
			var aClassifiedAdsCheckBox = document.getElementById("adverttype_507");
			
			if(aClassifiedAdsCheckBox.checked && aCheckedCountType == 1)
			{
				
			}
			else
			{
				if(aCheckedCount == 0 || (aClassifiedAdsCheckBox.checked && aCheckedCountType > 1 && aCheckedCount == 0))
				{
					alert("Please select at least 1 Activity");
					$aResult = false;
				}
			}
	
			if($aResult == true)
			{
				if(aCheckedCount > 5)
				{
					alert("You can only select a maximum of 5 Activities");
					$aResult = false;
				}
			}
		}
		// ******************************************************************************* //
	}
	else $aResult = true;
	
	return $aResult;
}
//****************************************************************************************************************

var aContactDetailsRequiredFields = new Array ("EmailAddress","Please enter an Email Address to continue",
										"FirstName","Please enter a Name to continue",
										"TelephoneNo","Please enter a Telephone Number to continue",
										"PasswordAdd","Please enter a Password to continue",
										"PasswordRepeatAdd","Please enter your Confirm Password to continue"
										);

function JSFnValidateContactDetails()
{
	var aTradeName = document.getElementById("TradeName");
	var aTradeCheckBox = document.getElementById("Trade");
	
	if(aTradeName.value != '')
	{
		if(!(aTradeCheckBox.checked))
		{
			alert("You have entered a Trade Name so you also need to check the box 'Is this a Trade Advert?'");
			return false;			
		}
	}
	else if(aTradeName.value == '' && aTradeCheckBox.checked)
	{
		alert("You have entered checked the box 'Is this a Trade Advert?' so you also need to enter a Trade Name");
		return false;			
	}
		
	return JSFnValidateForm(aContactDetailsRequiredFields);
}

var aContactDetailsRequiredFields2 = new Array ("EmailAddress","Please enter an Email Address to continue",
										"TelephoneNo","Please enter a Telephone Number to continue",
										"FirstName","Please enter a Name to continue"
										);

function JSFnValidateContactDetails2()
{
	var aTradeName = document.getElementById("TradeName");
	var aTradeCheckBox = document.getElementById("Trade");
	
	if(aTradeName.value != '')
	{
		if(!(aTradeCheckBox.checked))
		{
			alert("You have entered a Trade Name so you also need to check the box 'Is this a Trade Advert?'");
			return false;			
		}
	}
	else if(aTradeName.value == '' && aTradeCheckBox.checked)
	{
		alert("You have entered checked the box 'Is this a Trade Advert?' so you also need to enter a Trade Name");
		return false;			
	}
			
	return JSFnValidateForm(aContactDetailsRequiredFields2);
}

function JSFnValidatePhotos()
{
	var aPermissionToUseImages = document.getElementById("PermissionToUseImages");

	var aPhoto1 = document.getElementById("Photo1");
	var aPhoto2 = document.getElementById("Photo2");
	var aPhoto3 = document.getElementById("Photo3");
	var aPhoto4 = document.getElementById("Photo4");
	
	if(aPhoto1.value != '' || aPhoto2.value != '' || aPhoto3.value != '' || aPhoto4.value != '')
	{
		if (aPermissionToUseImages.type.toLowerCase() == 'checkbox')
		{ 
			if(!(aPermissionToUseImages.checked))
			{
				alert("Please check the box to say that you have permission to use images.");
				return false;			
			}
		}
	}

	var aAgreeTerms = document.getElementById("AgreedTC");
	if(!(aAgreeTerms.checked))
	{
		alert("Please tick the box to confirm you have accepted our terms and conditions'");
		return false;			
	}	
}

function JSFnValidatePreviewForm()
{
	var aAgreeTerms = document.getElementById("AgreedTC");
	if(!(aAgreeTerms.checked))
	{
		alert("Please tick the box to confirm you have accepted our terms and conditions'");
		return false;			
	}
}

CheckoutRequiredFields = new Array ("terms","You must tick to say that you agree to our terms and conditions in order to checkout.",
									"privacy","You must tick to say that you have read our privacy policy in order to continue.");
function JSFnValidateCheckout()
{
	return JSFnValidateForm(CheckoutRequiredFields);
}

DeliveryRequiredFields = new Array ("CustomerName","You must enter your name to continue.",
									"CustomerEmail","You must enter your email address to continue.",
									"DeliveryAddressLine1","You must enter the first line of your delivery address to continue.",
									"DeliveryAddressLine4","You must enter your delivery city/town to continue.",
									"DeliveryPostCode","You must enter your delivery postcode to continue.");

DeliveryBillingRequiredFields = new Array ("BillingName","You must enter your billing name to continue.",									   
										   "BillingAddressLine1","You must enter the first line of your billing address to continue.",
										   "BillingAddressLine4","You must enter your billing city/town to continue.",
										   "BillingPostCode","You must enter your billing postcode to continue.");

function JSFnValidateDelivery()
{
	aResult = JSFnValidateForm(DeliveryRequiredFields);

	if (aResult != false)
	{
		var aBillingFirstName = document.getElementById('BillingName');
		var aBillingAddress = document.getElementById('BillingAddressLine1');
		var aBillingCity = document.getElementById('BillingAddressLine4');
		var aBillingPostcode = document.getElementById('BillingPostcode');
		if ((aBillingFirstName.value != '') || (aBillingAddress.value != '') || (aBillingCity.value != '') || (aBillingPostcode.value != ''))
		{
			aResult = JSFnValidateForm(DeliveryBillingRequiredFields);	
		}
	}
	
	return aResult;
}

function JSFnValidateForm(aRequiredFields)
{
	for (aIndex = 0; aIndex < aRequiredFields.length; aIndex = aIndex + 2)
	{
		currElement = document.getElementById(aRequiredFields[aIndex]);
		if (currElement != null)
		{
			if  (   (   (currElement.type == 'text')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'password')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'checkbox')
				     && (currElement.checked == false))
				 || (   (currElement.type == 'file')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'textarea')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'select-one')
				     && (currElement.value == '')))
			{
				alert(aRequiredFields[aIndex + 1]);
				return false;
			}
			else if (currElement.type == 'radio')
			{
				aIndex = aIndex + 2;
				if (!currElement.checked)
				{
					currElement = document.getElementById(aRequiredFields[aIndex]);
					if ((currElement.type == 'radio') && (!currElement.checked))
					{
						alert(aRequiredFields[aIndex + 1]);
						return false;
					}
				}
			}
		}
	}
	return true;
}

function textCounter(field, countfield, maxlimit)
{
	if (field.value.length > maxlimit) field.value = field.value.substring(0, maxlimit);
	else countfield.value = maxlimit - field.value.length;
}

function showText(aParagraphID)
{
		var aParagraph = document.getElementById(aParagraphID);

		if ((aParagraph.value != ''))
		{
			aParagraph.style.display = 'block';
		}
}

function hideText(aParagraphID)
{
		var aParagraph = document.getElementById(aParagraphID);

		if ((aParagraph.value != ''))
		{
			aParagraph.style.display = 'none';
		}
}