_isFormValid=true;
var win=null;
function setActivetab(){
//            try{
    var headerdiv=document.getElementById('mainNav');
    var selectedtab=null;
    try{selectedtab=document.getElementById("selTab").value;} catch(e){}
    var elementUL=(document.all?headerdiv.all[0]:headerdiv.childNodes[1]);
    
    //------Remove selection from all Tabs------
    for(i=0;i<elementUL.childNodes.length;i++)
    {
        if(elementUL.childNodes[i].nodeName=="LI"){
            if((document.all && i==0) || (!document.all && i==1))
            {
                document.getElementById("imghome_101").src="/images/ico_home_off.gif";
                elementUL.childNodes[i].className="off home";
            }
            else
            {
                if(elementUL.childNodes[i].className.indexOf("doubleLine")!=-1)
                    {elementUL.childNodes[i].className="off doubleLine";}
                else    
                    {elementUL.childNodes[i].className="off";}
            }
        }
    }
    //-----------------------------------------
    
    if(typeof(selectedtab)=='undefined' || selectedtab==null) {return;}
    
    //------------Set Tab Active---------------
    if(document.getElementById(selectedtab).className.indexOf("doubleLine")!=-1)
        {
            document.getElementById(selectedtab).className="on doubleLine";
            document.getElementById(selectedtab).firstChild.style.paddingBottom="7px";
        }
    else
    { 
        if(document.getElementById(selectedtab).className.indexOf("home")!=-1)
        {
            document.getElementById("imghome_101").src="/images/ico_home_on.gif";
            document.getElementById(selectedtab).className="on";
        }
        else
        {
            document.getElementById(selectedtab).className="on";
        }
    }    
    //              } catch(e){alert(e);}      
    //-----------------------------------------
}

function checksalutation(){
    salutationOptions=document.getElementsByName('salutation');
    var check=false;
    for(var i=0;i<salutationOptions.length;i++)
    {
        check=(check || salutationOptions[i].checked);
    }
    return check;
}

function validateElement(Element,Caption,Type){
    isValid=false;

    switch(Type)
    {
        case "salutation":  isValid=checksalutation();
                            break;
        case "firstname":   isValid=myValidator.validateFirstName(Element);
                            break;
        case "lastname":    isValid=myValidator.validateLastName(Element);
                            break;     
        case "address1":    isValid=myValidator.validateAddress1(Element);
                            break;
        case "address2":    isValid=myValidator.validateAddress2(Element);
                            break;
        case "city":        isValid=myValidator.validateCity(Element);
                            break;
        case "state":       isValid=myValidator.validateState(Element);
                            break;
        case "zip":         isValid=myValidator.validateZipCode(Element);
                            break;
        case "email":       isValid=myValidator.validateEmailAddress(Element);
                            break;
        case "emailconfirm":isValid=myValidator.validateEmailAddress(Element);
                            break;                    
        case "age":         isValid=(Element.checked);
                            break;
        case "suffix":      isValid=myValidator.validateSuffix(Element);
                            break;
        default :           isValid=(Element.value.trim().length > 0);
                            break;                                        
    }
    
    if(!isValid)
    {
        changeElementClass(Caption,true);
        _isFormValid=false;
    }
    else
    {
        changeElementClass(Caption,false);
        _isFormValid=true;
    }
}

function changeElementClass(Caption,isSetRemove)
{
    //----------------------------------------------------------------------------------------------------
    //isSetRemove=true -  add error class if not already added, otherwise remove error class.
    //Change class name of multiple elements in case of error. These element Ids must be seperated by '~'.
    //----------------------------------------------------------------------------------------------------
    if(Caption==null) {return;}
    for(k=0;k<Caption.split("~").length;k++)
    {
        var caption=document.getElementById(Caption.split("~")[k]);
        if(isSetRemove)
        {
            if(caption.className.indexOf("error") == -1)
                caption.className=caption.className+" error";
        }
        else
        {
            if(caption.className.split(' ')[0]!='error')
                caption.className=caption.className.split(' ')[0];
            else
                caption.className=""; 
        }
    }
}

