var previousWidth; var previousHeight;
function Resize_Tables (skip_resize_check) {

 if ( skip_resize_check || $(window).width() != previousWidth || $(window).height() != previousHeight) {

  previousWidth = $(window).width(); 
  previousHeight = $(window).height();

  $("td.remaining-height").css('height','auto');
  $("td.remaining-height").each( function() {

    var siblings = $(this).parent().siblings();
    var siblings_height = 0;
    for (var i = 0; i < siblings.length; i++ ) {
     var max_height = $(siblings[i].cells[0]).css('max-height');
     if ( typeof max_height == 'undefined' || max_height == 'none') { 
      siblings_height += $(siblings[i]).height(); 
     } else { 
      siblings_height += parseInt(max_height); 
     }
    }

    var totalHeight = this.offsetParent.offsetParent.clientHeight;
    if ( this.offsetParent.clientHeight > totalHeight ) {
     totalHeight = this.offsetParent.clientHeight;
    }
    var remaining_height = totalHeight - siblings_height - 12;
    if (this.clientHeight < remaining_height) {
     $(this).css('height',remaining_height + 'px');
    }
  });
  
 }
}

/**
 * Prevent from filtering the same list column multiple times by delaying the execution of the ajax call of 500 ms.
 * Multiple calls can be experienced when calling this function onkeyup while the user is still typing in the input.
 */
var delay_function_timers = new Array();
/**
 * Delay the execution of a function at the specified delay, prevent multiple call to the same function i.e.: onkeyup event
 * @param functionPointer the function to be called 
 * @param uid the unique ID to identify this call
 * @param params an array of parameters in the order they are required by the function
 * @param milliseconds the number of millliseconds we want to delay this call
 */
function Delay_Function( functionPointer, uid, params , milliseconds ){
 
 if ( typeof functionPointer == 'string' ) {
   functionPointer = window[functionPointer];
 }
 
 if ( typeof milliseconds == 'undefined' ) {
   milliseconds = 500;
 }
 
 if ( typeof params == 'undefined' || params == null ) {
  params = [];
 }
 
 if ( typeof delay_function_timers[uid] != 'undefined' ) {
  clearTimeout(delay_function_timers[uid]);
  delay_function_timers[uid] = null;
 }
 
 var timer = setTimeout(function(){functionPointer.apply(null,params);},milliseconds);
 delay_function_timers[uid] = timer;
 
}


function Clock_Tick() {
  $('#clocktime').each(function(){
   var current_time = this.innerHTML;
   var twelve_hour = false;
   if ( current_time.match(/(am|pm)/i) ) {
    twelve_hour = true;
   }
   var d = new Date();
   var utcHour = d.getUTCHours();
   var utcMin = d.getUTCMinutes();

   var localMin = utcMin + minute_offset;
   var localHour = utcHour + hour_offset;

   if ( localMin < 0 ) {
    localHour--;
   }
   if ( localMin > 59 ) {
    localHour++;
   }

   if ( localHour < 0 ) {
    localHour += 24;
   }
   if ( localHour > 23 ) {
    localHour -= 24;
   }
   
   if ( localMin < 10 ){
    localMin = '0' + localMin;
   }

   var afternoon = false
   if ( twelve_hour && localHour >= 12) {
    if ( localHour > 12) {
     localHour -= 12;
    }
    afternoon = true;
   }
   
   if ( localHour < 10 ){
    localHour = '0' + localHour;
   }

   var timeString = localHour + ':' + localMin;
   if ( twelve_hour ) {
     if (afternoon) {
      timeString+= ' PM';
     } else {
      timeString+= ' AM';
     }
   }

   this.innerHTML = timeString;

   setTimeout( 'Clock_Tick()', 60000);
  });
}


