var loadingHTML     = "<center>Please wait.</center><br /><center>" +
                      "<img src='/images/loading.gif' width='220' height='19' alt='Loading...' /></center>";
var loadingBarTHTML    = "<img src='/images/loadingT.gif' width='220' height='19' alt='Loading...' />";
var loadingTHTML    = "<center>Please wait.</center><br /><center>" + loadingBarTHTML +
                      "</center>";
var loading3HTML    = "<center><img src='/images/loading3.gif' width='16' height='16' alt='Loading...' /></center>";
var searchingHTML   = "<center>Searching. Please wait.</center><br />" +
                      "<center><img src='/images/loading.gif' width='220' height='19' alt='Searching...' /></center>";
var modalPars       = {overlayCss:{backgroundColor:'#ffffff',cursor:'wait'},opacity:75,persist:true};

/*if (isSet(browserIE6))
{
  if(browserIE6==true)
    modalPars.position = ['10px','10px'];
}*/

var openMenuId = null;

function closeMenu ()
{
  if (openMenuId != null)
  {
    document.hideDIV(openMenuId);
		if(BrowserDetect.version < 7 && BrowserDetect.browser=="Explorer")
		{
			var oRef = document.getObjectRef(openMenuId);
			showSelects (true,oRef);
		}
    openMenuId = null;
  }
}

function openMenu (mainMenuId)
{
  var oTD = document.getObjectRef("menu-" + mainMenuId);

  newMenuId = "subMenu-" + mainMenuId;

  if (newMenuId == openMenuId)
    closeMenu();
  else
  {
    closeMenu();
    openMenuId = newMenuId;
    document.showDIVBelow(openMenuId,oTD);

		if(BrowserDetect.version < 7 && BrowserDetect.browser=="Explorer")
		{
			var oRef = document.getObjectRef(openMenuId);
			showSelects (false,oRef);
		}
  }
}

function openMenu2 (mainMenuId)
{
  var oTD = document.getObjectRef("menu-" + mainMenuId);

  newMenuId = "subMenu-" + mainMenuId;

  if (newMenuId == openMenuId)
    closeMenu();
  else
  {
    closeMenu();
    openMenuId = newMenuId;
    document.showDIVAboveCenter(openMenuId,oTD);
  }
}

function doSearch ()
{
  var oForm = document.getObjectRef("searchForm");

  oForm.submit();
}

function showSelects (flag,oExcept)
{
  var s     = document.body.getElementsByTagName("SELECT"),
      sLen  = s.length,
      es    = oExcept.getElementsByTagName("SELECT"),
      esLen = es.length;
  var si,
      esi;

  for (si = 0;si < sLen;si++)
  {
    so  = s[si];

    for (esi = 0;esi < esLen;esi++)
    {
      if (es[esi] == so)
        break;
    }

    if (esi == esLen) // not found in except object?
    {
      if (flag)
        so.style.visibility = "visible";
      else
        so.style.visibility = "hidden";
    }
  }
}

function isSet( variable )
{
	return( typeof( variable ) != 'undefined' );
}
function getUserPreference(key,defaultVal)
{
  if(!isSet(defaultVal))
    defaultVal = '';

  if(isSet(user_preferences) && isSet(user_preferences[key]))
  {
    return user_preferences[key];
  }
  else
  {
    setUserPreference(key,defaultVal);
    return defaultVal
  }
}
function setUserPreference(key,value)
{
  if(!isSet(user_preferences[key]) || user_preferences[key]!=value)
  {
    user_preferences[key] = value;
    $.post('set_user_preference.php',{preference_key:key,preference_value:value},preferenceSet,'json');
  }
}
function preferenceSet(data)
{
  switch(data.error)
	{
    case 'ok':
		  //alert('user preference successfully set');
		break;
		default:
			handleError(data);
		break;
	}
}
function handleError(data)
{
  if(data.error=='message')
    alert(data.message);
  else if(data.message==undefined)
    alert("ERROR: " + data.error);
  else
	  alert("ERROR: " + data.error + "\n" + data.message);
}


$(document).ready(
  function()
  {
    $('input#client_change_password_button').click(
      function()
      {
        changePassword();
      }
    )

    $('input#client_logout_button').click(
      function()
      {
        self.location.href = '/logout.php';
      }
    )

    $('#ipad_login_button').click(
      function ()
      {
        self.location.href = '/' + countryCode + '/' + languageCode + '/ipad-login';
      }
    );

/*    $('input#client_login_button').click(
      function()
      {
        $('form#login_client_form').submit();
//        alert('login');*/
        /*var pars = new Object();
        pars['user']      = $('input#client_login_username').val();
        pars['password']  = $('input#client_login_password').val();
        pars['returnJSON']= 1;

        $.post('/login.php',pars,
          function(data)
          {
            switch(data.error)
            {
              case 'OK':
//                alert('logged in successfully: ' + data.nextPage);
                location.href = data.nextPage;
                break;
              case 'OK_choice':
//                alert('choiceHTML: ' + data.choiceHTML);
                $('body').html(data.choiceHTML);
                break;
              default:
                handleError(data);
                break;
            }
          },'json'
        )*/
/*      }
    )*/

/*    $('input#search_button').click(
      function()
      {

      }
    )*/
  }
)
