/* Common JavaScript for the Plaxo 3.0 corp site */

// returns href with name=? removed (if it was there)
function removeQueryParam(href,name)
{
  var last=0;
  while(true)
  {
    var start=href.indexOf(name,last);
    if(start==-1) return href; // bail out
    if((start==0 || href.charAt(start-1)=='&' || href.charAt(start-1)=='?') && start+name.length<href.length)
    {
      var newHref=href.substring(0,start);
      var end=href.indexOf('&',start+name.length);
      if(end!=-1) newHref+=href.substring(end+1); // copy to the end
      else if(start>0) newHref=newHref.substring(0,newHref.length-1); // cut off the & or ? before this param, since there's nothing after it
      return newHref;
    }
    last = start+1;
  }
}

// appends query to href, inserting ? or & as necessary (e.g. to append "t=2" to location.href)
function appendQuery(href,query)
{
  if(href.indexOf('?')==-1)
    return href+'?'+query;
  return href+'&'+query;
}

// replaces the query string name in href with value (adds it if it wasn't there before)
// e.g. replaceQueryParam(xxx?a=b&c=d,a,e) -> xxx?a=e&c=d
function replaceQueryParam(href,name,value)
{
  return appendQuery(removeQueryParam(href,name),name+'='+value);
}

var g_chooserShowing = false;

function toggleLanguageChooser() {
  var chooser = document.getElementById('language_chooser');
  if (chooser) {
    chooser.style.display = g_chooserShowing ? 'none' : '';
    g_chooserShowing = !g_chooserShowing;
  }
}

function hideLanguageChooser(evt) {
  if (!g_chooserShowing) return;

  // don't dismiss if we're inside a descendent of the language picker
  if (!evt) evt = window.event || {};
	var t = (evt.srcElement ? evt.srcElement : (evt.target ? evt.target : null));
  while (t) {
    if (t.className == 'language') {
      return;
    }
    t = t.parentNode;
  }

  toggleLanguageChooser();
}

// outside clicks dismiss chooser 
if (!document.onclick) document.onclick = hideLanguageChooser;

function setLocale(locale) {
  var url = replaceQueryParam(location.href, 'lang', locale);
  if (typeof g_plx_template != 'undefined' && g_plx_template) url = replaceQueryParam(url, 't', g_plx_template); // make sure we have the current template (even this page was a POST result)
  location.href = url;
}

function ShowVideo() {
// link to new video on YouTube
window.open('http://www.youtube.com/watch?v=n-yXudmFowE', 'video');
return;

    // videoDiv is where the video goes
    var videoDiv = document.getElementById("video_div");
    if(videoDiv.style.display == "block")
    {
        document.getElementById("video_shim").style.display = "none";
        videoDiv.style.display = "none";
        videoDiv.innerHTML = "";
    }
    else
    {
        // video_shim is an IFRAME used to keep SELECT boxes from poking through the video
        document.getElementById("video_shim").style.display = "block";
        videoDiv.style.display = "block";
        videoDiv.innerHTML = '<img onclick="ShowVideo()" style="cursor:pointer;cursor:hand;width:13px;height:13px;background:#4c8cd2;margin-top:5px;margin-right:5px" src="/webapps/common/img/close.png"><div id="swf_div">&nbsp;</div>';
        var fo = new SWFObject("/css/m/swf/30demo_controller.swf", "/css/m/swf/30demo_controller.swf", "551", "468", "7", "#FFFFFF", false, "best" );
        fo.addVariable("csConfigFile", "/css/m/swf/30demo_config.xml");
        fo.addVariable("csColor", "FFFFFF");
        fo.write("swf_div");
    }
}

function showOpenID(state) {
  var on = state ? '' : 'none';
  var off = state ? 'none' : '';
  document.getElementById('signin_email_label').style.display = off;
  document.getElementById('signin_openid_label').style.display = on;
  document.getElementById('signin_email_field').style.display = off;
  document.getElementById('signin_openid_field').style.display = on;
  document.getElementById('signin_password_label').style.display = off;
  document.getElementById('signin_password_field').style.display = off;
  if (state) document.getElementById('signin_password').value = ''; // don't accidentally submit normal login

  document.form.action = state ? '/openid' : 'https://' + location.host + '/signin';
  g_noCheckForm = state;
}
