<!--hide this script from non-javascript-enabled browsers
// The first argument is the form
function checkform(f,sCurrentPasswd, sPasswd,sAllowBlank1, sAllowBlank2, sAllowBlank3, sEmail, sURL, sRelURL, AtMax, MaxDescriptionLength)
{
	var eTemp;     
	var nAtSym; 
	var nPeriod;
	var nAmpersand;
	var nSpace; 
	var nLength; 
	var sHTTP = 'http://';
	var day, month, year;
	var leapYear;
	var elementday;
	var MaxChars;
	var msg;
	MaxChars=255;
	msg='';
    // Loop through the elements of the form, looking for all 
    // select elements. 
	// Put together error messages for fields that are wrong.
	month=0;
	day=0;
	year=0;
	if(f.ClassifiedObjectID != null && AtMax)
	{
		if(f.ClassifiedObjectID.value<=0)
		{
            alert( "You have reached the maximum allowed number of entries.");
            return  (false);
        }
	}
 
    for(var i = 0; i < f.length; i++) 
    {
        var e = f.elements[i];
		// they don't have to have email or a URL - temporary fix don't allow blank htmldescription
        if (((e.type == "text") || (e.type == "textarea")) && e.name!= sURL && e.name!=sPasswd &&
		((e.name).indexOf(sEmail)== -1) &&((e.name).indexOf(sAllowBlank1)== -1)&&((e.name).indexOf(sAllowBlank2)== -1)&&((e.name).indexOf(sAllowBlank3)== -1)||((e.name).indexOf("html")==0)) 
        {
            if(e.value=="")
            {
                alert( "Please complete field indicated to submit this form");
                f.elements[i].focus();
                return  (false);
            }
        }
		else
		{
    		if(e.name == "month")
			{
				month=e.value;
			}
			else if (e.name == "day")
			{
				day=e.value;
				elementday=e;
			}
			else if (e.name == "year")
			{
				year = e.value;
			}
		}
 		if(e.type=="text" && e.value.length>MaxChars)
		{
			msg="Field indicated should not exceed "+MaxChars+" characters";
               alert(msg);
               f.elements[i].focus();
               return  (false);
		}			
		else if(e.type=="textarea" && e.value.length>MaxDescriptionLength)
		{
			var number= e.value.length;
			msg="Field indicated should not exceed "+MaxDescriptionLength+" characters";
      		alert(msg);
            f.elements[i].focus();
            return  (false);
		}
		// if there is an email element on the form then check that it is valid if non-empty
		if(e.type=="text" && ((e.name).indexOf(sEmail)>=0) && e.value.length > 0)
		{
			if(!checkemail(e.value))
			{
	      		alert('Please enter a valid e-mail address!');
	      		e.focus();
				return (false);
	   		}
		}
   }
// if there is a password element on the form then check that it is not empty - unless a previous password was defined
	if(sPasswd.length > 0 && f.elements[sPasswd] != null && sCurrentPasswd.length == 0 && f.elements[sPasswd].value.length<5)
	{
 		eTemp     = f.elements[sPasswd];
     	alert('Please enter a password of at least 5 letters and numbers ')
      	eTemp.focus();
		return (false);
	}
	// if there is a website address element on the form then check that it is valid if non-empty
	if(sURL.length > 0 && f.elements[sURL] != null && f.elements[sURL].value.length > 0)
	{
		eTemp     = f.elements[sURL];
		nHttp    = eTemp.value.indexOf(sHTTP);
		nPeriod   = eTemp.value.lastIndexOf('.');
		nSpace    = eTemp.value.indexOf(' ');
		nLength   = eTemp.value.length - 1;   // Array is from 0 to length-1
		if ((nHttp !=0) ||             // Must be atleast one valid char btwn '@' and '.'
    	(nPeriod == nLength ) ||             // Must be atleast one valid char after '.'
    	(nSpace  != -1))                    // No empty spaces permitted
   		{  
      		alert('Please enter a valid website address!');
			if(nHttp != 0)
			{
				var sNewURL = sHTTP + eTemp.value;
				eTemp.value=sNewURL;
			}
				
      		eTemp.focus();
			return (false);
   		}
	}
		// if there is a local website address element on the form then check that it is valid if non-empty
	if(sRelURL.length > 0 && f.elements[sRelURL] != null && f.elements[sRelURL].value.length > 0)
	{
		eTemp     = f.elements[sRelURL];
		nSpace    = eTemp.value.indexOf(' ');
		nApostrophe    = eTemp.value.indexOf("'");
		nAmpersand    = eTemp.value.indexOf("&");
		if (nSpace  != -1 || nApostrophe!=-1 || nAmpersand!=-1)                   // No empty spaces or apostrophes - but we shouldn't have any punctuation
   		{  
      		alert('Please enter a valid local folder name!');			
      		eTemp.focus();
			return (false);
   		}
	}

	if(month!=0 || year != 0 || day!= 0)
	{
		success = checkdate(day, month, year);
		if(!success)
		{
        	alert( "You have entered an invalid date");
        	elementday.focus();
        	return  (false);
		}
	}	
    return (true);
}
var widthstring="width";

