//** rtg_Functionality - (c) Reason To Go
//** Oct 30th, 2008- Script created

var _ButtonID;
var _Location;
var _ServURL;
var _BizCode;
var _mvType;
var _sVoteCode;

function rtgOnLoad(){
    _ServURL = document.getElementById('hidServURL');
    _BizCode = document.getElementById('hidBizCode');
    _mvType = document.getElementById('hidmvType');

    //add code for unique view page 1 if static minivoter. If popup, it's set in dddropdownpanel.js
    if (document.getElementById('rtg_DivPage1') != null && _mvType.value == "static") 
    {
        setTimeout("rtgAddPageView(1);", 5000);  //5 seconds 
    }

}

function addLoadEvent(func) {   
   var oldonload = window.onload;   
   if (typeof window.onload != 'function') {   
     window.onload = func;   
   } else {   
     window.onload = function() {   
       if (oldonload) {   
         oldonload();   
       }   
       func();   
     }   
   }   
}   

addLoadEvent(rtgOnLoad); 

//Check to see if cookie exists that user already answered questions for this business
function rtgCookieDisplayPopup(){
    var displayPopup = true;
    
    var sCookie = rtgGetCookie(_BizCode.value + 'CLOSED');
    if (sCookie != null) {
        if (sCookie.length>0) displayPopup = false;
    }
    
    sCookie = rtgGetCookie(_BizCode.value + 'VOTED');
    if (sCookie != null) {
        if (sCookie.length>0) displayPopup = false;
    }
    
    return displayPopup;
}

    
function rtgAddPageView(pagenum){

    var parDivPV = document.getElementById('rtg_DivIframePV'); 
    var rtg_ifrmPV = parDivPV.getElementsByTagName('iframe').item(0); 
    var currentTime = new Date();
    var sCookie = rtgGetCookie('R2GUID');
    var urlPV = _ServURL.value + 'pageviews.aspx?page=' + pagenum + '&b=' + _BizCode.value + '&c=' + sCookie + '&t=' + currentTime.getTime();
    rtg_ifrmPV.src = urlPV;
}

function rtgGetRadioValue(){
	var value='';
    var parDiv = document.getElementById('rtg_DivPage1'); 
    var radLocBiz = parDiv.getElementsByTagName('input').item(0); 
    if (radLocBiz.checked) 
		value = radLocBiz.value; 
    var radLocWeb = parDiv.getElementsByTagName('input').item(1); 
    if (radLocWeb.checked) 
		value = radLocWeb.value; 
    return value; 
}

function rtgButtonPress(){
	_Location =rtgGetRadioValue();
 	document.getElementById('rtg_DivPage1').style.display='none';
    document.getElementById('rtg_DivPage2').style.display='block';
    
    //don't allow page 2 to autoclose ***********************
    var popupDiv = document.getElementById('mypanel');
    
    if (popupDiv != null){
        popupDiv.onmouseout = "";
        clearAutoClose();
    }
    
    var parDiv = document.getElementById('rtg_DivPage2');
    if (_Location == 'web'){ 
        document.getElementById('rtg_divWhyBiz').style.display='none';
        document.getElementById('rtg_divWhyWeb').style.display='block';
    	//parDiv.getElementsByTagName('div').item(2).style.display='none'; 
    	//parDiv.getElementsByTagName('div').item(3).style.display='block'; 
    } 
    else{
        document.getElementById('rtg_divWhyBiz').style.display='block';
        document.getElementById('rtg_divWhyWeb').style.display='none';
        //parDiv.getElementsByTagName('div').item(2).style.display='block'; 
    	//parDiv.getElementsByTagName('div').item(3).style.display='none'; 
    }
    
    //add code for unique view page 2
    rtgAddPageView(2);
}

function rtgGOPress(){
    rtgButtonPress();
    _ButtonID = 'GO';   
}

function rtgDGPress(){
    rtgButtonPress();
    _ButtonID = 'DG';  
}

