/**
 * Javascript helps the page look better
 * However, the side should be fully functional without JavaScript
 */

 language='en';

function showElement(element) {
   if (element!=null)  {
       if (typeof( element.style) != 'undefined')
            element.style.display='';
        else
            element.visibility = '';
   }
}

function hideElement(element) {
   if (element!=null)  {
        if (typeof( element.style) != 'undefined')
            element.style.display='none';
        else
            element.visibility = 'hide';
   }
}

function onLoginClicked() {
	e=document.getElementById('languageBox');
    e.style.display='none';
	e=document.getElementById('loginBox');
	if(e.style.display!='inline') {
		e.style.display='inline';
 	    checkLogin(document.getElementById('validationPassword'))
	} else {
		e.style.display='none';
	}
	return false;
}

function setLanguage() {
	e=document.getElementById('loginBox');
	e.style.display='none';
	e=document.getElementById('languageBox');
	if(e.style.display!='inline') {
		e.style.display='inline';
	} else {
		e.style.display='none';
	}
	return false;
}

function checkLogin(element) {
	button=document.getElementById('loginButton');
	button.disabled=(!element.value);
	button2=document.getElementById('newUserButton');
	button2.disabled=(element.value);
}

function hideTopControls() {
	e=document.getElementById('loginBox');
	e.style.display='none';
	e=document.getElementById('languageBox');
	e.style.display='none';
}

function selectionCanged(radioObj) {
     negativeSecion=document.getElementById("negative-review");
   if ( negativeSecion!=null)
      if(getCheckedValue(radioObj)<0)
        showElement(negativeSecion);
       else
        hideElement(negativeSecion);
}

function getCheckedValue(radioObj) {
    if(!radioObj)
    return "";
    var radioLength = radioObj.length;
    if(radioLength == undefined)
    if(radioObj.checked)
    return radioObj.value;
    else
    return "";
    for(var i = 0; i < radioLength; i++) {
      if(radioObj[i].checked) {
        return radioObj[i].value;
      }
    }
    return "";
  }


    function setCheckedValue(radioObj, value) {
      if(!radioObj)
        return "";
     var radioLength = radioObj.length;
        if(radioLength != undefined)
        for(var i = 0; i < radioLength; i++) {
          if(radioObj[i].value==value)
            radioObj[i].checked="true";
          else
           radioObj[i].checked="false";
        }
    }

    function password_onclick() {
      document.getElementById("isExistingUser").checked="true";
    }


  function anonymous_onclick() {
    document.getElementById("validationPassword").disabled=true;
    showElement(document.getElementById("validationBlock"));
    document.getElementById("validationUserName").value='';
    document.getElementById("validationPassword").value='';
    hideElement(document.getElementById("passwordBlock"));
  }

  function newUserRadio_onclick() {
   if (document.getElementById("validationPassword")!=null) {
     //document.getElementById("validationPassword").disabled=true;
     document.getElementById("validationPassword").value='';
   }
    if (document.getElementById("validationBlock")!=null) {
       hideElement(document.getElementById("validationBlock"));
    }
  }

  function existingUserRadio_onclick() {
    if (document.getElementById("validationPassword")!=null) {
          document.getElementById("validationPassword").disabled=false;
    }
  }

  function userName_onclick() {
    if (document.getElementById("validationPassword")!=null)
       document.getElementById("validationPassword").disabled=false;
    if (document.getElementById("isAnonymousUser")!=null)
       document.getElementById("isAnonymousUser").checked="false";
    if (document.getElementById("isNewUser")!=null)
      document.getElementById("isNewUser").checked="true";
    hideElement(document.getElementById("validationBlock"));
    showElement(document.getElementById("passwordBlock"));
  }
/*
  function Submit1_onclick() {

    //validate the form
    if (getCheckedValue(document.getElementById("isNewUser"))=="true")
     return true;

    // existing user should have both username & password
    if (
      document.getElementById("validationUserName").value==null ||
      document.getElementById("validationUserName").value==""
    ) {
      alert( message[language] );
      alert('<bean:message key="validate.user.name.needed"/>');
      return false;
    }

    if (
      document.getElementById("validationPassword").value==null ||
      document.getElementById("validationPassword").value==""
    ) {
      alert( message[language] );
      alert('<bean:message key="validate.user.password.needed"/>');
      return false;
    }
    return true;
  }
*/

var timeZoneName="";
var isMobile=false;

