/**
 * submits the languages form by clicking on a text link
 **/
function submit_form( formname, sel, fieldname )
{

  if(!sel || !formname || !fieldname)
    return;
  
  form = myGetElementById(formname);

  for (i=0;i<form.elements.length;i++) 
  {
    if (form.elements[i].id == formname+'.'+fieldname) 
    {
      form.elements[i].value = sel;
    }
  }  
  
  form.submit();

}


/**
  * show a span with an img tag as a tooltip
  **/
function showToolTip(e)
{
  if(!e) var e = window.event;
  
  if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
      targ = targ.parentNode;

  span = targ.parentNode.getElementsByTagName('span');
  span[0].style.display = 'block';      
  
}

function hideToolTip(e)
{
  if(!e) var e = window.event;
  
  if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
      targ = targ.parentNode;

  span = targ.parentNode.getElementsByTagName('span');
  span[0].style.display = 'none';      
  
}  


/**
 * Adds slashes to string
 **/

function addslashes( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Ates Goral (http://magnetiq.com)
    // +   improved by: marrtins
    // *     example 1: addslashes("kevin's birthday");
    // *     returns 1: "kevin\'s birthday"
 
    return str.replace('/(["\'\])/g', "\\$1").replace('/\0/g', "\\0");

}

/**
 * Strips slashes from string
 **/
function stripslashes( str ) 
{

str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\\\/g,'\\');
str=str.replace(/\\0/g,'\0');
return str;

}


/**
 * Get a page element by it's ID
 **/
function myGetElementById ( pElement )
{
	if ( arguments.length != 1 || pElement == null || typeof ( pElement ) == "undefined" )
		return null;

	if ( typeof ( pElement ) != "string" )
		return pElement;

	if ( document.getElementById )
		pElement = document.getElementById ( pElement );
	else if ( document.all )
		pElement = document.all[pElement];
	else
		pElement = null;

	return pElement;
}


/** 
 * Shows or hides the overlay
 **/
function overlay()
{
  el = myGetElementById("popoverlay");
  
  if( navigator.userAgent.indexOf("MSIE 6.0") > 1) 
  {
    //determine page size and stretch the overlay to fit
    pageSize = getPageSize();
    el.style.height = pageSize[1]+'px';
    el.style.width  = pageSize[0]+'px';
    
    //determine scroll height and set overlay content to that position
    
    if(window.pageXOffset)
      ypos = window.pageXOffset + 50;
    else
      ypos = document.documentElement.scrollTop + 50;
    
    al = myGetElementById("popoverlay_content");
    al.style.top = ypos + 'px';
  }
  el.style.display = (el.style.display == "block") ? "none" : "block";
}

function closeSample()
{
  if(!myGetElementById( 'popoverlay_content' )) 
    return false;      
  
  if(myGetElementById('sample_loading'))
  {
    oldimg = myGetElementById('sample_loading');
    oldimg.style.display = 'block';
  }
  
  if(myGetElementById( 'sample_image' ))
  {
    sample =  myGetElementById( 'sample_image' );
    obj = myGetElementById( 'popoverlay_content' );
    obj.removeChild (sample);
  }
  
  overlay();   
}