function rtgSendVoteInfo(bizid, showcomment) { 

    //Create cookie the user addressed this page
    rtgSetCookie(_BizCode.value + "VOTED", "VOTED", 7, '/');

    //determine which dropdown has the reason
    var whyvalue;
    if (_Location == 'web') 
    	whyvalue = document.getElementById('rtg_selWhyWeb').value;
    else
        whyvalue = document.getElementById('rtg_selWhyBiz').value;
    if (whyvalue > 0)
    {
        var sUrlBeg = _ServURL.value;
        var sCookie = rtgGetCookie('R2GUID');
        if (sCookie == "") 
        {
            sCookie = rtgGenerateGuid();
            rtgSetCookie('R2GUID', sCookie, 365);
        }
        var sCookie2 = rtgGetCookie('R2GUserInfo');

        //var currentTime = new Date();
        sVoteCode = rtgGenerateGuid();
        var url = sUrlBeg + 'collectvote.aspx?bid=' + bizid.toString() + '&b=' + _ButtonID + '&l=' + _Location + '&w=' + whyvalue + '&c=' + sCookie + '&vc=' + sVoteCode + '&' + sCookie2;
        var parDiv = document.getElementById('rtg_DivIframe'); 
        var rtg_ifrm = parDiv.getElementsByTagName('iframe').item(0); 
        rtg_ifrm.src = url;
        if (showcomment)
            rtgToPage4(2);
        else{
            document.getElementById('rtg_DivPage2').style.display='none';
            document.getElementById('rtg_DivThanks').style.display='block'; 
        }
        
        //allow page 3 to autoclose *********************
        var popupDiv = document.getElementById('mypanel')
        if (popupDiv != null){
            popupDiv.onmouseout = "startAutoClose();";
            //start to close page 3 
            startAutoClose();
        }
           
        //add code for unique view page 3
        rtgAddPageView(3);
        
        //let session know that a vote occurred if popup minivoter
        if (_mvType.value == "popup"){
            sessvars.rtgvote = "1";  
            //clearInterval(_IntervalID);
        }    
        
    }
    else
    {
        rtgToggleReasonNeededPopup();
    }
}

function rtgSendVoteComment() { 
    var sUrlBeg = _ServURL.value;

    //var currentTime = new Date();
    var txtcomment = document.getElementById('rtg_txtComment');

    //See if it need to redirect to the Review page
    if (document.getElementById('rtg_locContactMe').checked == true)
    {
        var sBizID = document.getElementById('rtg_locBizID').value;
        rtgReviewContactMeWindow(sBizID, txtcomment.value);
    }
    else 
    {
       //only send the comment if there's text in it
       if (txtcomment.value.length > 0)
       {
           var surl = sUrlBeg + 'collectvote.aspx?vc=' + sVoteCode + '&vcom=' + txtcomment.value;
           var parDiv = document.getElementById('rtg_DivIframeComment'); 
           var rtg_ifrm = parDiv.getElementsByTagName('iframe').item(0); 
           rtg_ifrm.src = surl;
       }
    }
    document.getElementById('rtg_DivPage4').style.display='none';
    document.getElementById('rtg_DivThanks').style.display='block'; 
    document.getElementById('rtg_divAddComment').style.display='none';
    
    //add code for unique view page 4
    rtgAddPageView(4);
}

function rtgSetCookie(c_name, value, expiredays, cookiepath){
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) +  ((cookiepath==null) ? '' :'; path=' + cookiepath) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString());       
}

function rtgGetCookie(c_name){
    if (document.cookie.length>0)
    {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1)
        { 
            c_start = c_start + c_name.length + 1; 
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) 
                c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        } 
    }
    return "";
}

function rtgGenerateGuid(){
    var result, i, j;
    result = '';
    for(j=0; j<32; j++){
        if( j == 8 || j == 12|| j == 16|| j == 20)
        result = result + '-';
        i = Math.floor(Math.random()*16).toString(16).toUpperCase();
        result = result + i;
    }
    return result;
} 