function onLoad(new_language, isMobileVersion) {
     isMobile=isMobileVersion;
     changecss('.otherCountry','display','none');
     changecss('.showAllButton','display','inline');
     changecss('#showMapButton','display','inline');
     timeZoneName=getTimezoneName();

     resizeImages();

      if (document.getElementById("passwordBlock")!=null)
           hideElement(document.getElementById("passwordBlock"));

      if ( typeof(new_language)!='undefined' && language!=null && language.length>0) {
            language=new_language;
         }
     headerSearchFormInput=document.getElementById("searchActionFormInput");
      if (
          headerSearchFormInput!=null &&
          ( headerSearchFormInput.value==null || headerSearchFormInput.value.length==0)
        ) {
          searchBoxImage="url(img/prompt_"+language+".png)";
          headerSearchFormInput.style.backgroundImage=searchBoxImage;
      }

      negativeSecion=document.getElementById("negative-review");
      if ( negativeSecion!=null) {
        var x=document.getElementsByName("rating");
         if(getCheckedValue(x[0])>=0)
          hideElement(negativeSecion);
      }
      userNameForm=document.getElementById("validationUserName");
      if (
          userNameForm!=null &&
          ( userNameForm.value!=null && userNameForm.value.length>0)
        ) {
        userName_onclick();
        setCheckedValue(document.getElementsByName("isNewUser"),"false");
      }
      userPasswordForm=document.getElementById("validationPassword");
       if (
          userPasswordForm!=null &&
          ( userPasswordForm.value!=null && userPasswordForm.value.length>0)
        ) {

          password_onclick();
        }
}


function searchBoxChanged() {
    headerSearchFormInput=document.getElementById("searchActionFormInput");
    headerSearchFormInput.style.backgroundColor="white";
    headerSearchFormInput.style.backgroundImage="none";
}


function resizeImages() {
     var fp_image=document.getElementById("fp_image");
     var windowWidth=getWindowWidth();
      var precision=1000.;
      if ( fp_image !=null ) {

        var aspect= (precision * fp_image.height)/fp_image.width;
        if (isMobile=='true') {
           fp_image.width=getWindowWidth()*0.9;
        } else {
          fp_image.width = 0.76*windowWidth/2.;
          if (windowWidth>600)
          fp_image.width +=0.2*(windowWidth-600)
          }
       fp_image.height = (aspect * fp_image.width) / precision;
      }

       var loc_image=document.getElementById("main-image");
       if (loc_image!=null) {
          var aspect= (precision * loc_image.height)/loc_image.width;
          if (isMobile=='true') {
             loc_image.width=320;
          } else {
             loc_image.width = 0.24*windowWidth;
             var windowWidth=getWindowWidth();
             if (windowWidth>800) {
               loc_image.width +=0.1*(windowWidth-800)
             }
            }

            loc_image.height = (aspect * loc_image.width) / precision;
       }

      // resize text form
      var searchActionFormInput=document.getElementById("searchActionFormInput");
      if (searchActionFormInput!=null)
          searchActionFormInput.size=getWindowWidth()/14.;

  }

// -----------------------------------------------------
// Get window width in pixels; returns 0 if unidentifiable.
// -----------------------------------------------------
function getWindowWidth(){
  var ww = 0;
  d = document;
   if ( typeof window.innerWidth != 'undefined' )
     ww = window.innerWidth;  // NN and Opera version
   else
   {
     if ( d.documentElement
       && typeof d.documentElement.clientWidth!='undefined'
       && d.documentElement.clientWidth != 0 )
       ww = d.documentElement.clientWidth;
     else
       if ( d.body
         && typeof d.body.clientWidth != 'undefined' )
         ww = d.body.clientWidth;
   }
   return ww;
}

// -----------------------------------------------------
// Adjust image size according to window width
// -----------------------------------------------------
function resizeimg() {
  var ww = getWindowWidth();

  if (ww>0) {
    if (ww<550) changeimg(small)
    else if (ww>900) changeimg(large)
    else changeimg (medium);
  }


function findPosition(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent)
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);

    return [curleft,curtop];
}


function addRectangle(element) {
  newdiv=document.createElement("div");
  newdiv.style.position="absolute";
  [curleft,curtop]=findPosition(element);
  newdiv.style.left = curleft+1+"px";
  newdiv.style.top  = curtop+1+"px";
  reviewTable=document.getElementById(element.id);
  row=reviewTable.getElementsByTagName("tr")[1];

  newdiv.style.width =  reviewTable.offsetWidth+"px";
  newdiv.style.height = reviewTable.offsetHeight*0.5+"px";
  newdiv.style.opacity= 0.4;
  //alert(row.bgColor);
  newdiv.style.backgroundColor = row.bgColor;
  newdiv.style.visibility = 'visible';
  newdiv.style.zindex=-1;

  document.body.appendChild(newdiv);
  row.bgColor =  reviewTable.bgColor
}


