//Global variables
//debugger;
var gsNextButtonID = "";
var gsBackButtonID = "";
var gsCustomButtonID = "";
var gsResetButtonID = "";
var gbScrollToSummary = false;
var gsValidationSummaryOnSubmitDef = "";
var goClickedImageSpan;
var bEnableOnUnloadWarning = true;

//Detecting browser
var bIsIE5 = (document.all && document.getElementById) ? true : false;
var bIsNetscape = (document.getElementById && !document.all) ? true : false;


// =================================================================================================== 

// JavaScript functions used by the Million Handshakes' server controls. 
// Copyright © 2002-2004 Million Handshakes AS. 

// =================================================================================================== 



// ===================================================================================================
// Description	  : Fires prior to the page being unloaded
// Server control : MHQuestionnaireControl
function MHQuestionnaireControl_OnUnloadIE(sWarning)
{	
	if(bEnableOnUnloadWarning)
		event.returnValue = sWarning;
}
function MHQuestionnaireControl_OnUnloadNN(sWarning)
{		
	if(bEnableOnUnloadWarning)
	{
		alert(sWarning);
	}
}


// ===================================================================================================
// Description	  : Fires when the user clicks an image
// Server control : MHMultiOptionBaseControl/MHSectionControl
function MHImage_OnClick(spanID, imageID, imageURL)
{		
	var span = document.getElementById(spanID);
	var image = document.getElementById(imageID);
			
	if(goClickedImageSpan != null)
	{
		goClickedImageSpan.style.display = 'none';
    }
    
    goClickedImageSpan = span;
		
	if(span.style.display != 'inline')
	{
	    image.src = imageURL;
		span.style.display = 'inline';
		span.focus();
	}
	else
	{
		span.style.display = 'none';
	}
}


// ===================================================================================================
// Description	  : Fires when the user changes the value of the date textbox
// Server control : MHDateQuestionControl
function DateTextBox_onchange(domTextBox, sCalendarID)
{	

}


// ===================================================================================================
// Description	  : Validates a date when a selection is made with the date popup
// Server control : MHDateQuestionControl
function calendarPopup_JavascriptOnChangeFunction(date, domID)
{
	if(!bIsIE5)
		return;
		
	var calendarTextbox = document.getElementById(domID);		
	var vals = calendarTextbox.Validators;
    var i;
    for (i = 0; i < vals.length; i++) {
        ValidatorValidate(vals[i]);
    }
    
    ValidatorUpdateIsValid();    
    
    bEnableOnUnloadWarning = true;
}


// ===================================================================================================
// Description	  : Clientside logic for resetting the answer form
// Server control : MHQuestionnaireControl
function MHQuestionnaireControl_ResetButton_JavaScriptOnClick(ButtonResetWarningText)
{
	var domHiddenControl = document.getElementById("ResetButton_hiddenInputControl");
	var answer = confirm(ButtonResetWarningText);
	
	if(answer == true)
	{
		if(!bIsNetscape)
			event.returnValue = true;		
		if(domHiddenControl != null)
			domHiddenControl.value = "true";			
		return true;
	}
	else
	{
		if(!bIsNetscape)
			event.returnValue = false;
		if(domHiddenControl != null)
			domHiddenControl.value = "false";			
		return false; 
	}
}


// ===================================================================================================
// Description	  : Ensures that the first dom element on the page has focus after page has loaded
// Server control : MHQuestionnaireControl
function SetFirstElementFocus(clientID)
{
	var oDOMElem = document.getElementById(clientID);
			
	if (oDOMElem != null)
	{			
		oDOMElem.focus();
	}
}


// ===================================================================================================
// Description	  : Enables page navigation to the next page when the user press enter
// Server control : MHQuestionnaireControl
function BindNextButtonEvent(sNextButtonID, sCustomButtonID, sBackButtonID, sResetButtonID)
{
	gsNextButtonID = sNextButtonID;
	gsCustomButtonID = sCustomButtonID;
	gsBackButtonID = sBackButtonID;
	gsResetButtonID = sResetButtonID;
	
	if (bIsNetscape) 
	{		
		document.captureEvents(Event.KEYPRESS);
		document.onkeypress = document_onkeypress_NS;
	}

	if(bIsIE5)
	{
		document.onkeypress = function() { document_onkeypress_IE(); }		
	}
}


