var selectedDestCode;
var selectedResortCode;
var selectedAccomCode;
var sourceFlag;

function showFullReview(reviewnum)
{
   document.getElementById("reviewsmall"+reviewnum).style.display = 'none';
   document.getElementById("reviewbig"+reviewnum).style.display = 'block';
   document.getElementById("imagesmall"+reviewnum).style.display = 'none';
   document.getElementById("imagebig"+reviewnum).style.display = 'block';
}

function showShortReview(reviewnum)
{
   document.getElementById("reviewsmall"+reviewnum).style.display = 'block';
   document.getElementById("reviewbig"+reviewnum).style.display = 'none';
   document.getElementById("imagesmall"+reviewnum).style.display = 'block';
   document.getElementById("imagebig"+reviewnum).style.display = 'none';
}

function selUpdate(ElementID,ElementEvent)
{
   ElementEvent = ElementID + '_' + ElementEvent
   switch(ElementEvent)
   {
      case 'seasonCode_change':
      if (getElem('seasonCode').options[0].selected)
      {
         getElem('destinationCode').options[0].selected = getElem('destinationCode').disabled = true;
         getElem('resortCode').options[0].selected = getElem('resortCode').disabled = true;
         if(getElem('accommodationCode'))
         getElem('accommodationCode').options[0].selected = getElem('accommodationCode').disabled = true;
      }
      else
      {
         getElem('destinationCode').options[0].selected = getElem('destinationCode').disabled = true;
         getElem('resortCode').options[0].selected = getElem('resortCode').disabled = true;
         if(getElem('accommodationCode'))
         {
            getElem('accommodationCode').options[0].selected = getElem('accommodationCode').disabled = true;
         }
         saveselection();// WHY DO WE NEED IT?
         Params = "seasonCode="+getElem("seasonCode").value;
         LoadDD(Params,'','','destinationCode');
      }
      break;

      case 'destinationCode_change':
      if (getElem('seasonCode').options[0].selected)
      {//Check if Default Season is not selected
         getElem('destinationCode').options[0].selected = getElem('destinationCode').disabled = true;
         return SetFocus('Please select a Season before proceeding',getElem('seasonCode'))
      }
      if (getElem('destinationCode').options[0].selected)
      {
         getElem('resortCode').options[0].selected = getElem('resortCode').disabled = true;
         if(getElem('accommodationCode'))
         {
            getElem('accommodationCode').options[0].selected = getElem('accommodationCode').disabled = true;
         }
      }
      else
      {
         getElem('resortCode').options[0].selected = getElem('resortCode').disabled = true;
         if(getElem('accommodationCode'))
         {
            getElem('accommodationCode').options[0].selected = getElem('accommodationCode').disabled = true;
         }
         saveselection();
         Params = "seasonCode="+getElem("seasonCode").value+"&destinationCode="+getElem("destinationCode").value;
         if(getElem("destinationCode").value=='' || getElem("destinationCode").value=="" )
         {
            getElem('resortCode').disabled = true;
            if(getElem('accommodationCode'))
            {
               getElem('accommodationCode').disabled = true;
            }
         }
         LoadDD(Params,'','','','resortCode');
      }
      break;

      case 'resortCode_change':
      if (getElem('destinationCode').options[0].selected || getElem('seasonCode').options[0].selected)
      {
         getElem('resortCode').options[0].selected = getElem('resortCode').disabled = true;
         if(getElem('destinationCode').disabled = true)
         {
            return SetFocus('Please select a Season before proceeding',getElem('seasonCode'))
         }
         else
         {
            return SetFocus('Please select a Destination before proceeding',getElem('destinationCode'))
         }
      }
      if (getElem('resortCode').options[0].selected)
      {
         if(getElem('accommodationCode'))
         {
            getElem('accommodationCode').options[0].selected = getElem('accommodationCode').disabled = true;
         }
      }
      else
      {
         if(getElem('accommodationCode'))
         {
            getElem('accommodationCode').options[0].selected = getElem('accommodationCode').disabled = true;
            saveselection();
            Params = "seasonCode="+getElem("seasonCode").value+"&destinationCode="+getElem("destinationCode").value+"&resortCode="+getElem("resortCode").value;
            if(getElem("resortCode").value=='' || getElem("resortCode").value=="" )
            {
               if(getElem('accommodationCode'))
               {
                  getElem('accommodationCode').disabled = true;
               }
            }
            LoadDD(Params,'','','','','accommodationCode');
         }
      }
      break;

      case 'accommodationCode_change':
      if (getElem('resortCode').options[0].selected || getElem('destinationCode').options[0].selected || getElem('seasonCode').options[0].selected) 
      {
         getElem('accommodationCode').options[0].selected = getElem('accommodationCode').disabled = true;
         if(getElem('resortCode').disabled = true)
         {
            if(getElem('destinationCode').disabled = true)
            {
               return SetFocus('Please select a Season before proceeding',getElem('seasonCode'))
            }
            else
            {
               return SetFocus('Please select a Destination before proceeding',getElem('destinationCode'))
            }
         }
         else 
         {
            return SetFocus('Please select a Resort before proceeding',getElem('resortCode'))
         }
      }
   }//switch end
}//selUpdate end