function selectAllCategories() {
  if(document.forms[1].selectedCategories[0].checked==true) {
    document.forms[1].selectedCategories[1].checked=false;
    for(i=2;i<document.forms[1].selectedCategories.length;i++){
      document.forms[1].selectedCategories[i].checked=true;
    }
  }
}

function clearAllCategories() {
  if( document.forms[1].selectedCategories[1].checked==true) {
    document.forms[1].selectedCategories[0].checked=false;
    for(i=2;i<document.forms[1].selectedCategories.length;i++){
      document.forms[1].selectedCategories[i].checked=false;
    }
  }
}

function selectCategory() {
  document.forms[1].selectedCategories[0].checked=false;
  document.forms[1].selectedCategories[1].checked=false;
}

}

function showsize(){
  alert("Window width = " + getWindowWidth());
}

  function changecss(theClass,element,value) {
          //courtesy Shawn Olson & http://www.shawnolson.net
           var cssRules;

           var added = false;
           for (var S = 0; S < document.styleSheets.length; S++){

      if (document.styleSheets[S]['rules']) {
            cssRules = 'rules';
           } else if (document.styleSheets[S]['cssRules']) {
            cssRules = 'cssRules';
           } else {
            //no rules found... browser unknown
           }

            for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
             if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
              if(document.styleSheets[S][cssRules][R].style[element]){
              document.styleSheets[S][cssRules][R].style[element] = value;
              added=true;
                  break;
              }
             }
            }
            if(!added){
            if(document.styleSheets[S].insertRule){
                            document.styleSheets[S].insertRule(theClass+' { '+element+': '+value+'; }',document.styleSheets[S][cssRules].length);
                          } else if (document.styleSheets[S].addRule) {
                                  document.styleSheets[S].addRule(theClass,element+': '+value+';');
                          }
            }
           }
          }

function showAllCountries() {
  changecss('.otherCountry','display','inline');
  changecss('.showAllButton','display','none');
}

function getTimezoneName() {
	tmSummer = new Date(Date.UTC(2005, 6, 30, 0, 0, 0, 0));
	so = -1 * tmSummer.getTimezoneOffset();
	tmWinter = new Date(Date.UTC(2005, 12, 30, 0, 0, 0, 0));
	wo = -1 * tmWinter.getTimezoneOffset();

	if (-660 == so && -660 == wo) return 'Pacific/Midway';
	if (-600 == so && -600 == wo) return 'Pacific/Tahiti';
	if (-570 == so && -570 == wo) return 'Pacific/Marquesas';
	if (-540 == so && -600 == wo) return 'America/Adak';
	if (-540 == so && -540 == wo) return 'Pacific/Gambier';
	if (-480 == so && -540 == wo) return 'US/Alaska';
	if (-480 == so && -480 == wo) return 'Pacific/Pitcairn';
	if (-420 == so && -480 == wo) return 'US/Pacific';
	if (-420 == so && -420 == wo) return 'US/Arizona';
	if (-360 == so && -420 == wo) return 'US/Mountain';
	if (-360 == so && -360 == wo) return 'America/Guatemala';
	if (-360 == so && -300 == wo) return 'Pacific/Easter';
	if (-300 == so && -360 == wo) return 'US/Central';
	if (-300 == so && -300 == wo) return 'America/Bogota';
	if (-240 == so && -300 == wo) return 'US/Eastern';
	if (-240 == so && -240 == wo) return 'America/Caracas';
	if (-240 == so && -180 == wo) return 'America/Santiago';
	if (-180 == so && -240 == wo) return 'Canada/Atlantic';
	if (-180 == so && -180 == wo) return 'America/Montevideo';
	if (-180 == so && -120 == wo) return 'America/Sao_Paulo';
	if (-150 == so && -210 == wo) return 'America/St_Johns';
	if (-120 == so && -180 == wo) return 'America/Godthab';
	if (-120 == so && -120 == wo) return 'America/Noronha';
	if (-60 == so && -60 == wo) return 'Atlantic/Cape_Verde';
	if (0 == so && -60 == wo) return 'Atlantic/Azores';
	if (0 == so && 0 == wo) return 'Africa/Casablanca';
	if (60 == so && 0 == wo) return 'Europe/London';
	if (60 == so && 60 == wo) return 'Africa/Algiers';
	if (60 == so && 120 == wo) return 'Africa/Windhoek';
	if (120 == so && 60 == wo) return 'Europe/Amsterdam';
	if (120 == so && 120 == wo) return 'Africa/Harare';
	if (180 == so && 120 == wo) return 'Europe/Athens';
	if (180 == so && 180 == wo) return 'Africa/Nairobi';
	if (240 == so && 180 == wo) return 'Europe/Moscow';
	if (240 == so && 240 == wo) return 'Asia/Dubai';
	if (270 == so && 210 == wo) return 'Asia/Tehran';
	if (270 == so && 270 == wo) return 'Asia/Kabul';
	if (300 == so && 240 == wo) return 'Asia/Baku';
	if (300 == so && 300 == wo) return 'Asia/Karachi';
	if (330 == so && 330 == wo) return 'Asia/Calcutta';
	if (345 == so && 345 == wo) return 'Asia/Katmandu';
	if (360 == so && 300 == wo) return 'Asia/Yekaterinburg';
	if (360 == so && 360 == wo) return 'Asia/Colombo';
	if (390 == so && 390 == wo) return 'Asia/Rangoon';
	if (420 == so && 360 == wo) return 'Asia/Almaty';
	if (420 == so && 420 == wo) return 'Asia/Bangkok';
	if (480 == so && 420 == wo) return 'Asia/Krasnoyarsk';
	if (480 == so && 480 == wo) return 'Australia/Perth';
	if (540 == so && 480 == wo) return 'Asia/Irkutsk';
	if (540 == so && 540 == wo) return 'Asia/Tokyo';
	if (570 == so && 570 == wo) return 'Australia/Darwin';
	if (570 == so && 630 == wo) return 'Australia/Adelaide';
	if (600 == so && 540 == wo) return 'Asia/Yakutsk';
	if (600 == so && 600 == wo) return 'Australia/Brisbane';
	if (600 == so && 660 == wo) return 'Australia/Sydney';
	if (630 == so && 660 == wo) return 'Australia/Lord_Howe';
	if (660 == so && 600 == wo) return 'Asia/Vladivostok';
	if (660 == so && 660 == wo) return 'Pacific/Guadalcanal';
	if (690 == so && 690 == wo) return 'Pacific/Norfolk';
	if (720 == so && 660 == wo) return 'Asia/Magadan';
	if (720 == so && 720 == wo) return 'Pacific/Fiji';
	if (720 == so && 780 == wo) return 'Pacific/Auckland';
	if (765 == so && 825 == wo) return 'Pacific/Chatham';
	if (780 == so && 780 == wo) return 'Pacific/Enderbury'
	if (840 == so && 840 == wo) return 'Pacific/Kiritimati';
	return 'US/Pacific';
}