function document_onkeypress_IE()
{
	var domNextButton = document.getElementById(gsNextButtonID);
	var sKeyCode;
	var oSrcElement;
	
	if(bIsNetscape)
	{			
		sKeyCode = "";
	}
	else
	{
		sKeyCode = event.keyCode;
		oSrcElement = event.srcElement;
	}
	

	if (sKeyCode == 13 && domNextButton != null)
	{
		var sSrcTagName = oSrcElement.tagName.toUpperCase();
		var sSrcId = oSrcElement.id;
		var bProceed = true;
		
		if(sSrcId==gsCustomButtonID || sSrcId==gsBackButtonID || sSrcId==gsResetButtonID)
		{
			bProceed = false;
		}
		else
		{					
			switch (sSrcTagName)
			{
				case "SELECT":
					if(event.srcElement.size < 2)
						bProceed = false;
						break;
				
				case "TEXTAREA":
					bProceed = false;
					break;
					
				default:
					bProceed = true;
					break;
			}
		}
					
		if(bProceed)
		{
			event.returnValue = false;
			event.cancel = true;			
			domNextButton.click();
		}
	}
}


function document_onkeypress_NS(e)
{		
	var oCustomButton = document.getElementById(gsCustomButtonID);
	var domNextButton = document.getElementById(gsNextButtonID);
	
	oCustomButton.onclick = "return false;";
	
	if(e.which == 13)
	{				
		domNextButton.click();
		return false;
	}
}


// ===================================================================================================
// Description	  : Hides or displays the section comment dom object
// Server control : MHSectionControl
function MHSectionControl_SectionComments_OnClick(domSpan)
{
	var inputColl = domSpan.getElementsByTagName("INPUT");
	var sectionCommentsId = inputColl[0].value;
	var domTrObject = document.getElementById(sectionCommentsId);	
				
	if(domTrObject.style.display == 'block')
	{
		domTrObject.style.display = 'none';		
	}
	else
	{
		domTrObject.style.display = 'block';		
	}
}


// ===================================================================================================
// Description	  : Used for GUI purposes in multi-option validation.
//					Sets the background color of cells in a matrix.
// Server control : MHMatrixQuestionControl/MHradioButtonMatrixControl
function GetParentElement(elementType, domElement)
{
	var i = 0;
	var domFoundElement = domElement;

	while(++i < 100 && domFoundElement != null)
	{			
		if(domFoundElement.tagName == elementType)
			break;		
			
		domFoundElement = domFoundElement.parentElement;
	}
	
	return domFoundElement;
}


// ===================================================================================================
// Description	  : Fires when a property changes on a validator. 
//					Updates the validationsummary.
// Server control : All MH Questionnaire validator controls
function MHValidator_OnPropertyChange(domValidator, bIsMatrix)
{		
	//Check if the validator has been enabled
	if(domValidator.style.visibility == 'visible' || domValidator.style.display == 'inline')
	{
		//Update validationsummary		
		gsValidationSummaryOnSubmitDef = ValidationSummaryOnSubmit.toString();
		gsValidationSummaryOnSubmitDef = gsValidationSummaryOnSubmitDef.replace("window.scrollTo(0,0);", " ");				
		window.setTimeout("MHValidationSummaryOnSubmit()", 1, "JavaScript");
	}
	
	if(bIsMatrix == true)
	{			
		MatrixControl_GroupValidator_OnPropertyChange(domValidator);
	}
}
function MHValidationSummaryOnSubmit()
{	
	window.execScript(gsValidationSummaryOnSubmitDef + " ValidationSummaryOnSubmit();", "JavaScript");
}