function saveselection()
{
   selectedSeasonCode = getElem('seasonCode').value;
   selectedDestinationCode = getElem('destinationCode').value;
   selectedResortCode = getElem('resortCode').value;
   if(getElem('accommodationCode')){ selectedAccommodationCode = getElem('accommodationCode').value;}
}
// Deprecated method 
function ShowSelection(elementid)
{
   switch(elementid) {
      case 'seasonCode':
      objSel = getElem('seasonCode')
      for (z=0;z<objSel.length;z++)
      {
         if (objSel.options[z].value == seltest1)
         {
            objSel.selectedIndex = z;
         }
      }
      break;

      case 'destinationCode':
      objSel = getElem('destinationCode')
      if (reviewiFrame.document.getElementById("iframeSel2"))
      {
         objSel.selectedIndex = reviewiFrame.document.getElementById("iframeSel2").selectedIndex;
      }
      if(sourceFlag=='true' && objSel.selectedIndex == 0)
      {
         alert("The required destination is not present for the selected season.");
         sourceFlag = 'false';
      }
      break;

      case 'resortCode':
      objSel = getElem('resortCode')
      if (reviewiFrame.document.getElementById("iframeSel3"))
      {
         objSel.selectedIndex = reviewiFrame.document.getElementById("iframeSel3").selectedIndex;
      }
     if(sourceFlag=='true' && objSel.selectedIndex == 0)
      {
         alert("The required resort is not present for the selected season.");
         sourceFlag = 'false';
      }
      break;

      case 'accommodationCode':
      objSel = getElem('accommodationCode')
      if (reviewiFrame.document.getElementById("iframeSel4"))
      {
            objSel.selectedIndex = reviewiFrame.document.getElementById("iframeSel4").selectedIndex;
      }
     if(objSel.selectedIndex == 0)
      {
         alert("The required accommodation is not present for the selected season.");
         sourceFlag = 'false';
      }
      break;
   }// switch end
}// ShowSelection end

function LoadDD(params1,JSExecAfter)
{
   reviewFrame = 'reviewiFrame';
   DropDowns = ''
   for (var elemcount=1;elemcount < LoadDD.arguments.length - 1;elemcount++)
   {
      elementid = LoadDD.arguments[elemcount + 1]
      if (elementid != '')
      {
         DropDowns+= 'UpdateDropDown("'+elementid+'",' + elemcount + ',"'+ reviewFrame +'");'
         ClearDropDown(elementid,'Loading...');
      }
   }
   eval(reviewFrame+'LoadCompleteRun = '+String.fromCharCode(39) + DropDowns + JSExecAfter + String.fromCharCode(39))
   eval(reviewFrame+'.document.location.replace("' + updateReviewLocation + '?' + params1 + '")')
}