function rtgClose(){
     //Create cookie the user addressed this page
    rtgSetCookie(_BizCode.value + 'CLOSED', 'CLOSED', 7, '/');

    document.getElementById('mypanel').style.display='none';

    //let session know that user closed minivoter if popup
    if (_mvType.value == "popup"){
        sessvars.rtgclose = "1"; 
    }
}


function rtgMainWindow() {
    var varUrlBeg = document.getElementById('hidServURL');
    var xWin = window.open(varUrlBeg.value, "mainpage");
}

function rtgAboutWindow() {
    var varAboutURL = document.getElementById('hidAboutURL');
    var xWin = window.open(varAboutURL.value , 'About');
}

function rtgHelpWindow() {
    var varUrlBeg = document.getElementById('hidServURL');
    var xWin = window.open(varUrlBeg.value + 'Help_MiniVoter.asp', 'helpwindow', 'width=650,height=705,scrollbars=yes,resizable=yes');
}

function rtgSignupWindow() {
    var varUrlBeg = document.getElementById('hidServURL');
    var xWin = window.open(varUrlBeg.value + 'BusinessSignUp.aspx');
}

function rtgPPWindow(page) {
    var varUrlBeg = document.getElementById('hidServURL');
    var xWin = window.open(varUrlBeg.value + 'privacypolicy.asp');
}

function rtgTOSWindow(page) {
    var varUrlBeg = document.getElementById('hidServURL');
    var xWin = window.open(varUrlBeg.value + 'TOS.asp');
}

//(jen)javascript does not support function overloading (instead, you can test for the parameter being undefined, meaning it wasn't sent)
//function rtgReviewWindow(bizid) {
//    rtgReviewWindow(bizid,'');
//}

function rtgReviewWindow(bizid,commentcookiename) {
    var varUrlBeg = document.getElementById('hidServURL');
    var sCookie = rtgGetCookie('R2GUID');
    var sPage = 'SubmitReview.aspx?bid=' + bizid +  '&mv=1&c=' + sCookie;
    //if (commentcookiename != '') {
    if(typeof(commentcookiename) != "undefined"){
      sPage = sPage + '&commentcookie=' + commentcookiename;
    }   
    var xWin = window.open(varUrlBeg.value + sPage , 'submitreview', 'width=650,height=775,scrollbars=yes,resizable=yes');
}

function rtgReviewContactMeWindow(bizid,comment,commentcookiename) {

    //Need to substitude lfcr with [BR], to pass thru a querystring
    var comment = replacecarriagereturn(comment,'[BR]');
    var bizweb = rtgGetRadioValue();

    var varUrlBeg = document.getElementById('hidServURL');
    var sCookie = rtgGetCookie('R2GUID');
    var sPage = 'SubmitReview.aspx?bid=' + bizid +  '&contactme=1&mv=1&vcom=' + comment + "&bizweb=" + bizweb + '&c=' + sCookie;
    //if (commentcookiename != '') {
    if(typeof(commentcookiename) != "undefined"){
      sPage = sPage + '&commentcookie=' + commentcookiename;
    }   
    var xWin = window.open(varUrlBeg.value + sPage , 'submitreview', 'width=650,height=775,scrollbars=yes,resizable=yes');
}

function replacecarriagereturn(textvalue,replaceWith)  
    {   
     textvalue = escape(textvalue);  
       //encode all characters in text area  
       //to find carriage return character  
     for(i=0; i < textvalue.length; i++)  
     {   
      //loop through string, replacing carriage return   
      //encoding with HTML break tag  
     if(textvalue.indexOf("%0D%0A") > -1)  
     {   
      //Windows encodes returns as \r\n hex  
      textvalue=textvalue.replace("%0D%0A",replaceWith);  
     }  
     else if(textvalue.indexOf("%0A") > -1)  
     {   
      //Unix encodes returns as \n hex  
      textvalue=textvalue.replace("%0A",replaceWith);  
     }  
     else if(textvalue.indexOf("%0D") > -1)  
     {   
      //Macintosh encodes returns as \r hex  
      textvalue=textvalue.replace("%0D",replaceWith);  
    }  
   }  
   textvalue=unescape(textvalue);  
   return textvalue;
  }  