// ===================================================================================================
// Description	  : Used for GUI purposes in matrix-multi-option validation.
//					Sets the background color of cells in a matrix.
// Server control : MHMatrixQuestionControl
function MatrixControl_GroupValidator_OnPropertyChange(domValidator)
{	
	var domTD, domControl;	
	var domControls = domValidator.ControlIDs.split(";");
	
	//Check if the validator has been enabled
	if(domValidator.style.visibility == 'visible' || domValidator.style.display == 'inline')
	{			
		for(var i=0; i<domControls.length; i++)
		{
			domControl = document.getElementById(domControls[i]);
			domTD = GetParentElement("TD", domControl);						
			domTD.style.backgroundColor = domTD.CellErrorBackgroundColor;			
		}
	}
	else
	{
		for(var i=0; i<domControls.length; i++)
		{
			domControl = document.getElementById(domControls[i]);
			domTD = GetParentElement("TD", domControl);
			domTD.style.backgroundColor = "";
		}
	}
}


// ===================================================================================================
// Description	  : Fires when the validation summary displays
// Server control : MHQuestionnaireControl
function MHValidationSummary_OnPropertyChange(domMHValidationSummary)
{
	if(gbScrollToSummary)
		window.setTimeout("MHValidationSummary_OnPropertyChange_Timeout();", 1, "JavaScript");
}
function MHValidationSummary_OnPropertyChange_Timeout()
{
	if(!Page_IsValid && gbScrollToSummary)
	{
		document.location = "#MHValidationSummaryAnchor";
		gbScrollToSummary = false;
	}
}


// ===================================================================================================
// Description	  : Fires before the submit button is clicked
// Server control : MHQuestionnaireControl
function ForwardButton_OnBeforeActivate(domButton)
{
	gbScrollToSummary = true;
}


// =================================================================================================== 
// Description	  : Gets a MHTextLengthValidator as input. Returns true if the control it validates has less 
//				    charactes than the validator's maxLength attribute and more characters than the minLength
// Server control : MHTextLengthValidator 
function    MHTextLengthValidator_EvalFunction(domValidator)
{
	var text = "";
	text = domValidator.innerHTML;
	var value = ValidatorTrim(ValidatorGetValue(domValidator.controltovalidate));
	
	if(domValidator.allowNull != null && domValidator.allowNull.length > 0 && domValidator.allowNull && value.length == 0)
	{
		return true;
	}
	
	if (domValidator.maxLength != null && value.length > domValidator.maxLength)
	{
		domValidator.innerHTML = text;
		return false;
	}
	if (domValidator.minLength != null && value.length < domValidator.minLength)
	{
		domValidator.innerHTML = text;
		return false;
	}
	else return true;	
}



// =================================================================================================== 
// Description	  : Gets a MHInt64Validator as input. Returns true if the control the validator validates contains
//                  a Int64 value between the specified minimum and maximum values.
// Server control : MHInt64Validator 
function MHInt64Validator_EvalFunction(domValidator)
{
    try
    {
        var minValue = domValidator.minValue;
        var maxValue = domValidator.maxValue;
        var notInt64ErrorMessage = domValidator.notInt64ErrorMessage;
        var notInRangeErrorMessage = domValidator.notInRangeErrorMessage;
        
        var text = "";
	    text = domValidator.innerHTML;
	    var value = ValidatorTrim(ValidatorGetValue(domValidator.controltovalidate));
        
        //No entered value validates to 'true'.
        if(value.length == 0)
	    {
		    return true;
	    }

	    if(!IsInteger(value))
	    {
	        domValidator.innerHTML = text;
	        domValidator.errormessage = notInt64ErrorMessage;
		    return false;
	    }
    	
	    if(!IsIntegerInRange(value, minValue, maxValue))
	    {
	        domValidator.innerHTML = text;
	        var errorMessage = notInRangeErrorMessage;
	        errorMessage = errorMessage.replace(/\[MinValue\]/, minValue);
	        errorMessage = errorMessage.replace(/\[MaxValue\]/, maxValue);
	        domValidator.errormessage = errorMessage;
		    return false;
	    }
    	
	    return true;
	}
	catch(e)
	{
		window.status = "Scripterror: " + e.description;
	}
}

