function CheckOptions(obj) {
	var result=true;

	if (result && obj.size!=null && obj.size_required!=null ) {
		if (obj.size.options[obj.size.selectedIndex].value.length==0) {
			alert(obj.size_required.value);
			result=false;
		}
	}

	if (result && obj.color!=null && obj.color_required!=null ) {
		if (obj.color.options[obj.color.selectedIndex].value.length==0) {
			alert(obj.color_required.value);
			result=false;
		}
	}

	if (result && obj.specialoptions!=null && obj.specialoptions_required!=null ) {
	   if(obj.specialoptions.options)
	   {
			if (obj.specialoptions.options[obj.specialoptions.selectedIndex].value.length==0) {
				alert(obj.specialoptions_required.value);
				result=false;
			}
		}
		else
		{
			tmp_ischecked = false;
		    for(ti = 0; ti < obj.specialoptions.length; ti++)
			{
				if (obj.specialoptions[ti].checked == true)
				{
					tmp_ischecked = true;
				}
			}
			if(!tmp_ischecked)
			{
				alert(obj.specialoptions_required.value);
				result=false;
			}
		}
		
	}

	if (result && obj.oneclick!=null) {
		result = confirm(obj.oneclick.value);
	}
	
	if(result)
	{
		result = Check_Durations(obj);
	}
	if (document.le_form && document.le_form.oneclick_invis && result == true)
	{
		window.open("/mall/formtest/hold.htm","oneclick","toolbars=no,width=100,height=100,left=3000,top=3000");
		document.le_form.submit();
		alert("The product has been added to the basket.");		
		result=false;
	}
	return result;
}

function checkLength(element, maxChars)
{
	if(element.value.length > maxChars)
	{
		element.value = element.value.substring(0, maxChars);
		alert("Please enter upto a maxiumum of " + maxChars + " characters.");
		result = false;
	}
}