function showSample ( article_id, sample_id, num_samples, e )
{
	//alert(sample_id);
	
  try
  {
    if(!article_id || !num_samples)
      return false;
     
    if(!e)
      var e = window.event;
    
    if(!myGetElementById( 'popoverlay_content' )) 
      return false;  

    myGetElementById('popoverlay_loading').style.display = 'block';
    obj = myGetElementById('popoverlay_content');
    obj.innerHTML = '';  
                 
    img = document.createElement('img');
    img.src = 'images/samples/'+article_id+'_s'+sample_id+'.jpg';  
    img.id  = 'sample_image';
    img.onclick = closeSample;

    if (window.innerWidth)
       theWidth=window.innerWidth;
    else if (document.documentElement && document.documentElement.clientWidth)
       theWidth=document.documentElement.clientWidth;
    else if (document.body)
       theWidth=document.body.clientWidth;

    if (window.innerHeight)
       theHeight=window.innerHeight;
    else if (document.documentElement && document.documentElement.clientHeight)
       theHeight=document.documentElement.clientHeight;
    else if (document.body)
       theHeight=document.body.clientHeight;

    img.style.maxHeight = (theHeight-100)+'px';
    img.style.maxWidth = (theWidth-150)+'px';
    
    obj = myGetElementById('popoverlay_inner');
    obj.style.paddingTop = '35px';

    var close = document.createElement('div');
            
    close.style.position = 'absolute';
    close.style.top      = '15px';
    close.style.right    = '15px';
    close.style.width    = '50px';
    close.style.border   = 'solid red 1px';
    
    close.onclick        = closeSample;
    close.style.cursor   = 'pointer';
    var txt = document.createTextNode( 'X' );
    close.appendChild ( txt );
    obj.appendChild(close);
    
    link_holder = document.createElement('div');  
    link_holder.style.position = 'absolute';
    link_holder.style.top      = '15px';
    link_holder.style.left     = '15px';

    for( i = 0; i < num_samples; i++ )
    {
      link = document.createElement('a');
      link.setAttribute( 'href', '#' );
      link.setAttribute( 'name', i );
      //link.setAttribute( 'onclick', 'showSample('+article_id+', '+i+', '+num_samples+', event); event.returnValue=false; return false;' );
      if(document.all)
      {
      	
        link.onclick = function() { showSample(article_id, this.name, num_samples, event); return false; }
      }
      else
        link.setAttribute( 'onclick', 'showSample('+article_id+', '+i+', '+num_samples+', event); return false;' );
      
      link.style.marginRight = '10px';
      if(i == sample_id)
        link.style.color = '#FF0000';
      txt = document.createTextNode( (i + 1) );
      link.appendChild( txt );
      link_holder.appendChild( link );
    }    

    obj.appendChild ( link_holder );
                  
    setTimeout('insertSample( img );', 1000);    
 
  }
  catch(err)
  {
    alert('error occurred : ' + err.description);
  }
}


function insertSample( img )
{   
  myGetElementById('popoverlay_loading').style.display = 'none';
  obj = myGetElementById('popoverlay_content');
  obj.innerHTML = '';
  obj.appendChild(img);
}


/**
  * Handle key press
  **/
function onKeyPress(e)
{
  e = window.event || e;
  if(!e)
    return;

  switch(e.keyCode)
  {
    case 27:
      //the escape key was pressed
      //we close the overlay
      overlay();
      break;
  }
  return;
}

function getPageSize(){
  
  var xScroll, yScroll;
  
  if (window.innerHeight && window.scrollMaxY) {  
    xScroll = window.innerWidth + window.scrollMaxX;
    yScroll = window.innerHeight + window.scrollMaxY;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
  } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    xScroll = document.body.offsetWidth;
    yScroll = document.body.offsetHeight;
  }
  
  var windowWidth, windowHeight;
  
  if (self.innerHeight) {  // all except Explorer
    if(document.documentElement.clientWidth){
      windowWidth = document.documentElement.clientWidth; 
    } else {
      windowWidth = self.innerWidth;
    }
    windowHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if (document.body) { // other Explorers
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }  
  
  // for small pages with total height less then height of the viewport
  if(yScroll < windowHeight){
    pageHeight = windowHeight;
  } else { 
    pageHeight = yScroll;
  }

  // for small pages with total width less then width of the viewport
  if(xScroll < windowWidth){  
    pageWidth = xScroll;    
  } else {
    pageWidth = windowWidth;
  }
  arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
  return arrayPageSize;
}


/********************/
/* Cookie functions */
/********************/

function mySetCookie ( cookieName, cookieValue, daysToLive )
{
	var cookieText = cookieName + "=" + escape( cookieValue );

  if ( daysToLive > 0 )
  {
    var today = new Date();
    var expireDate = new Date(); 
    expireDate.setTime ( today.getTime () + 1000*60*60*24*daysToLive );
    cookieText += "; expires=" + expireDate.toGMTString ();
  }
 	
	document.cookie = cookieText;
	return null;
}

function myGetCookie ( cookieName )
{
	var cookieValue = null;
	var searchText = cookieName + "=";

	if ( document.cookie.length > 0 )
	{
		var startIndex = document.cookie.indexOf ( searchText );
		if ( startIndex != -1 )
		{
      startIndex += searchText.length;
			var endIndex = document.cookie.indexOf ( ";", startIndex );
			if ( endIndex == -1 )
				endIndex = document.cookie.length;
			cookieValue = unescape ( document.cookie.substring ( startIndex, endIndex ) );
		}
	}

	return cookieValue;
}

function myDeleteCookie ( cookieName, path )
{
	mySetCookie ( cookieName, "Deleted", -1, path )
}
/******* END COOKIES *******/