// =================================================================================================== 
// Description	  : MHInt64Validator_EvalFunction helper function. Checks if a value is an integer.
// Server control : MHInt64Validator 
function IsInteger (valueToEvaluate)
{
  var counter;
  
  //Removes the minus sign.
  if(valueToEvaluate.indexOf("-") == 0)
  {
    valueToEvaluate = valueToEvaluate.substr(1);
  }

  //Checks if the value is an integer
  for (counter = 0; counter < valueToEvaluate.length; counter++)
  {
     var character = valueToEvaluate.charAt(counter);

     if (!IsDigit(character)) 
     {
        return false;
     }
  }
  
  return true;
}


// =================================================================================================== 
// Description	  : MHInt64Validator_EvalFunction helper function. Checks if a character is a number.
// Server control : MHInt64Validator 
function IsDigit (character)
{
    var isDigit = ((character >= "0") && (character <= "9"));
    return isDigit;
}

// =================================================================================================== 
// Description	  : MHInt64Validator_EvalFunction helper function. Checks if an integer is in the specified range.
// Server control : MHInt64Validator 
function IsIntegerInRange (valueToEvaluate, minValue, maxValue)
{   

  // Catch non-integer strings to avoid creating a NaN below,
  // which isn't available on JavaScript 1.0 for Windows.
  if (!IsInteger(valueToEvaluate, false)) 
  {
    return false;
  }

  // Explicitly changes the type to integer via parseInt
  // so that the comparison code below will work both on
  // JavaScript 1.2 (which typechecks in equality comparisons)
  // and JavaScript 1.1 and before (which doesn't).
  var number = parseInt (valueToEvaluate);
  return ((number >= minValue) && (number <= maxValue));
}



function MHListBoxControl_OnBlur(domSelect)
{
	MHListBoxControl_OnChange(domSelect);
}

// =================================================================================================== 
// Description	  : Handles the clientside onclick event of the control
// Server control : MHListBoxControl 
function MHListBoxControl_OnChange(domSelect)
{		
	try
	{	
		var sListItemIsSingleChoiceID = domSelect.id + "_ListItemIsSingleChoice"
		var oListItemIsSingleChoiceID = document.getElementById(sListItemIsSingleChoiceID);
		var sListItemIsSingleChoice = oListItemIsSingleChoiceID.value;	
		var domOptions = domSelect.options;		
		var domSingleChoiceOption = null;	
		var ListItemIsSingleChoice = sListItemIsSingleChoice.split(";");
		
		for(var i=0; i<domOptions.length; i++)	
			if(domOptions[i].selected && ListItemIsSingleChoice[i] == "True")
				{domSingleChoiceOption = domOptions[i];	break;}
		
		if(domSingleChoiceOption != null)
			for(var i=0; i<domOptions.length; i++)					
				if(domOptions[i] != domSingleChoiceOption)			
					domOptions[i].selected = false;				
	}
	catch(e)
	{
		window.status = "Scripterror: " + e.description;
	}
}


// =================================================================================================== 
// Description	  : Gets a MHListBoxValidator as input. 
// Server control : MHListBoxValidator 
function MHListBoxValidator_EvalFunction(domValidator)
{	
	try
	{	
		var	selectedCount = 0;
		var domControlToValidate = document.getElementById(domValidator.controltovalidate);
		var domOptions = domControlToValidate.options;
		var bOnSubmit = !(domValidator.controltovalidate == window.event.srcElement.id);	
		var ListItemIsSingleChoice = null;			
		var sListItemIsSingleChoiceID = domControlToValidate.id + "_ListItemIsSingleChoice"
		var oListItemIsSingleChoiceID = document.getElementById(sListItemIsSingleChoiceID);
		var sListItemIsSingleChoice;
		
		if(oListItemIsSingleChoiceID != null)
		{
			sListItemIsSingleChoice = oListItemIsSingleChoiceID.value;		
			ListItemIsSingleChoice = sListItemIsSingleChoice.split(";");
		}
				
		//if(!bOnSubmit)
			//return true;
				
		for(var i=0; i<domOptions.length; i++)	
			if(domOptions[i].selected)
				selectedCount++;

		//If required and none selected
		if(domValidator.Required == "True" && selectedCount == 0)
		{	
			domValidator.innerHTML = "&nbsp;" + domValidator.HintMessage;
			domValidator.errormessage = domValidator.ErrorMessage;
			return false;
		}
		
		//If one is selected and it is a "single choice option"		
		if(ListItemIsSingleChoice != null && ListItemIsSingleChoice[domControlToValidate.selectedIndex] == "True")
		{
			return true;
		}

		//If some selected, but less than minumum required	
		if((selectedCount > 0) && (selectedCount < parseInt(domValidator.MinimumRequired)))
		{
			domValidator.innerHTML = "&nbsp;" + domValidator.HintMessage;
			domValidator.errormessage = domValidator.ErrorMessage;
			return false;
		}

		//If some selected, but more than maximum required	
		if((selectedCount > 0) && (selectedCount > parseInt(domValidator.MaximumRequired)))
		{	
			domValidator.innerHTML = "&nbsp;" + domValidator.HintMessage;
			domValidator.errormessage = domValidator.ErrorMessage;
			return false;
		}

		return true;	
	}
	catch(e)
	{
		window.status = "Scripterror: " + e.description;
		return true;
	}
}


