function ShowErrors(validatorList)
{
    for (var i = 0; i < validatorList.length; i++)
    {
        var val = validatorList[i];
        
        if (val.isvalid)
        {
	        val.innerText = '';
	        val.style.display = "none";
        }
        else
        {
	        val.innerText = '*';
	        val.style.display = "inline";
        }
    }
}

function ValidateControls(validatorList)
{
    ValidatorSetHandlers(validatorList);

    if (!CheckValidators(validatorList))
     {  
      ShowErrors(validatorList);
      return false;
     }
        
    return true;
}

function CheckValidators(validatorList)
{
    var isFormValid = true;
    
    for (var i = 0; i < validatorList.length; i++)
    {
        var val=validatorList[i];
        val.isvalid = true;
        if (typeof(val.enabled) == "undefined" || val.enabled != false) 
         {
          if (typeof(val.evaluationfunction) == "function") 
             val.isvalid = val.evaluationfunction(val);
          }
        
        if (!val.isvalid )
            isFormValid = false;
    }
    return isFormValid;
}


function RequiredFieldValidatorEvaluateIsValid(val) 
{
    var control = document.getElementById(val.controltovalidate);
    
    if (control == null)
        alert('Control not found');
    
    if ((control.type == "radio") || (control.type == "checkbox"))
        return ValidateButtonList(val.controltovalidate);

    var value=ValidatorTrim(ValidatorGetValue(val.controltovalidate));
    var initialValue=ValidatorTrim(val.initialvalue);
    return ( value != initialValue)
}

////////////////////////////////////////////////////////
// Resize handlers
function onTextboxResize(resizedObjId, divId)
{
	document.getElementById(resizedObjId).style.width = document.getElementById(divId).style.width;
}

function onTextAreaResize(resizedObjId, divId)
{
	document.getElementById(resizedObjId).style.width = document.getElementById(divId).style.width;
	document.getElementById(resizedObjId).style.height = document.getElementById(divId).style.height;
}


///////////////////////////////////////////////////////////
// Misc functions



function checkDate(input)
{
  var mes="";
  if(input.value.length==0)
    return;


  var d=Date.parse(input.value);  

if(isNaN(d))
{
  alert("Invalid Date");} 
  return false;
}
function checkMaxLength(textAreaObj, maxLength)
{
    if (textAreaObj.value.length > maxLength)
    {
        alert('Text of this field can not be longer then ' + maxLength + ' characters');
        textAreaObj.value = textAreaObj.value.substring(0, maxLength);
    }
}

function clearText(inputObj)
{
    wasClickedValue = inputObj.getAttribute("wasClicked");
    
    if (!wasClickedValue)
        return;
    
    if (wasClickedValue == "false")
    {
        inputObj.value = '';
        inputObj.setAttribute("wasClicked", "true");
    }
}

function formatphones(textBoxObj)
{
    var cntry="United States of America";
	phoneFilter(textBoxObj, '(###) ###-####',cntry);
}
		
function phoneFilter(form, format, country) 
{
	var input = form.value;
	
	if (input.length == 7 &&
	    input.match('\\d+'))
    {
        input = '000' + input;
    }
	
	var cntrytobvldtd=((country=="United States of America")||(country=="USA")||(country=="US")||(country=="Canada"))
	if((input.length > 0)&&(cntrytobvldtd==true))
	 {  //do not perform if empty input
    	var numbers = ""; //store all the numbers here
        //process to remove non-numbers and spaces
        for(var i = 0; i < input.length; i++) 
        {
	        var char = input.charAt(i);
	        if(!(isNaN(char) || char == " ")) numbers += char;
        }
        //remove country code, if any
        var output = ""; //assign numbers here
        //assign numbers to chosen format
    	var n = 0, i = 0;
		while(i < format.length && n < numbers.length) 
		{
	        var char = format.charAt(i);
	        if(char == "#") 
	        {
	        	output += numbers.charAt(n++)
	        } 
	        else 
	        {
		        output += char;
	        }
	        i++;
        }
        //give alert if length is less than 8.
        if(numbers.length < 10) 
        {
	        window.alert("Please enter 10 digits phone");
	        form.focus();
        }
        form.value = output; //output to form				
    }	
}

///////////////////////////////////////
//Calendar functions

