var prevDiv = null;         
  
 // Function to display the News Content
 
    function showNewsContent(divID){ 
        var titleDiv = document.getElementById("newsTitle" + divID);
        var contentDiv = document.getElementById("newsContent" + divID);
        if (prevDiv != null){
            hideNewsContent(prevDiv);
        }
        if (contentDiv != null)    {            
            prevDiv = divID;
            contentDiv.style.display = "block"; 
            titleDiv.className = "txtSelected";
        }
    }
    
    function hideNewsContent(prevDivID){
        var titleDiv = document.getElementById("newsTitle" + prevDivID);
        var contentDiv = document.getElementById("newsContent" + prevDivID);        
        if (contentDiv != null)
        {
            prevDiv = null;
            contentDiv.style.display = "none";
            titleDiv.className = "newsTxtNotSelected";            
        }
    }

 // Function to display the Column Content    

    function showColumnContent(divID){ 
        var titleDiv = document.getElementById("columnTitle" + divID);
        var contentDiv = document.getElementById("columnContent" + divID);
        if (prevDiv != null){
            hideColumnContent(prevDiv);
        }
        if (contentDiv != null)    {            
            prevDiv = divID;
            contentDiv.style.display = "block"; 
            titleDiv.className = "txtSelected";
        }
    }
    
    function hideColumnContent(prevDivID){
        var titleDiv = document.getElementById("columnTitle" + prevDivID);
        var contentDiv = document.getElementById("columnContent" + prevDivID);        
        if (contentDiv != null)
        {
            prevDiv = null;
            contentDiv.style.display = "none";
            titleDiv.className = "columnTxtNotSelected";            
        }
    }



//Function to sort  News elements in the Welcome page

 var myData = new Array();
    function Data(id,text) {
        this.Id = parseInt(id);
        this.aText = text;
    }
    
    function sortById(a, b) {
        var x = a.Id;
        var y = b.Id;
        return ((x > y) ? -1 : ((x < y) ? 1 : 0));
    }
    
    function sortElements(){
        var contentDiv = document.getElementById("lstContent");
        var containedDivElements = contentDiv.getElementsByTagName("span");
        for (var i = 0; i < containedDivElements.length; i++) {
            var elem = containedDivElements[i];
            myData[i] = new Data(elem.id,elem.innerHTML);
        }
        myData.sort(sortById);
        displayvalues();
    }
    
    function displayvalues(){
        var d= new Date(); 
        lastYearDate = String(((d.getFullYear() -1) * 100 + d.getMonth() + 1) * 100 + d.getDate());
        var contentnewDiv = document.getElementById("newlstcontent");
        for (var i=0; i<4; i++) {
            if(myData[i].Id > lastYearDate){
            contentnewDiv.innerHTML += "<div  style='padding-top:5px;' id='" + myData[i].Id + "'>" + myData[i].aText +"</div>";
            }
        }
    }

// Function for the onclick and keypress event in form elements 

 onclicktext=function(e){
    var keynum;
    if(window.event) // IE  
        keynum = e.keyCode;    
    else if(e.which) // Netscape/Firefox/Opera  
        keynum = e.which;
    if((e.type=="click") || (e.type=="keypress" && keynum==13)){
        return false;
    }else{
        return true;
    }
};

// Function to view Formula Investing Funds website
    function viewFundsPage(e){
     if(!onclicktext(e)){
       window.location.href='http://www.formulainvestingfunds.com';
     }    
    }


// Function to view Formula Investing website

    function viewpage(e){
     if(!onclicktext(e)){
       window.location.href='http://www.formulainvesting.com';
     }    
    }
    
// Function to confirm to the popup to view FT

        function confirmpage(left,top,e){
        layout = document.getElementById("Layer");
        if(!onclicktext(e)){ 
             if(layout){
                layout.style.display='block';
                layout.style.visibility='visible';
                layout.style.top=top;
                layout.style.left=left;
            }
        }
    }
    
   
// Function to cancel the popup
 
      function cancelpage(e){
        layout = document.getElementById("Layer");
        if(!onclicktext(e)){ 
             if(layout){
                layout.style.display='none';
                layout.style.visibility='hidden';
            }
        }
    }
 
// Function to hide all the div layers and show a specific layer

showLayer=function(formName,layerId){
hideAllLayer(formName);
_l=document.getElementById(layerId);
document.getElementById(layerId).style.display="block";
};