// checks that any duration fields are within min and max value ranges.
// input - form object
// output - true if fine, false if any duration object not within bounds. 
//          fuction displays alerts for each failed duration not within bounds.
//
function Check_Durations(obj) {
	var i,j;
	var detailsFound = 0;
	var details = new Array();
	var detailValue;
	var maxDurations;
	var maxList;
	var minDurations;
	var minList;
	var retStatus = true;
	i=0;

	// loop throught the elements of the form to find the duration details
	while( i < obj.elements.length)
	{
		//find the details that are durations.
		var position = obj.elements[i].name.search(/_duration/i);
		if (position != -1)
		{
			details[detailsFound] = obj.elements[i].name.substr(0,(position));
			detailsFound++;
		}
		i++;
	}
	// loop through the details found 
	i=0;
	
	while( i < details.length)
	{
		tmp_val = eval("obj." + details[i] + "_duration").value;
		tmp_duration = 	tmp_val.split("|");
		tmp_field_name = tmp_duration[5];
	
		// Get current duration values
		if(eval("obj." + details[i] + "_years"))
		{
			selTemp = eval("obj." + details[i] + "_years");
			selected_years = selTemp.options[selTemp.selectedIndex].value;
		}
		else
			selected_years = 0;
		if(eval("obj." + details[i] + "_months"))
		{
			selTemp = eval("obj." + details[i] + "_months");
			selected_months = selTemp.options[selTemp.selectedIndex].value;
		}
		else
			selected_months = 0;
		if(eval("obj." + details[i] + "_weeks"))
		{
			selTemp = eval("obj." + details[i] + "_weeks");
			selected_weeks = selTemp.options[selTemp.selectedIndex].value;
		}
		else
			selected_weeks = 0;
		if(eval("obj." + details[i] + "_days"))
		{
			selTemp = eval("obj." + details[i] + "_days");
			selected_days = selTemp.options[selTemp.selectedIndex].value;
		}
		else
			selected_days = 0;
		if(eval("obj." + details[i] + "_hours"))
		{
			selTemp = eval("obj." + details[i] + "_hours");
			selected_hours = selTemp.options[selTemp.selectedIndex].value;
		}
		else
			selected_hours = 0;
		if(eval("obj." + details[i] + "_minutes"))
		{
			selTemp = eval("obj." + details[i] + "_minutes");
			selected_minutes = selTemp.options[selTemp.selectedIndex].value;
		}
		else
			selected_minutes = 0;
		if(eval("obj." + details[i] + "_seconds"))
		{
			selTemp = eval("obj." + details[i] + "_seconds");
			selected_seconds = selTemp.options[selTemp.selectedIndex].value;
		}
		else
			selected_seconds = 0;

		// Work out duration in seconds
		
		current_duration = (selected_years * 29030400) + (selected_months * 2419200) + (selected_weeks * 604800)  + (selected_days * 86400) + (selected_hours * 3600) + (selected_minutes * 60 ) + (selected_seconds * 1);
			
		maxList = eval("obj." + details[i] + "_maxduration").value;		
		minList = eval("obj." + details[i] + "_minduration").value;
		maxDurations = maxList.split(",");
		minDurations = minList.split(",");
		for (a = 0; a < 7; a++)
		{
			if (minDurations[a] == "")
				minDurations[a] = 0;
			if(maxDurations[a] == "")
				maxDurations[a] = 0;
		}
		min_duration = (minDurations[0] * 29030400) + (minDurations[1] * 2419200) + (minDurations[2] * 604800) + (minDurations[3] * 86400) + (minDurations[4] * 3600) + (minDurations[5] * 60 ) + (minDurations[6] * 1);
		max_duration = (maxDurations[0] * 29030400) + (maxDurations[1] * 2419200) + (maxDurations[2] * 604800) + (maxDurations[3] * 86400) + (maxDurations[4] * 3600) + (maxDurations[5] * 60 ) + (maxDurations[6] * 1);
		if (current_duration < min_duration)
		{
			alertmsg = tmp_field_name + " - Sorry, the Minimum is ";
			if (!(minDurations[0] == "" || minDurations[0] == "0"))
			{
				alertmsg = alertmsg + minDurations[0] + " Years ";
			}
			if (!(minDurations[1] == "" || minDurations[1] == "0"))
			{
				alertmsg = alertmsg + minDurations[1] + " Months ";
			}
			if (!(minDurations[2] == "" || minDurations[2] == "0"))
			{
				alertmsg = alertmsg + minDurations[2] + " Weeks ";
			}
			if (!(minDurations[3] == "" || minDurations[3] == "0"))
			{
				alertmsg = alertmsg + minDurations[3] + " Days ";
			}
			if (!(minDurations[4] == "" || minDurations[4] == "0"))
			{
				alertmsg = alertmsg + minDurations[4] + " Hours ";
			}
			if (!(minDurations[5] == "" || minDurations[5] == "0"))
			{
				alertmsg = alertmsg + minDurations[5] + " Minutes ";
			}
			if (!(minDurations[6] == "" || minDurations[6] == "0"))
			{
				alertmsg = alertmsg + minDurations[6] + " Seconds ";
			}
			alert(alertmsg);
			retStatus = false;
		}
		if (current_duration > max_duration)
		{
			alertmsg = tmp_field_name + " - Sorry, the Maximum is ";
			if (!(maxDurations[0] == "" || maxDurations[0] == "0"))
			{
				alertmsg = alertmsg + maxDurations[0] + " Years ";
			}
			if (!(maxDurations[1] == "" || maxDurations[1] == "0"))
			{
				alertmsg = alertmsg + maxDurations[1] + " Months ";
			}
			if (!(maxDurations[2] == "" || maxDurations[2] == "0"))
			{
				alertmsg = alertmsg + maxDurations[2] + " Weeks ";
			}
			if (!(maxDurations[3] == "" || maxDurations[3] == "0"))
			{
				alertmsg = alertmsg + maxDurations[3] + " Days ";
			}
			if (!(maxDurations[4] == "" || maxDurations[4] == "0"))
			{
				alertmsg = alertmsg + maxDurations[4] + " Hours ";
			}
			if (!(maxDurations[5] == "" || maxDurations[5] == "0"))
			{
				alertmsg = alertmsg + maxDurations[5] + " Minutes ";
			}
			if (!(maxDurations[6] == "" || maxDurations[6] == "0"))
			{
				alertmsg = alertmsg + maxDurations[6] + " Seconds ";
			}
			alert(alertmsg);
			retStatus = false;
		}
		i++;
	}
	return retStatus;
}

function buyFormSubmitGoogleAnalytics(obj) {
	if (CheckOptions(obj))
	{
		if (typeof(__utmLinkPost) != "undefined")
		{
			__utmLinkPost(obj);
		}
		else if (typeof(pageTracker._link) != "undefined")
		{
			pageTracker._linkByPost(obj);
		}
		return true;
	}
	else
	{
		return false;
	}
}