function validateEmailForm(){
    var flag=true;
    for(var i=0;i < frmEleArr.length;i++)
    {
        validateElement(document.getElementById(frmEleArr[i].split("|")[0]),frmEleArr[i].split("|")[1],frmEleArr[i].split("|")[2]);
        flag=flag && _isFormValid;
    }

    //if(!flag)
    {
        document.getElementById("dvErrorMessage").style.display=(flag?"none":"block");
        //return false;                
        return flag;
    }                
}

function validateForm(){
    var flag=true;
    for(var i=0;i < frmEleArr.length;i++)
    {
        validateElement(document.getElementById(frmEleArr[i].split("|")[0]),frmEleArr[i].split("|")[1],frmEleArr[i].split("|")[2]);
        flag=flag && _isFormValid;
    }
    if(document.getElementById("addressTwo")!=null && document.getElementById("addressTwo").value!="")
    {
        validateElement(document.getElementById("addressTwo"),"lbladdressTwo","address2");
        flag=flag && _isFormValid;
    }
    if(document.getElementById("other")!=null && document.getElementById("other").checked && document.getElementById("othermed").value.trim().length > 0)
    {
        validateElement(document.getElementById("othermed"),"lblOtherMeds","lastname");
        flag= flag && _isFormValid;
    }
//    else
//    {
//        if(document.getElementById("lblOtherMeds").className.indexOf("error")>-1){document.getElementById("lblOtherMeds").className=document.getElementById("lblOtherMeds").className.split(" ")[0];}
//        flag= flag && true;
//    }
    if(document.getElementById("email")!=null && document.getElementById("email").value.trim().length > 0)
    {
        try{
            validateElement(document.getElementById("email"),"lblemail","email");
            flag=flag && _isFormValid;
            
            validateElement(document.getElementById("emailConfirm"),"lblemailConfirm","email");            
            flag=flag && _isFormValid;
            
            var confirmEmail=(document.getElementById("email").value==document.getElementById("emailConfirm").value)
            if(!confirmEmail || !_isFormValid)
                changeElementClass("lblemailConfirm",true);
            else
                changeElementClass("lblemailConfirm",false);
            
            flag=flag && confirmEmail;
        }
        catch(e){}
    }
    
    BMSIs_PageValid=flag;
    
    //if(!flag)
    {
        document.getElementById("dvErrorMessage").style.display=(flag?"none":"block");
        //return false;                
        return flag;
    }    
}

function validateSearch(searchbox) {
    var val = document.getElementById(searchbox).value.trim();
    if (val == null || val.length==0) {
        alert("Please type the word or words you wish to search for in the search box.");
        return false;
    } 
    else {
        var url=homeURL.substr(0,homeURL.lastIndexOf("/"));
        window.location.href = url+'/Search.aspx?q=' + val;
    }
}

// This function is used only from Search page. This was required to resolve IE issue
// with location.href used in validateSearch() function.
function validateSearchQuery(searchbox)
{
    var val = document.getElementById(searchbox).value.trim();
    if (val == null || val.length==0) {
        alert("Please type the word or words you wish to search for in the search box.");
        return false;
    } 
}

function checkSurveyData()
{
    var message = AjaxProxy.ShowHideGet().value;
    var dvS = document.getElementById("dvSurvey");
    var dvR = document.getElementById("dvSurveyResult");
	if (dvS == null && dvR == null) 
	{ 	
	    return false; 
	}
    else
    {
        if(message!=null && message != 0)
        {
            //Result Table
            getSurveyResult();
            document.getElementById("dvSurveyResult").style.display = "block";//result
            document.getElementById("dvSurvey").style.display = "none";//question
        }
        else
        {
            //Question Table
            document.getElementById("dvSurveyResult").style.display = "none";//result
            document.getElementById("dvSurvey").style.display = "block";//question
            loadSurvey();
        }
    }
}