function resetform(f, MaxImages)
{
	var i;
 	var sImageName, sPopupName;
   // Loop through the elements of the form 
    for(var i = 0; i < f.length; i++) 
    {
        var e = f.elements[i];
        if (((e.type == "text") || (e.type == "textarea")) && (e.name.indexOf(widthstring)<0)) 
        {
            e.value="";
        }
    }
	if(f.ClassifiedObjectID != null)
	{
		f.ClassifiedObjectID.value=-1;
	}
	if(f.pageorder != null)
	{
		f.pageorder.value=1;
	}
	if(f.image != null)
	{
		f.image.value=" ";
	}
	if(f.logo != null)
	{
		f.logo.value=" ";
	}
	if(f.LastRelativeURL != null)
	{
		f.LastRelativeURL.value="";
	}
	// clear out the images
	for(i=0;i<MaxImages; i++)
	{ 
		sImageName="image"+i;
		sPopupName="popup"+i;
		if(f.elements[sImageName] != null)
		{
			f.elements[sImageName].value=" ";
		}
		if(f.elements[sPopupName] != null)
		{
			f.elements[sPopupName].value=" ";
		}
	}
	return false;
}
function confirmdelete(f, EntryClassifiedObject)
{
	var msg;
	msg="";
	var classifiedObjectElement;
	var thisClassifiedObjectID=0;
	if(document.getElementById)
	{
		classifiedObjectElement = document.getElementById(EntryClassifiedObject);
	
	
		if(classifiedObjectElement!=null)
		{
			thisClassifiedObjectID=classifiedObjectElement.value;
		}
		else
		{
			return false;
		}
	}
	if(f.ClassifiedObjectID != null && f.ClassifiedObjectID.value>0 && thisClassifiedObjectID  > 0)
	{
		msg += "Are you sure you want to delete ";
		return (confirm(msg));
	}
	else
	{
		msg+="No current selection for deletion ";
		alert(msg);
		return false;
	}
	return false;
}
function confirmhide(f, EntryClassifiedObject)
{
	var msg;
	msg="";
	var classifiedObjectElement;
	var thisClassifiedObjectID=0;
	if(document.getElementById)
	{
		classifiedObjectElement = document.getElementById(EntryClassifiedObject);
	
	
		if(classifiedObjectElement!=null)
		{
			thisClassifiedObjectID=classifiedObjectElement.value;
		}
		else
		{
			return false;
		}
	}
	if(f.ClassifiedObjectID != null && f.ClassifiedObjectID.value>0 && thisClassifiedObjectID  > 0)
	{
		msg += "Are you sure you want to hide / unhide (to complete an unhide press submit after this operation) ";
		return (confirm(msg));
	}
	else
	{
		msg+="No current selection for hiding ";
		alert(msg);
		return false;
	}
	return false;
}
function confirmolddelete()
{
	var msg;
	msg="";
	msg += "Are you sure you want to delete ";
	return (confirm(msg));
}
function confirmarchive()
{
	var msg;
	msg="";
	msg += "Are you sure you want to archive ";
	return (confirm(msg));
}
function checkdate(day, month, year)
{
    var success=true;
	var leapYear;
	
	leapYear=((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));
	if ((day == 31 && month == 2) || 
	(day == 31 && month == 4) || 
	(day == 31 && month == 6) || 
	(day == 31 && month == 9) || 	
	(day == 31 && month == 11)|| 
	(day == 30 && month == 2) ||
	(day == 29 && month == 2 && !leapYear)) 
	{ 
		success=false;        
    } 
 	return (success);
}
// Thanks to JavaScript Kit www.javascriptkit.com
function checkemail(value)
{
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (!filter.test(value))
		return (false);
	else
		return (true);
}
// I think th Luhn Formula always uses modulus 10 so the generalization here to any base is probably unnecessary
// also could only work up to modulus 10 as it expects that the numberString consists of integers only.
// The last digit is the check digit and to find a valid number it can be calculated from the others
// A credit card  number must pass the following algorithm 
//Starting with the second to last digit and moving left, double the value of all the alternating digits. 
// For any doubled digit that is 10 or more, add the digits together (an alternative way to do this is to subtract 9 if the number exceeds 9)
// For example, 1111 becomes 2121, while 8763 becomes 7733 (from 2x8=16 -> 1+6=7 and 2x6=12 -> 1+2=3). 
//Add all these numbers together. For example, 1111 becomes 2121, then 2+1+2+1 is 6; while 8763 becomes 7733, then 7+7+3+3 is 20. 
//If the total mod 10 is 0 then the number is valid according to the Luhn formula, else it is not valid. 
// 
function CheckLuhnFormula(numberString, modulus)
{
	var numberLength = numberString.length;
	var digitDoubled=0;
	var checkSum = 0;
	var counter = numberLength - 1;
	while (counter >= 1)
	{
		// start by adding  on the right most number
		checkSum += parseInt(numberString.charAt(counter));
		counter--;
		// take the next number to the left, double it and sum the two digits
		digitDoubled = parseInt(numberString.charAt(counter) * 2);
		// This is equivalent to subtracting the modulus -1 if the digitDoubled is greater than modulus
		if(digitDoubled >=modulus)
		{
			checkSum+=digitDoubled-modulus+1;
		}
		else
		{
			checkSum+=digitDoubled;
		}
		counter--;
	}
	isValid = (checkSum % modulus == 0);
	return isValid;
}
//can be entered without a cardtype and will test on other criteria
// Supported cardtypes are visa, mastercard and American Express
// a credit card number should be a ISO 7812
function	CheckCreditCardNumber(cardNumber, cardType)
{
  	var isValid = false;
	var cardTypeFound = false;
  	var CreditCardCheckRegExp = /[^\d ]/;
	// Luhn algorithm mod 10
	var modulus=10;
	
	isValid = !CreditCardCheckRegExp.test(cardNumber);
	cardType.toLowerCase();
	
	if (isValid)
	{
		var cardNumbersOnly = cardNumber.replace(/ /g,"");
		var cardNumberLength = cardNumbersOnly.length;
		var lengthIsValid = false;
		var prefixIsValid = false;
		var prefixRegExp;
		lengthIsValid = (cardNumberLength >= 13 && cardNumberLength <= 19);
		
		switch(cardType)
		{
			case "mastercard":
				lengthIsValid = (cardNumberLength == 16);
				prefixRegExp = /^5[1-5]/;
				cardTypeFound=true;
				break;
			
			case "visa":
				lengthIsValid = (cardNumberLength == 16 || cardNumberLength == 13);
				prefixRegExp = /^4/;
				cardTypeFound=true;
				break;
			
			case "american express":
				lengthIsValid = (cardNumberLength == 15);
				prefixRegExp = /^3(4|7)/;
				cardTypeFound=true;
				break;
			
			default:
				prefixRegExp = /^$/;
				cardTypeFound=false;
		}
		if(cardTypeFound)
		{
    		prefixIsValid = prefixRegExp.test(cardNumbersOnly);
    		isValid = prefixIsValid && lengthIsValid;
		}
		else
		{
			isValid=lengthIsValid;
		}
	}
	
	if (isValid)
	{
		isValid = CheckLuhnFormula(cardNumbersOnly, modulus)
	}
	
	return isValid;
}

