window.onerror = handleError;

function handleError(msg, url, ln)
{
   // Do you custom code here
   return true;
}

function asciiValue(Char)
{
	// restrict input to a single character
	Char = Char.charAt(0);

	// loop through all possible ASCII values
	var intLoop;
	for (intLoop = 0; intLoop < 256; intLoop++)
	{
		// convert intLoop into a 2-digit hex string
		var hexValue = intLoop.toString(16);
		if (hexValue.length == 1)
			hexValue = "0" + hexValue;

		// insert a % character into the string
		hexValue = "%" + hexValue;

		// determine the character represented by the escape code
		hexValue = unescape(hexValue);

		// if the characters match, we've found the ASCII value
		if (hexValue == Char)
			break;
			
			
	}
	return intLoop;
}


function validateForm(formName)
{	
	//Set text for error message
	var errText='This is a required field';
	
	//Check all controls on the form
	for (count = 0; count < formName.length; count++)
	{	
		if(formName.elements[count].value != '')
		{
			var strText = formName.elements[count].value;
			while (asciiValue(strText.substring(0,1)) == '160' || strText.substring(0,1) == ' ') strText = strText.substring(1);
			while (asciiValue(strText.substring(strText.length-1,strText.length)) == '160' || strText.substring(strText.length-1,strText.length) == ' ') strText = strText.substring(0,strText.length-1);
			formName.elements[count].value = strText;
		}
		
		//If type is text or password and value is empty or space and manditory is true
		if((formName.elements[count].type == 'text' || formName.elements[count].type == 'password') && (formName.elements[count].value == '' || formName.elements[count].value == ' '))
		{
			for (check = 0; check < manditoryFields.length; check++)
			{
				if(formName.elements[count].name == manditoryFields[check])
				{
					formName.elements[count].value=errText;
					formName.elements[count].select();
					return false
				}
			}
		}
		
		// Check for manditory too, IE only
		if((formName.elements[count].type == 'text' || formName.elements[count].type == 'password') && (formName.elements[count].value == '' || formName.elements[count].value == ' ') && (formName.elements[count].manditory == 'true'))
		{
			formName.elements[count].value=errText;
			formName.elements[count].select();
			return false
		}
		
		// Check for manditory too, IE only
		if(formName.elements[count].type == 'checkbox' && formName.elements[count].checked == false  && (formName.elements[count].manditory == 'true'))
		{
			return false
		}
		
		//Perform check for check box
		if(formName.elements[count].type == 'checkbox' && formName.elements[count].checked == false)
		{
			for (check = 0; check < manditoryFields.length; check++)
			{
				if(formName.elements[count].name == manditoryFields[check])
				{
					return false
				}
			}
		}
		
		//If user hasn't replaced the error text
		if((formName.elements[count].type == 'text' || formName.elements[count].type == 'password') && formName.elements[count].value == errText)
		{
			formName.elements[count].select();
			return false
		}
	}
	
	return true

}

function validateEmail(emailField)
{
  if(emailField.value == '')
  {
	return false
  }
  
  if(-1 == emailField.value.indexOf("@"))
  { 
     emailField.select();
     emailField.value = "Your email must have a '@'."; 
     return false 
  }
  
  if(-1 != emailField.value.indexOf(","))
  { 
     emailField.select(); 
     emailField.value = "Your email must not have a ',' in it"; 
     return false
  }
     
  if(-1 != emailField.value.indexOf("#"))
  { 
     emailField.select(); 
     emailField.value = "Your email must not have an '#' in it."; 
     return false
  }
     
  if(-1 != emailField.value.indexOf("!"))
  { 
     emailField.select(); 
     emailField.value = "Your email must not have a '!' in it."; 
     return false 
  }
  
  if(-1 != emailField.value.indexOf(" "))
  { 
     emailField.select(); 
     emailField.value = "Your email must not have a space in it." ; 
     return false
  }
  
  if(emailField.value.length == (emailField.value.indexOf("@")+1) )
  {
     emailField.select();
     emailField.value = "Your email must have a domain name after the '@'.";
     return false
  }

  if(emailField.value.length == 0)
  { 
    emailField.select(); 
    emailField.value = "Please enter your email."; 
    return false
  }
}

function confirmMatch(password, confirm)
{
	if(password.value != confirm.value)
	{
		confirm.value = '';
		password.value = '';
		password.select();
		alert("Your passwords must match. Please try again.");
	}
}