hideAllLayer=function(formName){
if(formName=='contactForm'){
   ids=new Array('MandatoryMsg','EmailMsg'); 
}
if(formName=='registerForm'){
   ids=new Array('MandatoryMsg','EmailMsg','IDExists','PasswordErrMsg','AgreeMsg','PwdBlankMsg'); 
}
if(formName=='loginForm'){
   ids=new Array('MandatoryMsg','EmailMsg','InvalidID','InvalidPwd','IDLocked','NotyetActive','AlreadyActive','InvalidIDPwd','sspage'); 
}
if(formName=='emailForm'){
   ids=new Array('MandatoryMsg','EmailMsg','InvalidID'); 
}
if(formName=='resendForm'){
   ids=new Array('MandatoryMsg','EmailMsg','InvalidID'); 
}
if(formName=='AccntActivateForm'){
   ids=new Array('MandatoryMsg','EmailMsg','InvalidID','InvalidPwd','AlreadyActive','InvalidCode','Activate'); 
}
if(formName=='ChangePwdForm'){
   ids=new Array('MandatoryMsg','InvalidOldPwd','samePwd','ActivateAcct','PwdChanged','PasswordErrMsg','PwdBlankMsg'); 
}
if(formName=='editProfileform'){
   ids=new Array('MandatoryMsg','PasswordErrMsg','PwdBlankMsg'); 
}
for (var i=0;i<ids.length;i++){
        hideLayer(ids[i]);
    }
}

hideLayer=function(layerId){
    _l=document.getElementById(layerId);
    if(_l){
        document.getElementById(layerId).style.display="none";
    }
};

//Function to show the error in form elements

addError=function(eleId){
    _eI=document.getElementById(eleId);
    if(_eI){
        _eI.style.color="red";
    }
};
addErrors=function(eleIds){
    for(count=0;count<eleIds.length;count++){
        addError(eleIds[count]);
    }
};

//Function to remove the error from  form elements

removeError=function(eleId){
    _eI=document.getElementById(eleId)
    if(_eI){
        _eI.style.color="black";
    }
};
removeErrors=function(eleIds){
    for(count=0;count<eleIds.length;count++){
        removeError(eleIds[count]);
    }
};


// Function to validate the Form

validateForm=function(fId){ //to check null for  all the text and passowrd fields    
    isFormValid=true;
    f=document.getElementById(fId);
    for(i=0;i<f.elements.length;i++){
        _e=f.elements[i];
        
        if((_e.type=="text")|| (_e.type=="password") || (_e.type=="textarea")){            
             if(trim(f.elements[i].value)=="") {      
            addError(_e.id+"_td");
            isFormValid=false; 
            }  
            else{
            removeError(_e.id+"_td");
            }
        }                
    }
    return isFormValid;
};

validateFormElements=function(fId){ //to match/find the values in the form 
    f=document.getElementById(fId);
    isElementValid=true;
    for(i=0;i<f.elements.length;i++){
        _e=f.elements[i];
        
        if(_e.type=="checkbox"){
            chkbox=document.getElementById(_e.id);
            if(!chkbox.checked){
                showLayer(fId,"AgreeMsg");
                 isElementValid=false;
                 break;                    
            }    
            else{
                hideLayer("AgreeMsg");
            }                       
        }
        
        
        if(_e.type=="text"){
        var emailpattern = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
        var acctcodepattern = /^\d{5}$/;
                if(_e.id=="email"){
                   
                    if(!(checkPattern(_e.value,emailpattern)))
                    {
                    showLayer(fId,"EmailMsg");
                    isElementValid=false;
                    break;
                       }                   
               }
              else if(_e.id=="activationcode"){               
                    if(!(checkPattern(_e.value,acctcodepattern)))
                    {
                    showLayer(fId,"InvalidCode");
                    isElementValid=false;
                    break;
                       }                   
               }
        }
        
         if(_e.type=="password"){
          eleValue=_e.value;
          if(eleValue.split(' ').length>1){
                addError(_e.id+"_td"); 
                showLayer(fId,"PwdBlankMsg");
                isElementValid=false;
                break; 
            }         
            if(_e.id.substring(0,1)!="r"){ 
                rePass=document.getElementById("r"+_e.id);
                _eTValue=trim(eleValue);
                if(rePass){
                    rePassTValue=trim(rePass.value);                
                    if(_eTValue!=rePassTValue){
                        addErrors([_e.id+"_td",rePass.id+"_td"]); 
                        showLayer(fId,"PasswordErrMsg");
                        isElementValid=false;
                        break;
                      }  
                    else{
                        removeErrors([_e.id+"_td",rePass.id+"_td"]);
                    }   
                }             
            }
        }
        
    }
    return isElementValid;
};

submitForm=function(fId){
        if(validateForm(fId)){
            hideLayer("MandatoryMsg");
            if(validateFormElements(fId)){
            return true;
            }
            return false;
        }else{
            showLayer(fId,"MandatoryMsg");
            return false; 
        }
};

//Function to check whether the string matches the pattern

checkPattern=function(txtvalue,pattern){
    if(!(txtvalue.match(pattern))){
              return false;
         }    
return true;            
}