function ClearDropDown(elementid,FirstOptionText)
{
   eval('objelem = getElem("'+elementid+'")')
   // Remove all Options
   for (var q=objelem.options.length;q>=0;q--)
   {
      objelem.options[q]=null;
   }
   if (FirstOptionText)
   {
      objelem.disabled = true;
      FirstOption = FirstOptionText;
   }
   else
   {
      FirstOption = objelem.getAttribute("alt");
   }
 // Add option
   if (ie4 || ie5)
   {
      newOption = document.createElement("OPTION")
      newOption.text = FirstOption
      newOption.value = 'Any'
      objelem.options.add(newOption)
   }
   else
   {
      objelem.options[objelem.options.length] = new Option(FirstOption,'Any',false,false)
   }
}

function DynamicLoadCompletereview(obj)
{
   try
   {
      eval('strRun = ' + obj.name + 'LoadCompleteRun')
      eval(strRun)
   }
   catch (er) {}
}

function UpdateDropDown(elementid,bufferpos,BufferFrameID)
{
   eval('objelem = getElem("'+elementid+'")')
   eval('objsrc = '+ BufferFrameID +'.document.getElementById("iframeSel' + bufferpos + '")')
   CopyDropDown(objsrc,objelem)
   eval('getElem("'+elementid+'").disabled = false')
}

function CopyDropDown(objelemsource,objelemdest)
{
   // Remove all Options
   for (var q=objelemdest.options.length;q>=0;q--)
   {
      objelemdest.options[q]=null;
   }
   // Add options
   for (q=0;q<objelemsource.length;q++)
   {
      if (ie4 || ie5)
      {
         newOption = document.createElement("OPTION")
         newOption.text = objelemsource.options[q].text
         newOption.value = objelemsource.options[q].value
         objelemdest.options.add(newOption)
      }
      else
      {
         objelemdest.options[objelemdest.options.length] = new Option(objelemsource.options[q].text,objelemsource.options[q].value,false,false)
      }
   }
}

function isDefault(elementid)
{
   eval('elval = getElem("'+elementid+'").value.toUpperCase()')
   if ((elval == 'ANY') || (elval == '') )
   {
      return true;
   }
   else
   {
      return false;
   }
}

function ValidateReviewForm(formobj)
{
   // 1 >>This part checks the main selection dropdowns
   if(getElem('accommodationCode'))
   {
     if (getElem('accommodationCode').options[0].selected == true || getElem('accommodationCode').disabled == true || getElem('accommodationCode').options[getElem('accommodationCode').selectedIndex].value=='')
      {
         alert("Please select an Accommodation");
         if(!getElem('accommodationCode').disabled == true) getElem('accommodationCode').focus();
         return;
      }
   }
   else
   {
     if (getElem('resortCode').options[0].selected == true || getElem('resortCode').disabled == true || getElem('resortCode').options[getElem('resortCode').selectedIndex].value=='')
     {
      alert("Please select a Resort");
      if(!getElem('resortCode').disabled == true) getElem('resortCode').focus();
      return;
     }
   }
   // 2 >>This validates only the WRITE pages...not the SEARCH pages
   if(getElem('county'))
   {
      if(getElem('county').value=='')//checks the COUNTY dropdowns for Country selected
      {
         return SetFocus('You have selected a Country, please select a County',getElem('county'));
      }
   }
   // 3 >>This part checks the Travel Date dropdowns
   if(getElem('travelMonth'))//This validates only the WRITE pages...not the SEARCH pages
   {
      //Check for a Valid date selected
      selectedMonth  = getElem('travelMonth').value;
      selectedYear   = getElem('travelYear').value;
      var todate=new Date();
      if( (selectedYear >= todate.getFullYear()) && (selectedMonth > eval(todate.getMonth()+1)) )
      { 
         return SetFocus('The selected date of travel is invalid. Please amend', getElem('travelMonth'))
      }
   }
   ValidateForm(formobj); //Good ol' validation in formvalidation.js
}

function hidingTimer()
{
    if (getElem('writerev'))
    {
      getElem('submitLink').style.display = 'none';
      getElem('submitNoLink').style.display = 'inline';
    }
}