function rtgSuggestWindow(type, bizid) {
    var varUrlBeg = document.getElementById('hidServURL');
    var sCookie = rtgGetCookie('R2GUID');
    var sPage;
    if (type == 1) 
       sPage= 'Suggest.aspx?type=place&mv=1';
    else if (type == 2)
       sPage= 'Suggest.aspx?type=reason&mv=1&bid=' + bizid;

    var xWin = window.open(varUrlBeg.value + sPage , 'suggestsite', 'width=655,height=590,scrollbars=yes,resizable=yes');
}

function rtgBackToPage1() {
    document.getElementById('rtg_DivPage1').style.display='block';
    document.getElementById('rtg_DivPage2').style.display='none';
}

function rtgBackToPageThanks() {
    document.getElementById('rtg_DivThanks').style.display='block';
    document.getElementById('rtg_DivPage4').style.display='none';
}

function rtgToggleReasonNeededPopup() {
    var popup = document.getElementById('rtg_DivReasonNeeded')
    if (popup.style.display == 'block')
        popup.style.display='none';
    else
        popup.style.display='block';
}

function rtgToPage4(fromPage) {
    if (fromPage == 2)
            document.getElementById('rtg_DivPage2').style.display='none';
    if (fromPage == 3)
            document.getElementById('rtg_DivThanks').style.display='none';
    document.getElementById('rtg_DivPage4').style.display='block';
    document.getElementById('rtg_divAddComment').style.display='none';
    document.getElementById('rtg_DivSubmitReview').style.display='block';

}

function rtgTextCounter(field, maxlimit) {
    // if too long...trim it!
    if (field.value.length > maxlimit) 
        field.value = field.value.substring(0, maxlimit);
}


function rtgTextCounterDisplay(field, maxlimit) {
    txtToUpdate =  document.getElementById("r2g_comments_topRight");
    //Call textCounter
    rtgTextCounter(field, maxlimit)
    var curLen = field.value.length;
    if (field.value.length == maxlimit){
       txtToUpdate.innerHTML = "(0 chars left)";
    }
    else{
        //Could validate if the backspace was pressed here!
        //if (!rtgIsArrowKey()){
        //  curLen = curLen +1;
       // }
        txtToUpdate.innerHTML = "(" + (maxlimit - curLen) + " chars left)";

        txtToUpdate.style.color = '';
        if ((maxlimit - curLen) < 20){
          txtToUpdate.style.color = 'yellow';
        }
        if ((maxlimit - curLen) < 10){
          txtToUpdate.style.color = 'red';
        }
    }
}

function rtgIsArrowKey(){
  var e=window.event;
  switch(e.keyCode)
  {
  case 37:
    // Key left.
    return true;
    break;
  case 38:
    // Key up.
    return true;
    break;
  case 39:
    // Key right.
    return true;
    break;
  case 40:
    // Key down.
    return true;
    break;
  }
 
  return false;
}

//(jen)cookies don't work cross domain - instead use sessvars
//function rtgCallReviewWithComment(bizid,txtFieldValue,divToHide){
//   var commentcookiename = 'REVIEWCOMMENTCOOKIE';
//   divToHide.style.display='none';
//   rtgDeleteCookie(commentcookiename);
//   rtgSetCookie(commentcookiename,txtFieldValue,1);
//   rtgReviewWindow(bizid,commentcookiename);
//}

//function rtgDeleteCookie(c_name) {
//  var cookie_date = new Date ( );  // current date & time
//  cookie_date.setTime ( cookie_date.getTime() - 1 );
//  document.cookie = c_name += "=; expires=" + cookie_date.toGMTString();
//} 

//function rtgSwapDivs(divToHide, divToShow){
//      divToHide.style.display='none';
//      divToShow.style.display='block';
//}

//called from dddropdownpanel.js
function ShowPage1(){
    document.getElementById('rtg_DivPage1').style.display='block';
    document.getElementById('rtg_DivPage2').style.display='none';
    document.getElementById('rtg_DivThanks').style.display='none';
    document.getElementById('rtg_DivPage4').style.display='none';
}