function themeformcheck(f)
{
	var eTemp;     
	var nAtSym; 
	var nPeriod;
	var nSpace; 
	var nLength; 
	var themefileempty=false;
    // Loop through the elements of the form 
     for(var i = 0; i < f.length; i++) 
    {
        var e = f.elements[i];
		// they don't have to have email or a URL
        if  (e.name=="themefile" && (e.value=="")) 
        {
			themefileempty=true;
        
        }
		else if(e.name=="themename" && e.value=="" && themefileempty)
		{
        	alert( "Please complete field indicated to submit this form");
            f.elements[i].focus();
            return  (false);
		}
		else if(e.name=="themedescription" && e.value=="")
		{
        	alert( "Please complete field indicated to submit this form");
            f.elements[i].focus();
            return  (false);
		}
		
    }
	// if there is an email element on the form then check that it is valid if non-empty
    return (true);
}
function resetthemeform(f, MaxThemeImages)
{
	var i;
 	var sImageName;
   // Loop through the elements of the form 
    for(var i = 0; i < f.length; i++) 
    {
        var e = f.elements[i];
		// they don't have to have email or a URL
        if ((e.type == "text") || (e.type == "textarea")) 
        {
            e.value="";
        }
    }
	if(f.ThemeID != null)
	{
		f.ThemeID.value=-1;
	}
	for(i=0;i<MaxThemeImages; i++)
	{ 
		sImageName="image"+i;
		if(f.elements[sImageName] != null)
		{
			f.elements[sImageName].value="";
		}
	}
	return false;
}
function numberorder(a,b) {return a -b;}
function formformcheck(f,formdescriptionnamestring, typenamestring, promptnamestring, sequencenumbernamestring)
{
	var count=0;
	var SequenceNumberArray = new Array();
	var i;
    // Loop through the elements of the form 
    for(i = 0; i < f.length; i++) 
    {
        var e = f.elements[i];
		if(e.name=="formdescription" && e.value=="")
		{
        	alert( "Please complete field indicated to submit this form");
            f.elements[i].focus();
            return  (false);
		}
		else if(e.name.indexOf(typenamestring)>=0)
		{
			if(e.value>0)
			{
			
				exists=true;
			}
			else
			{
				exists=false;
			}
		}
		else if(e.name.indexOf(promptnamestring)>=0)
		{
			if(exists && e.value=="")
			{
	       		alert( "Please complete field indicated to submit this form");
            	f.elements[i].focus();
            	return  (false);
			}
		}
		else if(e.name.indexOf(sequencenumbernamestring)>=0)
		{
			if(exists)
			{
				SequenceNumberArray[count]=e.value;
				count++;
			}
		}
    }
	// sort in numerical order and check that no numbers are missing
	SequenceNumberArray.sort(numberorder);
	for(i = 0; i < SequenceNumberArray.length; i++) 
	{
		if(SequenceNumberArray[i]!=i+1)
		{
			alert("Position numbers must use the values 1 to "+SequenceNumberArray.length+" exactly once.");
			return false;
		}
 	}
	// if there is an email element on the form then check that it is valid if non-empty
    return (true);
}
function resetformform(f, MaxFormElements)
{
	var i;
 	var sImageName;
   // Loop through the elements of the form 
    for(var i = 0; i < f.length; i++) 
    {
        var e = f.elements[i];
		// they don't have to have email or a URL
        if ((e.type == "text") || (e.type == "textarea")) 
        {
            e.value="";
        }
    }
	if(f.FormID != null)
	{
		f.FormID.value=-1;
	}
	// this needs changing
	for(i=0;i<MaxFormElements; i++)
	{ 
		sElementName="element"+i;
		//if(f.elements[sElementName] != null)
		//{
		//	f.elements[sElementName].value="";
		//}
	}
	return false;
}
var textType = 1;
var  textAreaType= 2;
var  checkBoxType= 3;
var selectType= 4;
var dateType= 5;
var timeType= 6;
var monthYearDateType= 7;
var numberType= 8;