// =================================================================================================== 
// Description    : Refreshes the info in the "Remaning points to distribute" label 
// Server control : MHPoolValidator 
function RefreshRemaingInfo(domValidator, iSum)
{
	var sLabelRemaining = domValidator.LabelRemainingClientID;
	var domLabelRemaining = document.getElementById(sLabelRemaining);
	var iPointToDistribute = parseInt(domValidator.PointToDistribute);
	var iPointsToDistRemaing = iPointToDistribute - iSum;
	domLabelRemaining.innerHTML = domValidator.PointToDistributeInfoRemaining.replace("[PointToDistributeRemaining]", iPointsToDistRemaing);
	
	if (iPointsToDistRemaing < 0)
		domLabelRemaining.style.color = "blue";
	else
		domLabelRemaining.style.color = "";
}


// =================================================================================================== 
// Description    : Refreshes the info in the "Remaning points to distribute" label 
// Server control : MHValuationQuestionControl
function MHValuationQuestionControl_RefreshRemaingInfo(domInput, iSum, labelRemainingClientID, pointToDistribute, pointToDistributeInfoRemaining)
{	
	var domLabelRemaining = document.getElementById(labelRemainingClientID);
	var iPointToDistribute = parseInt(pointToDistribute);
	var iPointsToDistRemaing = iPointToDistribute - iSum;
	domLabelRemaining.innerHTML = pointToDistributeInfoRemaining.replace("[PointToDistributeRemaining]", iPointsToDistRemaing);
	if (iPointsToDistRemaing < 0)
		domLabelRemaining.style.color = "#FF0000";
	else
		domLabelRemaining.style.color = "";
}


// =================================================================================================== 
// Description    : Refreshes the info in the "Remaning points to distribute" label 
// Server control : MHValuationQuestionControl
function MHValuationQuestionControl_OnKeyUp(domInput, textBoxIDs, labelRemainingClientID, pointToDistribute, pointToDistributeInfoRemaining)
{
	var sVal;
	var sArray;
	var iSum = 0;
	
	//Summerize points in the text boxes

	sArray = textBoxIDs.split(";");
	
	for (var i=0; i<sArray.length; i++)
	{			
		if(document.getElementById(sArray[i]) != null)
		{
			sVal = parseInt(document.getElementById(sArray[i]).value);			
			if (!isNaN(sVal))
			{
				iSum += parseInt(sVal);			
			}
		}
	}

	MHValuationQuestionControl_RefreshRemaingInfo(domInput, iSum, labelRemainingClientID, pointToDistribute, pointToDistributeInfoRemaining);
}