function HideAll(eSrc,iGroupNumber,imagePath)
{
	if (((eSrc.tagName == "IMAGE") || (eSrc.tagName == "IMG")) && (eSrc.parentElement.parentElement.tagName == "TR"))
		eSrc = eSrc.parentElement.parentElement;
	else if (eSrc.tagName != "TR")
		return false;
	
	iGroupNumber = Number(iGroupNumber);
	if (eSrc.nextSibling && !isNaN(iGroupNumber))
	{
		var eNext = eSrc.nextSibling;
		var iNextGroupNumber = Number(eNext.GroupNumber);
		if (iGroupNumber < iNextGroupNumber)
		{
				if (eNext.className == "child" || eNext.className == "categoryShade")
				{
					if (eNext.firstChild.children(1).className == "eBranch")
					{
						HideAll(eNext);
						eNext.firstChild.children(1).src = imagePath + "plus.gif";
						eNext.firstChild.children(1).className = "uBranch";
						eNext.style.display = "none";
					}
					else if ((eNext.firstChild.children(1).className == "uBranch") || (eNext.firstChild.children(1).className == "leaf"))
					{
						eNext.style.display = "none";
					}
				}
				if (eSrc.firstChild.children(1).className != "leaf")
				{
					eSrc.firstChild.children(1).src = imagePath + "plus.gif";
					eSrc.firstChild.children(1).className = "uBranch";
					eSrc.style.display = "none";
				}	
			HideAll(eNext,iGroupNumber);
		}
		else
		{
			return true;
		}
	}
}

function showAll(eSrc,iGroupNumber,imagePath)
{
	if ((eSrc.tagName == "IMG") && (eSrc.parentElement.parentElement.tagName == "TR"))
		eSrc = eSrc.parentElement.parentElement;
	else if (eSrc.tagName != "TR")
		return false;
	
	iGroupNumber = Number(iGroupNumber);
	if (eSrc.nextSibling && !isNaN(iGroupNumber))
	{
		var eNext = eSrc.nextSibling;
		var iNextGroupNumber = Number(eNext.GroupNumber);
		
		if((eNext.firstChild.children(1) != null) && (eNext.firstChild.children(1).className == "uBranch"))
		{
			eNext.firstChild.children(1).src = imagePath + "plus.gif";
		}
				
		if (iGroupNumber < iNextGroupNumber)
		{
			if ((iGroupNumber + 1) == iNextGroupNumber) {
				if ((eNext.className == "child") || (eNext.className == "leaf") || (eNext.className == "categoryShade"))
				{
					eNext.style.display="";
				}
			} 
						
			showAll(eNext, iGroupNumber, imagePath);
		}
		else
		{
			return true;
		}
	}
}

function categoryNavigate(imagePath)
{
	var eSrc = window.event.srcElement;
	var eNext = eSrc.parentElement.parentElement.nextSibling;
	
	if ((eNext != null) && ((eSrc.tagName == "IMG") || (eSrc.tagName == "IMAGE")))
	{
		if (eSrc.className == "uBranch")
		{
			eSrc.src = imagePath + "minus.gif";
			eSrc.className="eBranch";
			showAll(eSrc.parentElement.parentElement, eSrc.parentElement.parentElement.GroupNumber, imagePath);
		} 
		else if (eSrc.className == "eBranch")
		{
			eNext.style.display = "none";
			eSrc.src = imagePath + "plus.gif";
			eSrc.className = "uBranch";
			HideAll(eNext, eSrc.parentElement.parentElement.GroupNumber, imagePath);
		}
	}
}

// This function takes a string of text, the number of chars in the text and a maximum
// If the text is longet than the max it is cropped

function charCount(sourceText, countTarget, count, max)
{
	var newCharCount = max - count;
	
	if(newCharCount < 0)
	{
		sourceText.select();
		sourceText.value = String(sourceText.value).substring(0,max);
		newCharCount = 0;
	}
	countTarget.value = newCharCount
}

// This function is called when the user changes their payment method
// If credit card is chosen then set action to be credit pay otherwise normal pay

function creditPayment(choice, formName, normalAction, creditAction)
{
	if(choice.value == 'Credit Card')
	{
		formName.action = creditAction;
	}
	else
	{
		formName.action = normalAction;
	}
}

// This function switches between two different actions for a given form
function toggleFormAction(action1, action2, formName)
{
	if(formName.action == action1)
	{
		formName.action = action2;
		formName.target = '_new';
	}
	else
	{
		formName.action = action1;
		formName.target = '_self';
	}
}

// Open a new window with the new image in it
function fullImage(path, imageName, name, imageSize)
{
	imageWindow= window.open("", name, imageSize);

	imageWindow.document.write('<html>');
	imageWindow.document.write('<head>');
	imageWindow.document.write('<link rel="stylesheet" type="text/css" href="' + path + '/screen.css"/>');
	imageWindow.document.write('</head>');
	imageWindow.document.write('<title>' + name + '</title>');
	imageWindow.document.write('<body class="fullImage" topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">');
	imageWindow.document.write('<table cellpadding="0" cellspacing="0" width="100%" height="100%">');
	imageWindow.document.write('<tr>');
	imageWindow.document.write('<td valign="middle" align="middle">');
	imageWindow.document.write('<img class="fullImage" src="'+ imageName +'" alt="' + name + '"/>');
	imageWindow.document.write('</td>');
	imageWindow.document.write('</tr>');
	imageWindow.document.write('</table>');
	imageWindow.document.write('</body>');
	imageWindow.document.write('</html>');
	imageWindow.focus();
}