function simplecheckform(f)
{
	var e, eprompt, etype;
	var isEmail=false, isNumber=false;
	var isValidNumber=false;
	var isCreditCard=false, isCCV=false;
	var month, day, year;
   	var NumberCheckRegExp = /[^\d ]/;
	
   // Loop through the elements of the form 
    for(var i = 0; i < f.length; i++) 
    {
        e = f.elements[i];
        if (((e.type == "text") || (e.type == "textarea"))) 
        {
            if(e.value=="")
            {
                alert( "Please complete all fields to submit this form or enter a space if not applicable");
                f.elements[i].focus();
                return  (false);
            }
			// before each element (except for dates and times) there are two hidden elements.  
			// The first is the 'type' (internal type which is 
			// text, textarea, date, time, select, monthyeardate and number) and the
			// second is the prompt.  The prompt should indicate special nature of some of the elements
        	if(i>1)
			{
				etype=f.elements[i-2];
				eprompt=f.elements[i-1];
				if(etype.value==numberType && etype.value!=" ")
				{
					isValidNumber = !NumberCheckRegExp.test(e.value);
					if(!isValidNumber)
					{
						alert( "Please enter a number");
			            e.focus();
						return false;
					}
				}

				if(eprompt.type=="hidden")
				{
			 		isEmail    = eprompt.value.search(/email/i);
					if(isEmail < 0)
					{
						isEmail=eprompt.value.search(/e-mail/i);
					}
					if(isEmail>=0 && !checkemail(e.value))
					{
						alert( "Please enter a valid email address");
			            e.focus();
						return false;
					}
			 		isCreditCard    = eprompt.value.search(/card/i);
					if(isCreditCard < 0)
					{
						isCreditCard=eprompt.value.search(/credit/i);
					}
					if(isCreditCard < 0)
					{
						isCreditCard=eprompt.value.search(/debit/i);
					}
					if(isCreditCard < 0)
					{
						isCreditCard=eprompt.value.search(/mastercard/i);
					}
					if(isCreditCard < 0)
					{
						isCreditCard=eprompt.value.search(/visa/i);
					}
					// also check for the word number because they could just be asking the type
					if(isCreditCard>=0)
					{
						isCreditCard=eprompt.value.search(/number/i);
					}
					// we don't know the card type
					if(isCreditCard>=0 && e.value!=" " && !CheckCreditCardNumber(e.value, ""))
					{
						alert( "Please enter a valid Card Number");
		            	e.focus();
						return false;
					}
				}
			}
		}
		else if (e.type!="hidden")
		{
	  		if(e.name.indexOf("month")>=0)
			{
				month=e.value;
			}
			else if (e.name.indexOf("day")>=0)
			{
				day=e.value;
				elementday=e;
			}
			else if (e.name.indexOf("year")>=0)
			{
				year = e.value;
			}
			if(day>0 && month>0 && year >0)
			{
				if(!checkdate(day, month, year))
				{
					alert( "Please enter a valid date");
	            	elementday.focus();
					return false;
				}
				else
				{
					// allow for more than one date on the form
					day=-1;
					month=-1;
					year=-1;
				}
			}
		}
			
    }
    return (true);

}
// for a comboBox of any choice element (e.g. select or list) and a related text field
// If the user selects then that should update the text field.  
// Ideally also If they update the text field to a value supported in the select list then mark that as selected
// but for the moment we won't do that
function ComboBoxUpdate(formName, textElement, choiceElement) 
{
	var i;
	var j;
    if(!document.forms[formName] || !document.forms[formName].elements[textElement] || !document.forms[formName].elements[choiceElement])
    {
        return;
    }
	if(document.forms[formName].elements[choiceElement].type=="select" || document.forms[formName].elements[choiceElement].type=="select-one")
	{
		// this is for a select
        document.forms[formName].elements[textElement].value = document.forms[formName].elements[choiceElement].value;
	}
}
function CheckCurrentImage(currentImageElement, currentImageName) 
{
	var i;
	var j;
    if(!currentImageElement)
    {
        return;
    }
	
	if(currentImageElement.value=="" || currentImageElement.value.charAt(0)==" ")
	{
		// set it to a single space so that the form checking will not object
        currentImageElement.value = " ";
	}
	else
	{
		// set it back to its original value
		currentImageElement.value = currentImageName;
	}		
}

// -->