// =================================================================================================== 
// Description    : Verifies that an array of controls put together equals a pool of points 
// Server control : MHPoolValidator 
function MHPoolValidator_EvalFunction(domValidator)
{
	try
	{
		var iSum = 0;
		var sVal = "";	
		var domControlToValidate;
		var iControlToValidateValue = null;
		var bControlToValidateValue = false;
		var iMaximumValue = parseInt(domValidator.MaximumValue);
		var iMinimumValue = parseInt(domValidator.MinimumValue);
		var bIsValid = false;
		var bIsFirstControl = false;
		var iRankedCount = 0;
		var sArray;
		var bOnSubmit = (domValidator.textBoxIDs.indexOf(window.event.srcElement.id) == -1);
		var iPointToDistribute = parseInt(domValidator.PointToDistribute);
		
		
		//If any, get value from ControlToValidate	
		domControlToValidate = document.getElementById(domValidator.controltovalidate);	
		sControlToValidateValue = domControlToValidate.value;	
		if (sControlToValidateValue.length > 0)
		{
			bControlToValidateValue = true;
			iControlToValidateValue = parseInt(sControlToValidateValue);
		}
		
		//Summerize points in the text boxes
		sArray = domValidator.textBoxIDs.split(";");	
		for (var i=0; i<sArray.length; i++)
		{
			if(document.getElementById(sArray[i]) != null)
			{
				sVal = parseInt(document.getElementById(sArray[i]).value);
				if (!isNaN(sVal))
				{
					iSum += parseInt(sVal);
					iRankedCount++;
				}
			}
		}
		
		//Verify min
		if (iControlToValidateValue != null && iControlToValidateValue < iMinimumValue)
		{
			domValidator.errormessage = domValidator.MinimumValueErrorMessage;
			domValidator.innerHTML = domValidator.MinimumValueHint;
			return false;
		}	
		
		//Verify max
		if (iControlToValidateValue != null && iControlToValidateValue > iMaximumValue)
		{
			domValidator.errormessage = domValidator.MaximumValueErrorMessage;
			domValidator.innerHTML = domValidator.MaximumValueHint;
			return false;
		}	
			
		//Only verify on page submit!
		if (!bOnSubmit)
			return true;
		
		//Check if current textbox is the first in the array of text boxes
		if (domControlToValidate == document.getElementById(sArray[0]))
			bIsFirstControl = true;
		else
			bIsFirstControl = false;

		//Proceed if not required and none value given
		if(iRankedCount == 0 && domValidator.Required != "True")
			return true;

		//Verify that all controls are given values, if RequiredToRateAll
		if(!bControlToValidateValue && domValidator.RequiredToRateAll == "True" && iRankedCount != sArray.length)
		{
			domValidator.innerHTML = domValidator.AllRequiredHint;
			domValidator.errormessage = domValidator.AllRequiredErrorMessage;
			return false;
		}

		//Verify that value(s) are given if required	

		if(bIsFirstControl && domValidator.Required == "True" && iRankedCount == 0)
		{		
			domValidator.innerHTML = domValidator.RequiredHint;
			domValidator.errormessage = domValidator.RequiredErrorMessage;		
			return false;
		}

		//Verify sum			
		if (iSum == iPointToDistribute)
		{
			bIsValid = true;
		}
		else if(bIsFirstControl && iSum < iPointToDistribute)
		{				
			domValidator.innerHTML = domValidator.SumSmallHint.replace("[PointToDistributeSum]", iSum);		
			domValidator.errormessage = domValidator.SumSmallErrorMessage.replace("[PointToDistributeSum]", iSum);		
		}
		else if(bIsFirstControl && iSum > iPointToDistribute)
		{	
			domValidator.innerHTML = domValidator.SumLargeHint.replace("[PointToDistributeSum]", iSum);	
			domValidator.errormessage = domValidator.SumLargeErrorMessage.replace("[PointToDistributeSum]", iSum);	
		}
		else
		{
			bIsValid = true;
		}

		return bIsValid;	
	}
	catch(e)
	{
		window.status = "Scripterror: " + e.description;
		return true;
	}
}


