// function allows (amonsgt other things) dynamic changing of style attributes in templates other than those where the call exists
// original code available here - - ->  http://simonwillison.net/2004/May/26/addLoadEvent/

function addLoadEvent(func) { 
  var oldonload = window.onload; 
  if (typeof window.onload != 'function') { 
    window.onload = func; 
  } else { 
    window.onload = function() { 
      oldonload(); 
      func(); 
    } 
  } 
} 

function setCookie( cname, value, exp_y, exp_m, exp_d, path, domain, secure )
{
  var cookie_string = cname + "=" + escape ( value );

  if ( exp_y )
  {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
}

function getCookie ( cookie_name )
{
  var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );

  if ( results )
    return ( unescape ( results[1] ) );
  else
    return null;
}

// show or hide various DIV's in large & ddl_large & both MTX_large templates

function showHide(div_id) {
	// hide all the divs
	document.getElementById('summary').style.display = 'none';
	document.getElementById('descr').style.display = 'none';
	document.getElementById('specs').style.display = 'none';
	document.getElementById('photos').style.display = 'none';
	document.getElementById('socialbook').style.display = 'none';
	// show the requested div
	document.getElementById(div_id).style.display = 'block';
}



// alternative to target="_blank" link must include rel="external"
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}
window.onload = externalLinks;


function deleteOption(object,index) {
    object.options[index] = null;
}

function addOption(object, text, value, selected) {
    var optionName = new Option(text, value, false, selected)
    object.options[object.length] = optionName;
    object.options[object.length-1].selected = selected;
    
}

function sortOptions(what) {
    var copyOption = new Array();
    for (var i=0;i<what.options.length;i++) {
        copyOption[i] = new Array(what[i].value,what[i].text,what[i].selected);
    }

    copyOption.sort(function(a,b) { return a[1]-b[1]; });

    for (var i=what.options.length-1;i>-1;i--)
        deleteOption(what,i);

    for (var i=0;i<copyOption.length;i++)
        addOption(what,copyOption[i][1],copyOption[i][0],copyOption[i][2]);
}

function sortCountries() {
    if (document.getElementById('countryx') != undefined) {
        sortOptions(document.getElementById('country'));
    }
}

addLoadEvent(sortCountries);


