﻿//<![CDATA[

//_________________________________________________________________________________________________________________
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

//_________________________________________________________________________________________________________________

function ValidationResult(vValid, vMessage, vField){ 

    this.valid = vValid;
    this.message = vMessage;
    this.field = vField;
    
    return this;
}

//_________________________________________________________________________________________________________________

function DisplayOptInPageValidation(MessageList,ContainerId,HightlightError){
    //show on page validation
    if(ContainerId == null || ContainerId == '')
    {
        ContainerId = "OptInPageStatusMessage";
    }
        
    //This allows the validation message to hightlight the text box or not and 
    //the leave the text box class the as is
    if (HightlightError != null && HightlightError == false)
    {
        DisplayMessageOnly(MessageList,ContainerId);
    }
    else
    {
        //call messaging com
        DisplayMessage(MessageList,ContainerId);
    }
    
    
}

//_________________________________________________________________________________________________________________

//_________________________________________________________________________________________________________________

function DisplayValidation(MessageList,ContainerId,HightlightError){
    //show on page validation

    if(ContainerId == null || ContainerId == '')
    {
        ContainerId = "StatusMessage";
    }
    //This allows the validation message to hightlight the text box or not and 
    //the leave the text box class the as is
    if (HightlightError != null && HightlightError == false)
    {
        DisplayMessageOnly(MessageList,ContainerId);
    }
    else
    {
        //call messaging com
        DisplayMessage(MessageList,ContainerId);
    }
    
    
}

//_________________________________________________________________________________________________________________

//_________________________________________________________________________________________________________________

function DisplayCheckBalanceValidation(MessageList,ContainerId,HightlightError){
    //show on page validation
    if(ContainerId == null || ContainerId == '')
    {
        ContainerId = "CheckBalanceStatusMessage";
    }
    //This allows the validation message to hightlight the text box or not and 
    //the leave the text box class the as is
    if (HightlightError != null && HightlightError == false)
    {
        DisplayGCBalanceMessageOnly(MessageList,ContainerId);
    }
    else
    {
        //call messaging com
        DisplayGCBalanceMessage(MessageList,ContainerId);
    }
    
    
}

//_________________________________________________________________________________________________________________

//_________________________________________________________________________________________________________________

function DisplayRequestCatValidation(MessageList,ContainerId,HightlightError){
    //show on page validation
    if(ContainerId == null || ContainerId == '')
    {
        ContainerId = "RequestStatusMessage";
    }
    //This allows the validation message to hightlight the text box or not and 
    //the leave the text box class the as is
    if (HightlightError != null && HightlightError == false)
    {
        DisplayCatRequestMessageOnly(MessageList,ContainerId);
    }
    else
    {
        //call messaging com
        DisplayCatRequestMessage(MessageList,ContainerId);
    }
    
    
}

//_________________________________________________________________________________________________________________

function DisplayCustServValidation(MessageList,ContainerId,HightlightError){
    //show on page validation
    if(ContainerId == null || ContainerId == '')
    {
        ContainerId = "ContactStatusMessage";
    }
    //This allows the validation message to hightlight the text box or not and 
    //the leave the text box class the as is
    if (HightlightError != null && HightlightError == false)
    {
        DisplayContactMessageOnly(MessageList,ContainerId);
    }
    else
    {
        //call messaging com
        DisplayContactMessage(MessageList,ContainerId);
    }
    
    
}

//_________________________________________________________________________________________________________________

//_________________________________________________________________________________________________________________

function DisplayRegisterValidation(MessageList,ContainerId,HightlightError){
    //show on page validation
    if(ContainerId == null || ContainerId == '')
    {
        ContainerId = "RegisterStatusMessage";
    }
    //This allows the validation message to hightlight the text box or not and 
    //the leave the text box class the as is
    if (HightlightError != null && HightlightError == false)
    {
        DisplayRegisterMessageOnly(MessageList,ContainerId);
    }
    else
    {
        //call messaging com
        DisplayRegisterMessage(MessageList,ContainerId);
    }
    
    
}

//_________________________________________________________________________________________________________________


function IsNumeric(sText)
{
    var ValidChars = "0123456789.";
    var IsNumber=true;
    var Char;
    
    if (sText.length == 0)
    {
        return false;
    }

    for (i = 0; i < sText.length && IsNumber == true; i++) 
    { 
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) 
        {
            IsNumber = false;
        }
   }
   
   return IsNumber;
}

//_________________________________________________________________________________________________________________
function IsPasswordValid(pwd1, pwd2) {
    
    //pwd1 = pwd1.trim;
    //pwd2 = pwd2.trim;
    var MessageList = new Array(0);
    
    if (pwd1.value.trim().length >= 4){
        if (pwd1.value.trim() == pwd2.value.trim()){
            if (pwd1.value.trim().length <= 15){
                MessageList[MessageList.length] = new ValidationResult(true, PasswordUpdated, pwd1.id)
                MessageList[MessageList.length] = new ValidationResult(true, "", pwd2.id)
                
            } else {
                MessageList[MessageList.length] = new ValidationResult(false, InvalidPasswordFormat, pwd1.id)
                MessageList[MessageList.length] = new ValidationResult(false, "", pwd2.id)
            }
        } else {
            MessageList[MessageList.length] = new ValidationResult(false, PasswordsDontMatch, pwd1.id)
            MessageList[MessageList.length] = new ValidationResult(false, "", pwd2.id)
        }
    } else {
        MessageList[MessageList.length] = new ValidationResult(false, InvalidPasswordFormat, pwd1.id)
        MessageList[MessageList.length] = new ValidationResult(false, "", pwd2.id)
    }
    return MessageList;
}

