//Collection of scripts used in Cogito Marketing Intelligence

                                //------------------------------------//
                                //       Commmon Functions            //
                                //------------------------------------//
function init(ClassName){
	var stretchers = document.getElementsByClassName('box');
	
    var toggles = document.getElementsByClassName(ClassName);
    toggleSelected = document.getElementById("toggleSelected");	
	var myAccordion = new fx.Accordion(toggles, stretchers, {opacity: false, height: true, duration: 600},toggleSelected);
	
	//hash functions
	var found = false;
	toggles.each(function(h3, i){
		var div = Element.find(h3, 'nextSibling');
            if (window.location.href.indexOf(h3.title) > 0) {
				myAccordion.showThisHideOpen(div);
				found = true;
			}
		});

       if (!found) 
       {
            showStrechers(toggleSelected.value);
            myAccordion.showThisHideOpen(stretchers[toggleSelected.value]);
       }
}


var mv_XMLHttpRequest;

try
{
	// Firefox, Opera 8.0+, Safari
	mv_XMLHttpRequest=new XMLHttpRequest();
}
catch (bv_Exception)
{
	// Internet Explorer
	try
	{
		mv_XMLHttpRequest=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (bv_Exception)
	{
		try
		{
			mv_XMLHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (bv_Exception)
		{
			alert("Your browser does not support AJAX!");
		}
	}
}

String.prototype.chomp = function ()
{
  return this.replace(/[\n\r\t]/g,''); 
};

function call(url,pars)
{

    if (mv_XMLHttpRequest)
    {
        mv_XMLHttpRequest.onreadystatechange = function(){
		    if(mv_XMLHttpRequest.readyState == 4){
			    var ajaxDisplay = document.getElementById('boxGrafico');
			    var risultato = mv_XMLHttpRequest.responseText.chomp();
//			    var viewstatePos = risultato.indexOf("id=\"__VIEWSTATE\"");
//			    if (viewstatePos != -1)
//			    {
//			        var viewstateStartValuePos = risultato.indexOf("value=\"",viewstatePos)+7;
//			        var viewstateEndValuePos   = risultato.indexOf("\"",viewstateStartValuePos);
//			        var viewstateVal = risultato.slice(viewstateStartValuePos,viewstateEndValuePos);
//			        var viewstate = document.getElementById("__ViewState");
//			        if (viewstate)
//			            viewstate.value+=viewstateVal;
//			    }
			    var startPos = risultato.indexOf("<div id='areaGrafico'");
			    if (startPos != -1)
			    {
			      var endPos = risultato.lastIndexOf("</div>");
			      if (endPos != -1)
			      {
			          startPos+=22;//skip <div id="areaGrafico">
			          ajaxDisplay.innerHTML = risultato.slice(startPos,endPos);
			      }
			    }
		    }
    	}
    	
        mv_XMLHttpRequest.open("GET", url + pars, true);
	    mv_XMLHttpRequest.send(null); 
    }
}
        
function mf_GetResponse(lp_URL)
{
	mv_XMLHttpRequest.open("GET", lp_URL, false);
	mv_XMLHttpRequest.send(null);
	return mv_XMLHttpRequest.responseText;
}

function mf_SelectToggle(number)
{
  toggleSelected = document.getElementById("toggleSelected");
  toggleSelected.value = number;
  showStrechers(number);
}


function showStrechers(number)
{
    var stretchers = document.getElementsByClassName('box');
    for(var i=0; i < stretchers.length; i++)     
      {
        if (i != number)
            stretchers[i].style.display = "none";
      }
      
      stretchers[number].style.display = "block";
}

function cleanAndCallInit(ClassName) {
    Element.cleanWhitespace('content');
	init(ClassName);
}

                          
                            //------------------------------------//
                            //Function used by VotesDrillDown.aspx//
                            //------------------------------------//
function mf_VoteChangeRequest(lp_User, lp_VoteID, lp_SuggestedRating, lp_Confirmation)
{
    var lv_ConfirmationMessage;
    var lv_URL;
    
    if(lp_SuggestedRating == 0)
    {
        if(!confirm("By signaling this vote's driver is wrong, you are asking to DELETE the vote. Is this really what you want to do?"))
        {
            return;
        }
    }
    
    lv_URL = "VoteChangeRequest.aspx?user=" + lp_User + "&vote_id=" + lp_VoteID + "&suggested_rating=" + lp_SuggestedRating  + "&no_cache=" + Math.random()*1234567;
    lv_ConfirmationMessage = mf_GetResponse(lv_URL);
    
    if(lp_Confirmation)
    {
        alert(lv_ConfirmationMessage);
    }
}

function mf_DeleteAllVotesRequest(lp_User, lp_PostID, lp_Confirmation)
{
    var lv_ConfirmationMessage;
    var lv_URL;
    
    
    if(!confirm("You are asking to DELETE ALL THE VOTES of this post. Is this really what you want to do?"))
    {
        return;
    }
    
    lv_URL = "DeleteVotesRequest.aspx?user=" + lp_User + "&post_id=" + lp_PostID + "&no_cache=" + Math.random()*1234567;
    lv_ConfirmationMessage = mf_GetResponse(lv_URL);
    
    if(lp_Confirmation)
    {
        alert(lv_ConfirmationMessage);
    }
}

function mf_Feedback(lp_User, lp_VoteID, lp_ThumbVerse)
{
    var lv_Div;
    var lv_Notes;
    var lv_Response;
    var lv_URL;
    
    
    lv_Div = document.getElementById(lp_VoteID);
    lv_Notes = document.getElementById("notes " + lp_VoteID);
    lv_URL = "UserFeedback.aspx?user=" + lp_User + "&vote_id=" + lp_VoteID + "&thumb=" + lp_ThumbVerse + "&notes=" + escape(lv_Notes.value) + "&no_cache=" + Math.random()*1234567;

    lv_Response = mf_GetResponse(lv_URL);
    
    if(lv_Response == "OK")
    {
        if(lp_ThumbVerse == "up")
        {
            lv_Div.className = "ThumbUpVote";
        }
        else
        {
            lv_Div.className = "ThumbDownVote";
        }
    }
    else
    {
        alert(lv_Response);
    }
} 

function DisableAllControls(n)
{
    if (n.nodeType == 1 /*Node.ELEMENT_NODE*/)  // Check if n is an Element
        DisableControl_SetTimeout(n.id,100);
    var children = n.childNodes;                // Now get all children of n
    for(var i=0; i < children.length; i++) {    // Loop through the children
        DisableAllControls(children[i]);      // Recurse on each one
    }
}

function DisableControl(controlId)
{
  document.getElementById(controlId).disabled =true;
}

function DisableControl_SetTimeout(controlId,interval)
{
  if (controlId.length > 0)
    setTimeout("DisableControl('" +controlId + "')",interval);
}
 
function UpdateImg(ctrl,imgsrc) {
		var img = document.getElementById(ctrl);
		img.src = imgsrc;
}

function IsDemoMode()
{
    demoMode = document.getElementById('lbDemoMode');
    return (demoMode!= null && demoMode.value == "True");
}

function ShowDemoMessage()
{
  alert("This function is not available in demo mode");   
}

function ShowMessage(n,alsoAreas)
{
    
    
     if (IsDemoMode() && !alsoAreas)
     {
       //è stato fatto click su VoteDrillDown o ETLDrillDown
       ShowDemoMessage();
       return false;    
     }
     else
     {
        //visualizza una gif animata
        //il setTimeout serve per attivare l'animazione
	    if ((waitDiv=document.getElementById('wait'))!=null) { 
	        if (waitDiv.style){
	            document.getElementById('wait').style.visibility = 'visible';
                //setTimeout("UpdateImg('image1','img/bigrotation2.gif');",50);
	        }
	    }	
	    disableLinks();
	    if (alsoAreas)
	        disableAreas();
        DisableAllControls(n);
    }
}


function disableAreas()
{
    var myAreas = document.getElementsByTagName("area"); 
    for(var i=0;i < myAreas.length;i++) 
    {
    
    if(window.addEventListener) // Mozilla, Netscape, Firefox
    { 
	    myAreas[i].addEventListener('click', bloccaClic, false);
	} 
	else // IE
	{ 
	    myAreas[i].attachEvent('onclick', bloccaClic);
    }
    
       myAreas[i].removeAttribute('href');          
    }
}


function disableLinks()
{
    var myLinks = document.getElementsByTagName("a"); 
    for(var i=0;i < myLinks.length;i++) 
    {
    
    if(window.addEventListener) // Mozilla, Netscape, Firefox
    { 
	    myLinks[i].addEventListener('click', bloccaClic, false);
	} 
	else // IE
	{ 
	    myLinks[i].attachEvent('onclick', bloccaClic);
    }
    
      myLinks[i].removeAttribute('href');          
    }
}

function bloccaClic()
{
 return false;
}

function highlight(idPost,progressiveNumber,userName,userInsider,brand,line,productService,featureOrPart,quality,rating)
{                                    
    var tdPost = $get(idPost);
    var chkBoxs = tdPost.getElementsByTagName('input');
    var voteIDs = new Array();                        
    var j=0;
    for(var i=0, leni=chkBoxs.length; i < leni; i++) {                
        if (chkBoxs[i].type == "checkbox" && chkBoxs[i].checked)                
            voteIDs[j++]=chkBoxs[i].value;
    }
    
    GUI.HighlightTextService.HighlightText(userName,userInsider,idPost,progressiveNumber,brand,line,productService,featureOrPart,quality,rating,voteIDs,true,onSuccess,null,idPost);
}

function highlightFromZoom(idPost,userName,userInsider)
{                                    
    var sVotes = $get("s_Votes");
    var inputs = sVotes.getElementsByTagName("input");
    var voteIDs = new Array();                        
    var j=0;
    for(var i=0, leni=inputs.length; i < leni; i++) {                
        if (inputs[i].type == "checkbox" && inputs[i].name.endsWith(":highlight") && inputs[i].checked)                
            voteIDs[j++]=inputs[i].value;
    }
    GUI.HighlightTextService.HighlightText(userName,userInsider,idPost,-1,"","","","","","",voteIDs,false,onSuccess,onFailure,"s_PostText");
}
                
function onSuccess(result, context, methodName)
{
  var tdPost = $get(context);
  tdPost.innerHTML = result;
}

function onFailure(error, context, methodName)
{
 alert(error);
}