function Display_Password_Score( scoreData, meter_id ){
 
 var intScore = scoreData.score;
 var verdict = scoreData.verdict;
 
 var percent = parseInt((intScore/50) *100);
 
 document.getElementById(meter_id + '_percent').innerHTML = verdict + " (" + percent + "/100)";
 var width = document.getElementById(meter_id + "_container").offsetWidth;
 width = parseInt((intScore/50) * width);
 document.getElementById(meter_id + '_meter').style.width = width + 'px';
 
 if ( intScore < 16 ) {
  
  document.getElementById(meter_id + '_meter').style.backgroundColor = '#FF0000';
  
 } else if ( intScore < 28 ) {
  
  document.getElementById(meter_id + '_meter').style.backgroundColor = '#FFCC00';
  
 } else if ( intScore < 35 ) {
  
  document.getElementById(meter_id + '_meter').style.backgroundColor = '#4FEF1D';
  
 } else if ( intScore < 45 ) {
  
  document.getElementById(meter_id + '_meter').style.backgroundColor = '#00FF00';
  
 } else {
  
  document.getElementById(meter_id + '_meter').style.backgroundColor = '#006600';
  
 }
 
}

function Empty_Select ( select_id, keep_first ) {
 if( !is_array(select_id) ) {
  select_id = new Array(select_id);
 }
 for ( var i = 0; i < select_id.length; i++ ) {
   if ( document.getElementById(select_id[i]) && document.getElementById(select_id[i]).options ) {
    var select_element = document.getElementById(select_id[i]);
    var first_option_text = '';
    var first_option_value = '';
    if ( keep_first ) {
      first_option_text = select_element.options[0].innerHTML;
      first_option_value = select_element.options[0].value;
    }    
    while ( select_element.childNodes.length >= 1 )
    {
     select_element.removeChild( select_element.firstChild );       
    }
    //select_element.options.length = 0;
    if ( keep_first ) {
      select_element.options[0] = new Option( first_option_text, first_option_value );
    }
   }
 }
}

function Add_Select_Option ( select_id, value, text ) {
  if ( document.getElementById(select_id) && document.getElementById(select_id).options ) {
   var select_element = document.getElementById(select_id);
   var index = select_element.options.length;
   select_element.options[index] = new Option( text, value );
  }
}

function Set_Select_Options ( select_id, select_options ) {
  if ( document.getElementById(select_id) && document.getElementById(select_id).options ) {
   var select_element = document.getElementById(select_id);
   select_element.options.length = 0;
   for ( var i=0; i < select_options.length; i++ ) {
     select_element.options[i] = new Option( select_options[i].innerHTML, select_options[i].value);
   }
  }
}

function is_array (data){
  return (typeof(data)=='object' && (data instanceof Array));
}