//--Start..County/Country dropdown changes
var altCounty  = 'your County|Y';//alt tag for the county
var altCountry = 'your Country|Y';//alt tag for the country
function showOtherCounty()
{
   if(getElem('county').value == 'nonuk')
   {
      getElem('othercountriesDiv').style.display = 'inline';
      getElem('otherCountries').setAttribute("alt",altCountry);
      getElem('county').setAttribute("alt",'');
   }
   else
   {//first 2 lines ensure the COUNTRY DD is reset and is able to Hide/show when COUNTY is again OTHERS.
      getElem('county').options[getElem('county').options.length-1].value = 'nonuk';
      getElem('otherCountries').options[0].selected = true;
      getElem('othercountriesDiv').style.display = 'none';
      getElem('otherCountries').setAttribute("alt",'');
      getElem('county').setAttribute("alt",altCounty);
   }
   //To check whether Country is selected instead of County in Firefox
   if(getElem('county').value=='' && (window.Event))
   {
      getElem('county').selectedIndex=0;
      return SetFocus('You have selected a Country, please select a County',getElem('county'));
   }
}

//To check whether Country is selected instead of County in IE
function checkCountyunderCountry()
{
   if(getElem('county').value=='' && (window.event))
   {
      getElem('county').selectedIndex=0;
      return SetFocus('You have selected a Country, please select a County',getElem('county'));
   }
}

function copyValueToCounty()
{//Copies the 'COUNTRY' value to others field of COUNTY before submission
   getElem('county').options[getElem('county').selectedIndex].value = getElem('otherCountries').options[getElem('otherCountries').selectedIndex].value
}

// deprecated method not in use any more
function checkDefaultSelections(seasonCode, destinationCode, resortCode, accommodationCode,pageEntryIndicator)
{
   sourceFlag=pageEntryIndicator;
   if(sourceFlag=='true')
   {
      var seasonOption;
      selectedDestCode=destinationCode;
      selectedresortCode=resortCode;
      selectedAccomCode=accommodationCode;
      if(seasonCode!='')
      {
         objSel = getElem('seasonCode')
         for (z=0;z<objSel.length;z++)
         {
            seasonOption = objSel.options[z].value;
            subString = seasonCode.substring(0,1);
            if (subString == seasonOption.substring(0,1))
               {
                  objSel.selectedIndex = z;
                  break;
               }
         }
         selUpdate('seasonCode','change',1);
      }
   }
   else
   {
      sourceFlag='false';
      selectedDestCode='';
      selectedresortCode='';
      selectedAccomCode='';
      getElem('seasonCode').options[0].selected = true;//force the SeasonCode to be Default
      if(getElem('county'))
      {
         getElem('county').options[0].selected = true;
      }//Firefox - force County to Default in write pages
      if(getElem('destinationCode').options[0].selected) {getElem('destinationCode').disabled = true;}
      if(getElem('resortCode').options[0].selected) {getElem('resortCode').disabled = true;}
      if(getElem('accommodationCode'))
      {
         if(getElem('accommodationCode').options[0].selected)
         {
            getElem('accommodationCode').disabled = true;
         }
      }
   }
}

//Functions to restrict the Write Resort/Acco to maxlength
function maxtext(x,y)
{
   tempstr = x.value
   if(tempstr.length>y)
   {
     x.value = tempstr.substring(0,y);
   }
}

//Function for the Review Privacy Policy Popup
function pop_up(actionname)
{
   var pageaction = po_url + '/po/' + actionname + '.do?'
   pageaction += 'popup=true'
   Popup(pageaction,780,500,'scrollbars=1,left=25,top=25')
}

function showData(DataName)
{
   switch(DataName)
   {
      case 'data_protection':
         getElem('data_protection').style.display = 'inline';
         window.location.href = "#data_protection";
      break;

      case 'privacy_policy':
         getElem('privacy_policy').style.display = 'inline';
         window.location.href = "#privacy_policy";
      break;

      case 'terms_conditions':
         getElem('terms_conditions').style.display = 'inline';
         window.location.href = "#terms_conditions";
      break;
   }
}