//_________________________________________________________________________________________________________________
function IsValidLogin(accountName, accountPassword) {
    
    var MessageList = new Array(0);
   // alert(accountName.value);
    if (accountName.value.trim().length == 0){
        MessageList[MessageList.length] = new ValidationResult(false, InvalidEmail, accountName.id)
    }

    if ( (accountPassword.value.trim().length < 4) || (accountPassword.value.trim().length > 15) ){
        MessageList[MessageList.length] = new ValidationResult(false, InvalidPasswordFormat, accountPassword.id)
    } 
   // alert("IsValidLogin END");
    return MessageList;
}

//_________________________________________________________________________________________________________________

function IsValidZipCode(msgList,objZip,sZipErr,IsReq)
{
    sText = objZip.value;

    if (!sText.trim.length==0 || IsReq)
        {
     
            if (sText.length != 5)
                {
                msgList[msgList.length] = new ValidationResult(false, sZipErr, objZip.id);
                }
            else
                {
               
                if (IsNumeric(sText) ==false)
                    {
                      msgList[msgList.length] = new ValidationResult(false, sZipErr, objZip.id);
                     }
               }
               
               }
              
               return msgList;
    }
    
function IsValidEmailAddress(msgList,sEmail,sErr, IsReq)
{
    var objEmail=$(sEmail);
    if (objEmail.value.trim.length >0 || IsReq==true)
        {
        
        if (CheckIfEmailAddress(objEmail.value)==false)
            {
                msgList[msgList.length] = new ValidationResult(false, sErr, objEmail.id);
            }
        }
       return msgList;
}	

	function CheckIfEmailAddress(str) {

		var emailRegex  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var valid = emailRegex.test(str);
		return 	valid;
	}


 function IsValidPhone(msgList,sAreaCode,sExch,sPhoneLine,sErr,sReq)
{
    
    objAreacode =$(sAreaCode);
    objExch =$(sExch);
    objPhoneLine=$(sPhoneLine); 
    vAllBlank = objAreacode.value.length == 0 && objExch.value.length ==0 && objPhoneLine.value.length==0;
    vAllNumeric=false;
    if (IsNumeric(objAreacode.value)&& IsNumeric(objExch.value)&&IsNumeric(objPhoneLine.value) )
                    {
                   vAllNumeric=true;
                    }
              
          
    
        if(sReq||!vAllBlank)
            {
                if (!vAllNumeric||objAreacode.value.length != 3 || objExch.value.length !=3&& objPhoneLine.value.length!=4)
                    {
                    msgList[msgList.length] = new ValidationResult(false, sErr, objAreacode.id);
                  }
              }
              return msgList;
 }
 
 //date ddls should be surrounded by a div, assuming index 0 is not valid index
function IsValidDate(MessageList,divContainerId)
{
	var divContainer = $(divContainerId);
	//returns 
	var test = divContainer.getElementsBySelector("select").findAll(function(n){ return n.selectedIndex==0; });
	if(test.length > 0)	{MessageList[MessageList.length] = new ValidationResult(false, InvalidRegistryDate, divContainer.down("select").id);}
	return MessageList;
}
 
// //send in a textbox that is required, returns a msg list with an error
// function IsValidReqTextBox(msgList, sErr, sTxtBox)
// {
//    objTxtBox = document.getElementById(sTxtBox);
//	//objTxtBox = $(sTxtBox);
//	
//	if(objTxtBox.value.trim() == "")
//	{
//		msgList[msgList.length] = new ValidationResult(false, sErr, objTxtBox.id);
//	}
//	
//	
//	return msgList;
// }
 
 //simply checks to see if the DDL 
 function IsValidReqDDL(msgList, sError, sDDL)
 {
	objDDL = $(sDDL);
	
	if(objDDL.selectedIndex == 0)
	{
		msgList[msgList.length] = new ValidationResult(false, sError, objDDL.id);
	}
	
	return msgList;
 }

//send in a textbox that is required, returns a msg list with an error
 function IsValidReqTextBox(msgList, sErr, sTxtBox)
 {
	//objTxtBox = $(sTxtBox);
	objTxtBox = document.getElementById(sTxtBox);
	//.strip()
	if(objTxtBox.value.trim() == "")
	{
		msgList[msgList.length] = new ValidationResult(false, sErr, objTxtBox);
	}
	
	
	return msgList;
 }
 
 //_________________________________________________________________________________________________________________

function DisplayValidation(MessageList,ContainerId,HightlightError, DisplayType){
//    if($(clickedButtonId))
//    {
//		$(clickedButtonId).removeClassName(spinningClass);
//    }
    
    //show on page validation


    if(MessageList.length == 0)
		return;
		
	var container;
		
	if(ContainerId)
	{   
		container = $(ContainerId);
	}
	else
	{
		container = $("StatusMessage");
	}

    var type = DisplayType || "form";
    
    //This allows the validation message to hightlight the text box or not and 
    //the leave the text box class the as is
    
    if(type == "line") //if there is only one item, just a single validation
    {
		DisplayValidationLine(MessageList);
    }
    else if (HightlightError != null && HightlightError == false)
    {
        DisplayMessage(MessageList,container, false);
        $(container).scrollTo();
    }
    else
    {
        //call messaging com
       DisplayMessage(MessageList,container, true);
       $(container).scrollTo();
    }
    
}


function clearErrorMessages() {

	//reset form elements
	var classArray = $$('div.error','p.error');
	for (var i=0;i< classArray.length;i++)
	{
		classArray[i].removeClass("error");
	}

	//clear error messages
	var errorArray = $$('.infoMessage');
	for (var i=0;i< errorArray.length;i++)
	{
		errorArray[i].innerHTML = "";
	}
}


//]]>