function loadSurvey()
{
    var arrAllQs = new Array();
    var arrNotYetShownQs = new Array();
    var SurveyList = AjaxProxy.GetPageSurveyData("index",6);
    var SurveyData =  SurveyList.value;
    var ShownQKeys = AjaxProxy.GetShownQ().value;
    
    for(var a=0;a < SurveyData.length; a++)
    {
        arrAllQs[a] = SurveyData[a].QuestKey;
    }
    arrAllQs.sort();
    var prevQues = 0;
    var _incr = 0;    

    for(var b=0;b < arrAllQs.length ; b++)
    {
        if (prevQues == arrAllQs[b])
        {
            prevQues = arrAllQs[b];
        }
        else
        {
            if(ShownQKeys.indexOf(arrAllQs[b]) == -1)
            {
                arrNotYetShownQs[_incr] = arrAllQs[b];
                _incr = _incr + 1;
            }
            prevQues = arrAllQs[b];
        }
    }
    arrNotYetShownQs.sort();        
        
    //Get Random Question
    var whichQuestion=get_random(arrNotYetShownQs.length);

    var _clientID = 0;
    var _QuestionText;
    for (var z=0; z< SurveyData.length; z++)
    {
        if (SurveyData[z].QuestKey == arrNotYetShownQs[whichQuestion] && ShownQKeys.indexOf(SurveyData[z].QuestKey)==-1)
        {
            _clientID = SurveyData[z].QuestClientID;
            _QuestionText = SurveyData[z].QuestText;
        }
    }
    
    if(typeof(_QuestionText)=='undefined' || _QuestionText=="")
    {
        AjaxProxy.ShowHideSet(1);
        checkSurveyData();
        return;
    }
    
    var qDiv = document.getElementById("dvSurveyQ");
    if(qDiv!=null)
    {
        spn=document.createElement("span");
        spn.setAttribute("id",_clientID);
        spn.style.styleFloat="left"; //("style","float: left; width: 200px; font-family: arial, sans-serif; font-size: 11px; margin: 5px 0px;");
        spn.style.width="200px";
        spn.style.fontFamily="arial, sans-serif";
        spn.style.fontSize="11px";
        spn.style.margin="5px 0px";
        spn.innerHTML=_QuestionText;
        qDiv.appendChild(spn);
    }
    
    // Iterate and fetch related answers for the current question     
    aDiv=document.getElementById("dvSurveyA");
    tbody = document.getElementById("tbSurvey");
    clearTable(tbody);
    j=0;
    if(aDiv!=null)
    {
        for (var i = 0; i < SurveyData.length; i++) 
        {
            if (SurveyData[i].QuestKey == arrNotYetShownQs[whichQuestion])
            {
                if(j==0) {tr = tbody.insertRow(tbody.rows.length);}
                
                l=document.createElement("label");
                //l.setAttribute("class",SurveyData[i].AnsText.length < 5?"Poll":SurveyData[i].AnsText.length < 10?"PollLong":"PollXLong");
                l.setAttribute("style","float: left; line-height: 1em;");
                //td='td'+i;
                td = tr.insertCell(-1);
                if(tr.cells.length==1){td.setAttribute("class","first");}
                
                Ans=document.createTextNode(SurveyData[i].AnsText);
                if(Ans.nodeValue.indexOf('<BR>')>-1) {
                    arAns=Ans.nodeValue.split('<BR>');
                    br=document.createElement('BR');
                    l.appendChild(document.createTextNode(arAns[0]));
                    l.appendChild(br);
                    l.appendChild(document.createTextNode(arAns[1]));
                }
                else
                {
                    l.appendChild(Ans);
                }
                if(document.all && !window.opera && document.createElement)
                {
                    r=document.createElement("<input type='"+SurveyData[i].AnsClientType+"' Name='awarenessParticipation' id='"+SurveyData[i].AnsClientID+"' />");                        
                }
                else
                {
                    r=document.createElement("input");                        
                    r.setAttribute("type",SurveyData[i].AnsClientType);
                    //r.className="radioAnswers";
                    r.setAttribute("id",SurveyData[i].AnsClientID);
                    r.setAttribute("name","awarenessParticipation");
                    r.setAttribute("value","true");
                }
//                aDiv.appendChild(r);
//                aDiv.appendChild(l);
                td.appendChild(r);
                td.appendChild(l);
                
                j+=1;
                if((j % 2 == 0) && (j > 0))
                {
//                    br=document.createElement("br");
//                    aDiv.appendChild(br);
                    tr = tbody.insertRow(tbody.rows.length);
                }
            }             
        }
        
        if(tbody.rows[tbody.rows.length-1].cells.length==0){tbody.deleteRow[tbody.rows.length-1];}
//        if(aDiv.lastChild.tagName=="BR"){aDiv.removeChild(aDiv.lastChild);}
        a=document.createElement("a");
        img=document.createElement("input");
        img.setAttribute("type","image");
        img.setAttribute("id","submit_116");
        img.className="pollSubmit";
        img.setAttribute("value","Submit");
        img.setAttribute("name","submit");
        img.setAttribute("src","/images/btn_vote.gif");
//        img.style.marginTop="-1.25em";
//        img.style.width="42px";
//        img.style.height="17px";
        img.onclick=new Function("evt","javascript:return QuestionNav();");
        a.appendChild(img);
        aDiv.appendChild(a);
    }    
      AjaxProxy.SessSet(arrNotYetShownQs[whichQuestion]);
}