// =================================================================================================== 
// Description    : Verifies that the control to validate contains a unique value in a sequence 
//				    among an array of controls 
// Server control : MHUniqueSequenceValidator 
function MHUniqueSequenceValidator_EvalFunction(domValidator)
{
	try
	{
		var sArray;
		var sSortArray = new Array();
		var sValue = "", sPrevValue = "";
		var domTextBox, domControlToValidate;	
		var iMaximumValue = parseInt(domValidator.MaximumValue);
		var iMinimumValue = parseInt(domValidator.MinimumValue);
		var bControlToValidateValue = false;
		var sControlToValidateValue = "";
		var lMinValue = null;
		var bIsValid = true;
		var bSequenceBegin = false;
		var iRankedCount = 0;
		var iCount = 0;	
		var bFirst = false;
		var bOnSubmit = (domValidator.textBoxIDs.indexOf(window.event.srcElement.id) == -1);
		
			
		//If any, get value from ControlToValidate
		domControlToValidate = document.getElementById(domValidator.controltovalidate);
		if(domControlToValidate == null) return true;
		
		sControlToValidateValue = domControlToValidate.value;
		
		if (sControlToValidateValue.length > 0)
		{
			bControlToValidateValue = true;
			iControlToValidateValue = parseInt(sControlToValidateValue);
		}
			 
		//Count boxes ranked, sort controls and verify first value
		sArray = domValidator.textBoxIDs.split(";");	
		for (var i=0; i<sArray.length; i++)		
		{	
			if(document.getElementById(sArray[i]) != null)
			{
				sSortArray[i] = document.getElementById(sArray[i]).value;
				if (sSortArray[i] == "1")
					bSequenceBegin = true;
				if (sSortArray[i].length > 0)
					iRankedCount++;			
				if ( !isNaN(parseInt(sSortArray[i])) && (lMinValue == null || parseInt(sSortArray[i]) < parseInt(lMinValue)) )
					lMinValue = sSortArray[i];
			}
		}	
		
		for (var i=0; i<sArray.length; i++)
		{
			bFirst = (sArray[i] == domValidator.controltovalidate);
			if(document.getElementById(sArray[i]) != null)
				break;
		}


		sSortArray = sSortArray.sort(CompareNumbersForSorting);
		
		//Proceed if not required and none value given	
		if(iRankedCount == 0 && domValidator.Required != "True")
			return true;
			
		//Verify that value(s) are given if required
		if(bOnSubmit && bFirst && iRankedCount == 0 && domValidator.Required == "True")
		{
			domValidator.errormessage = domValidator.MinimumRequiredErrorMessage;
			domValidator.innerHTML = domValidator.MinimumRequiredHint;
			return false;
		}
		
		//Verify that the first number is 1
		if (bOnSubmit && bControlToValidateValue && lMinValue == iControlToValidateValue && (!bSequenceBegin || lMinValue != 1))
		{
			domValidator.errormessage = domValidator.SequenceBeginErrorMessage;
			domValidator.innerHTML = domValidator.SequenceBeginHint;
			return false;
		}
				
		//Verify correct number of controls ranked (min)
		if (bOnSubmit && bFirst && iRankedCount < iMinimumValue)
		{
			domValidator.errormessage = domValidator.MinimumRequiredErrorMessage;
			domValidator.innerHTML = domValidator.MinimumRequiredHint;
			return false;
		}	
		
		//Verify correct number of controls ranked (max)
		if (bOnSubmit && bFirst && iRankedCount > iMaximumValue)
		{
			domValidator.errormessage = domValidator.MaximumRequiredErrorMessage;		
			domValidator.innerHTML = domValidator.MaximumRequiredHint;
			return false;
		}	
				
		//Verify correct sequence and unique values
		for (var i=0; i<sSortArray.length; i++)
		{			
			if (sSortArray[i] && sSortArray[i].length > 0)
			{						
				//Verify unique value			
				if ((sSortArray[i] == sPrevValue) && (sSortArray[i] == sControlToValidateValue))
				{
					domValidator.errormessage = domValidator.UniqueValueErrorMessage;											
					domValidator.innerHTML = domValidator.UniqueValueHint;											
					bIsValid = false;
				}
				//verify correct sequence
				else if (bOnSubmit && (sSortArray[i] == sControlToValidateValue) && (sPrevValue.length > 0) && (iControlToValidateValue != (parseInt(sPrevValue)+1)))
				{
					domValidator.errormessage = domValidator.SequenceErrorMessage;
					domValidator.innerHTML = domValidator.SequenceHint;
					bIsValid = false;
				}
			}	
			sPrevValue = sSortArray[i];	
		}
			
		return bIsValid;	
	}
	catch(e)
	{
		window.status = "Scripterror: " + e.description;
		return true;
	}
}

function CompareNumbersForSorting(a, b) {
    return a - b;
}