function Close_Popup_Div () {
 if ( document.getElementById("popup_div") ) {
  var popupDiv = document.getElementById("popup_div");
  document.body.removeChild(popupDiv);
  $(document).unbind('click');
 }
 if ( document.getElementById("modal_div") ) {
  var modalDiv = document.getElementById("modal_div");
  document.body.removeChild(modalDiv);
 }
}
function Popup_Div( url, sender, width, height, title, title_class, center, close_image, modal ){
 
 if ( !width || typeof width == 'undefined' ) {
  width = 654;
 }
 if ( document.getElementById("popup_div") ) {
   return;
 }
 if ( typeof modal == 'undefined' ) {
  modal = true;
 }
   
 var scrollY = $(window).scrollTop();
 var innerWidth = $(window).width();
 var innerHeight = $(window).height();
 var pixelWidth = width;
 
 if ( width.toString().indexOf('%') > -1 ) {
   pixelWidth = innerWidth * (parseFloat(width) / 100);
 }
 
 if ( height.toString().indexOf('%') > -1 ) {
   height = innerHeight * (parseFloat(height) / 100);
 }
 
 var curleft = curtop = 0;
 var obj = sender;
 if ( !center && obj ) {
   curleft += $(obj).offset().left;
   curtop += $(obj).offset().top;
   
   curtop += $(sender).height()+5;
   /*if ( scrollY && scrollY >= curtop ) {
    curtop = scrollY + 15;
    curleft += $(sender).width() +10;
   }*/
   
   if ( innerWidth <= (curleft + pixelWidth) ) {
    curleft = innerWidth - pixelWidth;
   }
 } else {
   curleft = (innerWidth/2) - (pixelWidth/2);
   curtop = ((innerHeight/2) - (height/2)) + scrollY;
 }
   
  if ( curleft < 5 ) {
    curleft = 5;
  }
  
  if ( curleft+width+4 > innerWidth ) {
     curleft = curleft - ((curleft+width+4) - innerWidth);
  }
  
  if ( curtop < 5 ) {
    curtop = 5;
  }
 
 var popupDiv = document.createElement("div");
 popupDiv.setAttribute("id", "popup_div");
 popupDiv.setAttribute("name", "popup_div");
 popupDiv.style.position = "absolute";
 popupDiv.style.width = pixelWidth + "px";
 popupDiv.style.height = height + "px";
 popupDiv.style.left = curleft + "px";
 popupDiv.style.top = curtop + "px";
 popupDiv.style.zIndex = 99998;
 
 if ( modal ) {
  
  var modalDiv = document.createElement("div");
  modalDiv.setAttribute("id", "modal_div");
  modalDiv.style.position = "absolute";
  modalDiv.style.zIndex = 8;
  modalDiv.style.left = "0px";
  modalDiv.style.top = "0px";
  modalDiv.style.width = innerWidth + "px";
  modalDiv.style.height = $(document).height() + "px";
  modalDiv.style.backgroundColor = "#000000";
  modalDiv.style.zIndex = 99997;
  modalDiv.onclick = function(){
   document.body.removeChild(popupDiv);
   document.body.removeChild(modalDiv);
  };
  $(modalDiv).fadeTo(0,0.65);
 }
 
 var titleDiv = document.createElement("div");
 titleDiv.setAttribute("class", title_class );
 titleDiv.className = title_class;
 titleDiv.style.margin = "0";
 titleDiv.style.position = "relative";
 
 var titleTable = document.createElement("table");
 titleTable.style.margin = "0";
 titleTable.cellSpacing = "0";
 titleTable.cellPadding = "0";
 
 var titleRow = titleTable.insertRow(0);
 var leftCell = titleRow.insertCell(0);
 leftCell.setAttribute("class", 'left' );
 leftCell.className = 'left';
 var centerCell = titleRow.insertCell(1);
 centerCell.setAttribute("class", 'center' );
 centerCell.className = 'center';
 var rightCell = titleRow.insertCell(2);
 rightCell.setAttribute("class", 'right' );
 rightCell.className = 'right';

 titleText = document.createTextNode(title);
 centerCell.appendChild(titleText);
 centerCell.innerHTML += '&nbsp;';
 
 titleDiv.appendChild(titleTable);
 
 var footerDiv = document.createElement("div");
 footerDiv.setAttribute("class", 'bottombox' );
 footerDiv.className = 'bottombox';
 footerDiv.style.margin = "0";
 
 var footerTable = document.createElement("table");
 footerTable.style.margin = "0";
 footerTable.cellSpacing = "0";
 footerTable.cellPadding = "0";
 
 var footerRow = footerTable.insertRow(0);
 var leftCell = footerRow.insertCell(0);
 leftCell.setAttribute("class", 'left' );
 leftCell.className = 'left';
 var centerCell = footerRow.insertCell(1);
 centerCell.setAttribute("class", 'center' );
 centerCell.className = 'center';
 var rightCell = footerRow.insertCell(2);
 rightCell.setAttribute("class", 'right' );
 rightCell.className = 'right';
 centerCell.innerHTML += '&nbsp;';
 
 footerDiv.appendChild(footerTable);
 
 var closeSpan = document.createElement("span");
 closeSpan.style.position = "absolute";
 closeSpan.style.right = "12px";
 closeSpan.style.top = "0px";
 closeSpan.style.cursor = "pointer";
 closeSpan.style.border = "none";
 closeSpan.style.lineHeight = "14px";
 closeSpan.style.verticalAlign = "bottom";

 closeImage = new Image(); 
 closeImage.src = close_image;
 
 var closeImg = document.createElement("img");
 closeImg.setAttribute("src", close_image);
 //closeImg.setAttribute("alt", "X");
 closeImg.style.marginTop = "0px";
 closeImg.style.width = "21px";
 closeImg.style.marginLeft = "10px";
 closeImg.style.verticalAlign = "middle";
 closeImg.onclick = function(){
  document.body.removeChild(popupDiv);
  $(document).unbind('click',monitorClick);

  if ( modal ) {
   document.body.removeChild(modalDiv);
  }
 };

 closeSpan.appendChild(closeImg);
 titleDiv.appendChild(closeSpan);
 popupDiv.appendChild(titleDiv);
 
 // clone the title element so we can find its height
 titleSpanClone = titleDiv.cloneNode(true);
 titleSpanClone.style.visibility = 'hidden';
 document.body.appendChild(titleSpanClone);
 var titleHeight = titleSpanClone.offsetHeight;
 document.body.removeChild(titleSpanClone);
 
 // clone the title element so we can find its height
 footerSpanClone = footerDiv.cloneNode(true);
 footerSpanClone.style.visibility = 'hidden';
 document.body.appendChild(footerSpanClone);
 titleHeight += footerSpanClone.offsetHeight;
 document.body.removeChild(footerSpanClone);
 
 var object_height = (height - titleHeight) + "px";
 
 if (typeof popupDiv.insertAdjacentHTML != 'undefined') {
   
   // ****** IFRAME is required in IE window.parent won't work on object! ******* //
   popupDiv.insertAdjacentHTML('beforeEnd', 
                               ['<iframe id="obj_results" frameborder="0" allowtransparency="true" src="' + url + '"', 
                                ' width="100%" height="' + object_height + '">', 
                                '<\/iframe>'  
                               ].join('\r\n'));
 } else {
  var frame_object = frame_object = document.createElement("object");
  frame_object.setAttribute("data", url);
  frame_object.setAttribute("type", "text/html");
  frame_object.style.width = "100%";
  frame_object.style.height = object_height;
  popupDiv.appendChild(frame_object);
 }

 if ( modal ) {
  document.body.appendChild(modalDiv);
 }
 

 popupDiv.appendChild(footerDiv);
 
 document.body.appendChild(popupDiv);
 
 var monitorClick = function(e){
  var evt = (e)?e:event;
  
  var clicked_element = (evt.srcElement)?evt.srcElement:evt.currentTarget;

  try{
  var descendent_clicked = $(sender).has(clicked_element).length;
  if ( !modal && clicked_element != popupDiv && clicked_element != titleSpan && clicked_element != closeSpan && (!printImg || clicked_element != printImg) && clicked_element != sender && !descendent_clicked ){
   if ( document.getElementById("popup_div") ) {
    document.body.removeChild(document.getElementById("popup_div"));
    if ( modal ) {
     document.body.appendChild(modalDiv);
    }
    // remove event handler IMPORTANT!!!
    $(window).unbind('click',monitorClick);
   }
  }
  } catch(exception) {}
  return true;
};
 
 $(window).bind('click',monitorClick);
     
 if ( center ) {  
   $(window).resize(function(eventObject){
       var newWidth = $(window).width();
       var newHeight = $(window).height();
       var newPixelWidth = width;
       
       if ( width.toString().indexOf('%') > -1 ) {
         newPixelWidth = newWidth * (parseFloat(width) / 100);
         document.getElementById("popup_div").style.width = newPixelWidth + "px";
         titleSpan.style.width = (newPixelWidth - 5) +"px";
       }
     
       var new_left = (newWidth/2) - (newPixelWidth/2);
       if (new_left < 5) {
        new_left = 5;
       }
       var new_top = (newHeight/2) - (height/2);
       if (new_top < 5) {
        new_top = 5;
       }
       popupDiv.style.left = new_left + "px";
       popupDiv.style.top = new_top + $(window).scrollTop() + "px";
       

       if ( modal ) {
        modalDiv.style.width = newWidth + 'px';
        modalDiv.style.height = $(document).height() + 'px';
       }
     
   });
   
   $(window).scroll(function ( ev ) { 
     var newHeight = $(window).height();
     var new_top = (newHeight/2) - (height/2);
     if (new_top < 5) {
      new_top = 5;
     }
     popupDiv.style.top = new_top + $(window).scrollTop() + "px";
   });
 }
 

     
 return false;
}
function Popup_Video( url, width, height, close_image, modal ){
 
 if ( !width || typeof width == 'undefined' ) {
  width = 654;
 }
 if ( document.getElementById("popup_div") ) {
   return;
 }
 if ( typeof modal == 'undefined' ) {
  modal = true;
 }
   
 var scrollY = $(window).scrollTop();
 var innerWidth = $(window).width();
 var innerHeight = $(window).height();
 var pixelWidth = width;
 
 if ( width.toString().indexOf('%') > -1 ) {
   pixelWidth = innerWidth * (parseFloat(width) / 100);
 }
 
 if ( height.toString().indexOf('%') > -1 ) {
   height = innerHeight * (parseFloat(height) / 100);
 }
 
 var curleft = curtop = 0;

   curleft = (innerWidth/2) - (pixelWidth/2);
   curtop = ((innerHeight/2) - (height/2)) + scrollY;

   
  if ( curleft < 5 ) {
    curleft = 5;
  }
  
  if ( curleft+width+4 > innerWidth ) {
     curleft = curleft - ((curleft+width+4) - innerWidth);
  }
  
  if ( curtop < 5 ) {
    curtop = 5;
  }
 
 var popupDiv = document.createElement("div");
 popupDiv.setAttribute("id", "popup_div");
 popupDiv.setAttribute("name", "popup_div");
 popupDiv.style.position = "absolute";
 popupDiv.style.width = pixelWidth + "px";
 popupDiv.style.height = height + "px";
 popupDiv.style.left = curleft + "px";
 popupDiv.style.top = curtop + "px";
 popupDiv.style.zIndex = 99998;
 
 if ( modal ) {
  
  var modalDiv = document.createElement("div");
  modalDiv.setAttribute("id", "modal_div");
  modalDiv.style.position = "absolute";
  modalDiv.style.zIndex = 8;
  modalDiv.style.left = "0px";
  modalDiv.style.top = "0px";
  modalDiv.style.width = innerWidth + "px";
  modalDiv.style.height = $(document).height() + "px";
  modalDiv.style.backgroundColor = "#000000";
  modalDiv.style.zIndex = 99997;
  modalDiv.onclick = function(){
   document.body.removeChild(popupDiv);
   document.body.removeChild(modalDiv);
  };
  $(modalDiv).fadeTo(0,0.65);
 }
 
 var titleDiv = document.createElement("div");
 titleDiv.style.height = "0px";
 titleDiv.style.margin = "0";
 titleDiv.style.position = "relative";

 closeImage = new Image(); 
 closeImage.src = close_image;
 
 var closeImg = document.createElement("img");
 closeImg.setAttribute("src", close_image);
 //closeImg.setAttribute("alt", "X");
 closeImg.style.position = "absolute";
 closeImg.style.right = "-26px";
 closeImg.style.top = "0px";
 closeImg.style.cursor = "pointer";
 closeImg.style.margin = "0px";
 closeImg.onclick = function(){
  document.body.removeChild(popupDiv);

  if ( modal ) {
   document.body.removeChild(modalDiv);
  }
 };

 titleDiv.appendChild(closeImg);
 popupDiv.appendChild(titleDiv);
 
 // clone the title element so we can find its height
 titleSpanClone = titleDiv.cloneNode(true);
 titleSpanClone.style.visibility = 'hidden';
 document.body.appendChild(titleSpanClone);
 var titleHeight = titleSpanClone.offsetHeight;
 document.body.removeChild(titleSpanClone);
 
 var object_height = (height - titleHeight) + "px";
 
 if (typeof popupDiv.insertAdjacentHTML != 'undefined') {
   
   // ****** IFRAME is required in IE window.parent won't work on object! ******* //
   popupDiv.insertAdjacentHTML('beforeEnd', 
                               ['<iframe id="obj_results" frameborder="0" allowtransparency="true" src="' + url + '"', 
                                ' width="100%" height="' + object_height + '">', 
                                '<\/iframe>'  
                               ].join('\r\n'));
 } else {
  var frame_object = frame_object = document.createElement("object");
  frame_object.setAttribute("data", url);
  frame_object.setAttribute("type", "text/html");
  frame_object.style.width = "100%";
  frame_object.style.height = object_height;
  popupDiv.appendChild(frame_object);
 }

 if ( modal ) {
  document.body.appendChild(modalDiv);
 }
 
 document.body.appendChild(popupDiv);
     
 $(window).resize(function(eventObject){
     var newWidth = $(window).width();
     var newHeight = $(window).height();
     var newPixelWidth = width;
     
     if ( width.toString().indexOf('%') > -1 ) {
       newPixelWidth = newWidth * (parseFloat(width) / 100);
       document.getElementById("popup_div").style.width = newPixelWidth + "px";
       titleSpan.style.width = (newPixelWidth - 5) +"px";
     }
   
     var new_left = (newWidth/2) - (newPixelWidth/2);
     if (new_left < 5) {
      new_left = 5;
     }
     var new_top = (newHeight/2) - (height/2);
     if (new_top < 5) {
      new_top = 5;
     }
     popupDiv.style.left = new_left + "px";
     popupDiv.style.top = new_top + $(window).scrollTop() + "px";
     

     if ( modal ) {
      modalDiv.style.width = newWidth + 'px';
      modalDiv.style.height = $(document).height() + 'px';
     }
   
 });
 
 /*$(window).scroll(function ( ev ) { 
   var newHeight = $(window).height();
   var new_top = (newHeight/2) - (height/2);
   if (new_top < 5) {
    new_top = 5;
   }
   popupDiv.style.top = new_top + $(window).scrollTop() + "px";
 });*/
 

     
 return false;
}