function get_random(arrLen)
{
    var ranNum= Math.floor(Math.random()*arrLen);
    return ranNum;
}

function getSurveyResult()
{
//Question Primary Key
    var QuestionKey=AjaxProxy.SessGet().value;
    var CntList = AjaxProxy.GetAllAnsCount(QuestionKey);  
      
    // Get Question Assigned to Page 
    var SurveyList = AjaxProxy.GetPageSurveyData("index",6);
    var SurveyData =  SurveyList.value;  
    var _QuestionText;
    
    for (var z=0; z< SurveyData.length; z++)
    {
        if (SurveyData[z].QuestKey == QuestionKey)
        {
            _QuestionText = SurveyData[z].QuestText;
        }
    }
    
    var qDiv = document.getElementById("dvSurveyQnA");
    if(qDiv!=null)
    {
        qDiv.innerHTML=_QuestionText;
    }
     
    var AnsLst =  CntList.value;   
    var tr, td, imgt;

    tbody = document.getElementById("tbSurveyResult");
    clearTable(tbody);
    
    var sum = 0;
    for (var j = 0; j < AnsLst.length; j++) 
    {
        sum = sum + AnsLst[j].AnsCount;
    }
    
    for (var i = 0; i < AnsLst.length; i++) 
    {
        if(AnsLst[i].AnsCount==0){continue;}
        
        tr = tbody.insertRow(tbody.rows.length);
        td = tr.insertCell(-1);
        td.innerHTML=AnsLst[i].AnsText;    
                
        var img = document.createElement('img');
        img.setAttribute("src", "/images/poll.gif");   
        img.setAttribute("height", "10");  
        img.setAttribute("border", "0");  
        percentage = (AnsLst[i].AnsCount / sum * 100);
        percentage = Math.round(percentage);
        var w = percentage;
        img.setAttribute("width", w);
        
        td = tr.insertCell(-1);
        td.appendChild(img);

        td = tr.insertCell(-1);
        td.innerHTML=percentage + "%";

        AjaxProxy.ShowHideSet(0);        
    }
    
    AjaxProxy.SetShownQ(QuestionKey);
}

function QuestionNav()
{
    if(!checkAnswer())
        return false;

    AjaxProxy.ShowHideSet(1);
}

function checkAnswer(){
    var check=false;
    aDiv=document.getElementById("dvSurveyA");
    radioAnswers=aDiv.getElementsByTagName('input');
    var check=false;
    for(var i=0;i<radioAnswers.length;i++)
    {
       if(radioAnswers[i].type == 'radio'){
            check = (check || radioAnswers[i].checked);
        }
    }
    return check;    
}

function clearTable(tbody) {
    while (tbody.rows.length > 0) {
        tbody.deleteRow(0);
    }
}

function confirmExit(url,googletag){
    var homeurl=homeURL.substr(0,homeURL.lastIndexOf("/"));
    win=window.open(homeurl+"/SiteExit.aspx?url="+escape(url)+"&googletag="+escape(googletag),"ExitSite","height=350px,width=610px,status=no,menubar=no,location=no");
}

function leaveSite(url){
    window.location=url;
    if(win!=null){win.close();}    
}
function InvokeFloodlightTagOnClick(category) {
    var newimage = document.createElement('script');
    var axel = Math.random() + "";
    var a = axel * 10000000000000;
    var source = "http://fls.doubleclick.net/activityj;src=2381103;type=reyat302;cat=" + category + ";ord=" + a + "?"
    newimage.setAttribute("src", source);
}
