// window.onerror = handleError;

window.onload = pageLoad;

function pageLoad()	{
	if(document.getElementById('createCategoryRequest')!=null)
		closeAll();
	
	if(document.getElementById('tblEditor')!=null)	{
		baseURL = document.getElementById('baseURL').value;
		projectName = document.getElementById('projectName').value;
		siteId = document.getElementById('_siteId').value;

		document.iView.document.designMode = 'On';
		setTimeout("doToggleBasic(baseURL, projectName, siteId)", 100);
	}
}

function closeAll()	{
	thisForm = document.getElementById('createCategoryRequest');
	aTags = thisForm.getElementsByTagName('a');
	
	for(i=0; i<aTags.length; i++)	{
		if(aTags[i].className=='pageSectionLink')	{
			divId = aTags[i].nextSibling.id;
			
			togglePageSection(aTags[i], divId)
		}
	}
	
}

function togglePageSection(linkSource, pageSection)	{
	if(linkSource.className=='pageSectionLink')	{
		linkSource.className='pageSectionLinkOff';
		document.getElementById(pageSection).className='pageSectionOff';
	}
	else	{
		linkSource.className='pageSectionLink';
		document.getElementById(pageSection).className='pageSection';
	}
}

function changeImage(target, imgName)
{

	var newImage = new Image();

	newImage.src = imgName;

	document.all[target].src = eval('newImage.src');
}

function showAdminSection(sectionNum)	{
	for(intCount=1; intCount<10; intCount++)	{
		document.getElementById('adminSection'+intCount).style.display='none';
		
		if(document.getElementById('button'+intCount)!=null)
			document.getElementById('button'+intCount).className='adminSection';
	}
	
	document.getElementById('adminSection'+sectionNum).style.display='block';
	document.getElementById('button'+sectionNum).className='adminSectionOn';
}

function isNumeric(sText)
{
	var validChars = '0123456789.';
	var isNumber = true;
	var character;
	var intCount = 0;

	for (intCount = 0; intCount < sText.length && isNumber == true; intCount++)
	{
		character = sText.charAt(intCount);
		if (validChars.indexOf(character) == -1)
		{
			isNumber = false;
		}
	}
	return isNumber;
}


function handleError(msg, url, ln)
{
   // Do you custom code here
   return true;
}


function changeClass(target, className)
{
 	target.className = className;
}

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;
}


// Called with parameter of name of form to validate. Form objects with a value of manditory="true" must contain
// values.
function validateForm(formName)
{
	
	if(navigator.appName == 'Netscape')
	{
		return true;
	}

	//Set text for error message
	var errText='is a required field';

	//Check all controls on the form
	for (count = 0; count < formName.length; count++)
	{
	
		// Strip of preceeding white space (tabs, spaces and non-breaking space) from any fields
		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;
		}

		// Validate email fields (any fields with attribute of type="email")
		if(formName.elements[count].type == 'email')
		{
		 	return validateEmail(formName.elements[count]);
		}

		// 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 == '')
		{
			for (var check = 0; check < manditoryFields.length; check++)
			{
				if(formName.elements[count].name == manditoryFields[check])
				{
					alert(formName.elements[count].name + ' ' + errText);

					formName.elements[count].focus();
					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].manditory == 'true'))
		{
			alert('This is a required field');
			
			formName.elements[count].focus();
			return false;
		}
		
		// Check for manditory too, IE only
		if(formName.elements[count].type == 'checkbox' && formName.elements[count].checked == false  && (formName.elements[count].manditory == 'true'))
		{
		 	alert(formName.elements[count].name + ' must be ticked');
			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;
				}
			}
		}
	}
}

function validateEmail(emailField)
{
  var emailError = '';
  
  if(emailField.value == '')
  {
	return false
  }
  
  if(-1 == emailField.value.indexOf("@"))
  { 
     emailError = "Your email must have a '@'."; 
  }
  
  if(-1 == emailField.value.indexOf("."))
  { 
     emailError = "Your email must have a '.' in it."; 
  }
  
  if(-1 != emailField.value.indexOf(","))
  { 
     emailError = "Your email must not have a ',' in it"; 
  }
     
  if(-1 != emailField.value.indexOf("#"))
  { 
     emailError= "Your email must not have an '#' in it."; 
  }
     
  if(-1 != emailField.value.indexOf("!"))
  { 
     emailError = "Your email must not have a '!' in it."; 
  }
  
  if(-1 != emailField.value.indexOf(" "))
  { 
    emailError = "Your email must not have a space in it." ; 
  }
  
  if(emailField.value.length == (emailField.value.indexOf("@")+1) )
  {
     emailError = "Your email must have a domain name after the '@'.";
  }

  if(emailField.value.length == 0)
  { 
    emailError = "Please enter your email."; 
  }
  
  if(emailError != '')
  {
   	 emailField.value = '';
   	 alert(emailError);
	 emailField.focus();
	 return false;
  }
  
  return true;
}

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.firstChild.children(1) != null) && (eNext.firstChild.children(1).className == "eBranch"))
			{
				eNext.firstChild.children(1).src = imagePath + "plus.gif";
			}
			if (eNext.className == "child" || eNext.className == "categoryShade")
			{
				eNext.style.display = "none";
				HideAll(eNext, iGroupNumber, imagePath);
			}
			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";
			}
		}
		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, category)
{

 	if(navigator.appName == 'Netscape')
	{
		
	}
	else
	{
    	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;
	}
	
	var syntaxString = '';
	if(newCharCount != 1)
	{
		syntaxString = 's';
	}

	document.getElementById(countTarget).innerHTML = newCharCount + ' Character' + syntaxString + ' Remaining.';
}

// 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();
}

function swapImage(target, imgName)
{
   var newImage = new Image();         
   newImage.src = imgName;
   
   alert(this.name);
   
   document.images[target].src = eval('newImage.src');
}