function CalendarDateIsValid(id)
{
 var v=$($(id).controltovalidate).value;
 if(v.length==0)
   return true;
 var ar=v.split("/");
 if(ar.length!=3)
    return false;
    
 var d=new Date(ar[0],ar[1]-1,ar[2]);  
 return (d.getFullYear()==ar[0]&& d.getMonth()==(ar[1]-1) && d.getDate()==ar[2] );

 
}

function calendarSetValue(id)
{
 var y=$(id+"Y");
 var m=$(id+"M");
 var d=$(id+"D");
 $(id).value=(y.value.length==0 || m.value.length==0|| d.value.length==0)? "": y.value +"/"+m.value+"/"+d.value;
}
///////////////////////////////////////////////////////////
// Validation functions


function LengthValidator(val)
{

 var maxlegth=parseInt(val.maxlegth);
 if(maxlegth<=0)
   return true;
  return $F(val.controltovalidate).length<=maxlegth;
}
function updateRatingScaleState(spanId)
{
    document.getElementById(spanId).setAttribute('WasChecked', 1);
}

function RatingScaleRequireEvaluateIsValid(val)
{
    RatingScaleStateEvaluateIsValid(val);
    return val.getAttribute('WasFilled')
}

function RatingScaleStateEvaluateIsValid(val)
{
    if (val.rowCount < 1)
    {
        val.setAttribute('WasFilled',1);
        return true;
    }

    var checkCount = 0;
    
    for (i = 0; i < val.rowCount; i++)
    {
        spanObj = document.getElementById(val.spanPrefix + i);
        
        if ( spanObj.getAttribute('WasChecked') )
            checkCount++;
    }

    if (checkCount > 0)
    {
        val.setAttribute('WasFilled', 1);
        return true;
    }
    return false;
}


// Updates e-mail list visibility
function invitationCheckClickHandler(checkBoxObj, tableId)
{
	var tableObj = document.getElementById(tableId);
	
	if (checkBoxObj.checked) 
		 tableObj.style.display = "inline";
	else
		 tableObj.style.display = "none";	
}


function ValidatorSetHandlers(validatorList) 
{
    if (validatorList == null)
        return;
        
    var i, val;
    for (i = 0; i < validatorList.length; i++) 
    {
        val = validatorList[i];
        
        if (typeof(val.evaluationfunction) == "string") 
            eval("val.evaluationfunction = " + val.evaluationfunction + ";");
            
        val.isvalid = false;
    }
}







// Value handlers
function ValidatorGetValue(id) 
{
    var control = document.getElementById(id);
    
    if (typeof(control.value) == "string") 
    {
        return control.value;
    }
    return ValidatorGetValueRecursive(control);
}

function ValidatorGetValueRecursive(control)
{
    if (typeof(control.value) == "string" && (control.type != "radio" || control.checked == true)) 
    {
        return control.value;
    }
    var i, val;
    for (i = 0; i<control.childNodes.length; i++) 
    {
        val = ValidatorGetValueRecursive(control.childNodes[i]);
        if (val != "") return val;
    }
    return "";
}


function ValidateButtonList(controlName)
{
    var i, radioButtons;
    
    radioButtons = document.getElementsByName(controlName);
    
    for (i = 0; i < radioButtons.length; i++)
    {
        if ((radioButtons[i].type == "radio") || (radioButtons[i].type == "checkbox")) 
        {
            if (radioButtons[i].checked == true)
                return true;
        }
    }
    return false;
}

function ValidatorTrim(s) 
{
    var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (m == null) ? "" : m[1];
}

/***********************************************/
// Validation functions


function MailListValidatorEvaluateIsValid(val) 
{
    var invitationCheckBox;
    invitationCheckBox = document.getElementById(val.invitationCtrlID);
    
    if (invitationCheckBox == null)
        alert('Control not found');
    if (invitationCheckBox.type != "checkbox")
        return false;
    if (invitationCheckBox.checked == false)
        return true;
    return RequiredFieldValidatorEvaluateIsValid(val);
}

function RegularExpressionValidatorEvaluateIsValid(val) 
{
    var value = ValidatorGetValue(val.controltovalidate);
    if (ValidatorTrim(value).length == 0)
        return true;
    var rx = new RegExp(val.validationexpression);
    var matches = rx.exec(value);
    return (matches != null && value == matches[0]);
}