function Limit_Text( field, max_chars ) {
		
		if ( field.value.length > max_chars ) {
				field.value = field.value.substring( 0, max_chars );
		}
		
}

function Constrain_Int( field ) {
 
 if ( field.value != '' ) {
   field.value = isNaN(parseInt(field.value)) ? '' : parseInt(field.value);
 }
 
}

function Constrain_Float( field ) {

 if ( field.value != '' ) {
   var add_dot = false;
   if ( field.value.substr(field.value.length-1) == '.' && field.value.indexOf('.') == field.value.length-1) {
    
     add_dot = true;
    
   }
   field.value = isNaN(parseFloat(field.value)) ? '' : parseFloat(field.value);
   if ( add_dot ) {
    field.value += '.';
   }
 }
 
}

function Show_Login() {
 document.getElementById('loginform').style.display='block';
 document.getElementById('login_name').focus();
}

/**
 * This code run automaticly on page load
 */
$(function(){
 
 // this add the this.form to the table buttons of a table 
 $('form .send table').each( function(){
   if ( !this.form ) {
    try{
     this.form = $(this).closest('form')[0];
    } catch(error) {}
   }
 });
 
 // this override the form.submit function so it calls onsubmit before submitting the form
 $('form').each( function(){
  this._submit = this.submit;
  this.submit = function(){
   if ( typeof this.onsubmit == 'function' ) {
    this.onsubmit();
   }
   this._submit();
  }
 });
 
 //$('#coursecontent .send').clone(true).prependTo('#coursecontent');
});

function Display_Cart_Transition(){
  $('#cart_transition').slideDown();
  $(document).scrollTop($('#cart_transition').offset().top - 20);
}

function Hide_Cart_Transition(){
  $('#cart_transition').slideUp();
}