var req;

// Get an XMLHttpRequest object in a portable way.
function newRequest()
{
  req = false;
  // For Safari, Firefox, and other non-MS browsers
  if (window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch (e) {
      req = false;
    }
  } else if (window.ActiveXObject) {
    // For Internet Explorer on Windows
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        req = false;
      }
    }
  }
}


function evaluateReview(element, reviewId, grade) {
  newRequest();
  req.open("POST", "endorse.do", false);
  req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  var encoded = "r=" + reviewId+"&v="+grade+"&a=4"
  var result=req.send(encoded);

   //make it visible
   var report=document.getElementById("ru_"+reviewId);
   if (report!=null)
     report.style.display = '';


  var parentNode= element.parentNode;
  var text = parentNode.firstChild;
  var links=parentNode.getElementsByTagName("a");

  var usefulSpan=document.getElementById("u_"+reviewId);
  var isNew=links[1].className=='review-evaluation' && links[0].className=='review-evaluation';

  if (isNew) {
      //increment total
      var totalSpan=document.getElementById("t_"+reviewId);
      if (totalSpan!=null)
         totalSpan.innerHTML=parseInt(totalSpan.innerHTML)+1;
   }


  if (links[0]==element) {
    // first element clicked
    if (links[0].className!='review-evaluation-selected') {
      // increment useful count
      if (usefulSpan!=null)
         usefulSpan.innerHTML=parseInt(usefulSpan.innerHTML)+1;
      links[0].className='review-evaluation-selected';
      links[1].className='review-evaluation';
    }
  } else {
    // second element checked
    if ( links[1].className!='review-evaluation-selected') {
      links[1].className='review-evaluation-selected';
      links[0].className='review-evaluation';
      // decrement useful count
      if ( !isNew && usefulSpan!=null)
         usefulSpan.innerHTML=parseInt(usefulSpan.innerHTML)-1;
    }
  }

/*  while(parentNode.hasChildNodes())
     parentNode.removeChild(parentNode.firstChild);
   parentNode.appendChild(text);
   parentNode.innerHTML = newText;
*/
  //hideElement(element);
  return false;
}

