var opera = (navigator.appName.indexOf("Opera") != -1) ? true : false;
var firefox = navigator.userAgent.toLowerCase(); 
    firefox = (firefox.indexOf("firefox") != -1) ? true : false;
var safari = navigator.userAgent.toLowerCase();
    safari = (safari.indexOf("safari") != -1) ? true : false;
var chrome = navigator.userAgent.toLowerCase();
    chrome = (chrome.indexOf('chrome') != -1) ? true : false;
var msie_only = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;
var msie_old = false; // MSIE versions older than 7
var msie_80  = false; // MSIE version 8.0 or newer
var msie_70  = false; // MSIE vesrion 7.0
var msie_60  = false; // only MSIE 6.0
var msie_55  = false; // only MSIE 5.5

if (msie_only) {
  fullVerStr = navigator.appVersion;
  verStr = fullVerStr.substring(fullVerStr.indexOf('MSIE')+4,fullVerStr.length);
  verStr = verStr.substring(0,verStr.indexOf(';'));
  msie_old = (parseFloat(verStr) <  7.0) ? true : false;
  msie_80  = (parseFloat(verStr) >= 8.0) ? true : false;  
  msie_70  = (parseFloat(verStr) == 7.0) ? true : false;
  msie_60  = (parseFloat(verStr) == 6.0) ? true : false;
  msie_55  = (parseFloat(verStr) == 5.5) ? true : false;
}

document.writeln('<style type="text/css">\n/*<![CDATA[*/\n');

  if (msie_80) {
    document.writeln('.Radio, .CheckBox {margin-left:4px; margin-right:2px;}\n');
    document.writeln('.AccessCBList input {top:2px;}\n');  
  }
  
  if (msie_70) {
    document.writeln('.AccessCBList input {margin-right:-1px; top:2px;}\n');
  
  }

  if (msie_old) {
    document.writeln('.WebUp {position:absolute; top:0px; left:0px;}\n');
    document.writeln('.WebDown {position:absolute; top:0px; left:0px}\n');
    document.writeln('.WebRemove {position:absolute; top:0px; left:0px; margin-top:8px}\n');
    document.writeln('.PublicZoneWidth {height:400px}\n');
    document.writeln('.HeaderLinks .SubMenu li a {height:17px}\n');
    document.writeln('.CentreColumn {}\n');
    document.writeln('.ContentsBlock .LeftColumn {margin-left:1px}\n');
    document.writeln('.MiddleSection .GlowTop {left:-5px}\n');
    document.writeln('.MiddleSection .GlowBot {left:-5px}\n');
    document.writeln('.MidColumn .ColumnLink {height:35px}\n');    
    document.writeln('.AccessCBList input {margin-right:-2px; top:2px;}\n');
    document.writeln('.FormSpaceTop2 {margin-top:50px;}\n');
    document.writeln('.ContentsBlock .LeftColumn {margin-right:11px;}\n');
    document.writeln('.ContentsBlock .LeftColumn .EditBox {width:128px;}\n');
  }

  if (msie_only) {
    document.writeln('.RbVerticalList {left:-6px; top:-2px;}\n');
    document.writeln('.RbVerticalList input {position:relative; top:3px}\n');
    document.writeln('.CbVerticalList {left:-6px; top:-2px;}\n');
    document.writeln('.CbVerticalList input {position:relative; top:3px}\n');
  }
  if (opera) {
    document.writeln('.Radio, .CheckBox {margin-left:3px; margin-right:2px;}\n');  
    document.writeln('.RbVerticalList {left:-1px;}\n');
    document.writeln('.RbVerticalList input {margin-top:2px; margin-bottom:3px;}\n'); 
    document.writeln('.CbVerticalList {left:-1px;}\n');
    document.writeln('.CbVerticalList input {margin-top:2px; margin-bottom:3px;}\n');     
  }
  if (firefox) {
    document.writeln('.Radio, .CheckBox {margin:4px; margin-right:3px; top:2px;}\n');
    document.writeln('.Radio2 {position:relative; top:2px;}\n');
    document.writeln('.RbVerticalList input {margin-top:4px; margin-bottom:3px;}\n');    
    document.writeln('.RbVerticalList label {position:relative; top:-3px; margin-left:6px;}\n');
    document.writeln('.CbVerticalList input {margin-top:4px; margin-bottom:3px;}\n');    
    document.writeln('.CbVerticalList label {position:relative; top:-3px; margin-left:6px;}\n');
    document.writeln('.AccessCBList td {padding-bottom:10px; }\n');
    document.writeln('.AccessCBList input {top:3px;}\n');
  }
  if (safari) {
    document.writeln('.Radio, .CheckBox {margin:4px; top:0px;}\n');
    document.writeln('.RbVerticalList input {margin-top:4px; margin-bottom:3px;}\n');    
    document.writeln('.RbVerticalList label {margin-left:7px;}\n');
    document.writeln('.CbVerticalList input {margin-top:4px; margin-bottom:3px;}\n');    
    document.writeln('.CbVerticalList label {margin-left:7px;}\n');    
  }
  if (chrome) {
    document.writeln('.RbVerticalList input {margin-top:4px; margin-bottom:3px;}\n');    
    document.writeln('.RbVerticalList label {position:relative; top:-2px; margin-left:7px;}\n');
    document.writeln('.CbVerticalList input {margin-top:4px; margin-bottom:3px;}\n');    
    document.writeln('.CbVerticalList label {position:relative; top:-2px; margin-left:7px;}\n');
  }
document.writeln('/*]]>*/\n</style>\n');


function ajaxFunction() {
  var xmlhttp;
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
  }
  else if (window.ActiveXObject) {
    // code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4) {
      //document.myForm.time.value = xmlhttp.responseText;
      alert('222')
    }
  }
}





function clearField (field_to_clear, initial_value) {
  if (initial_value == field_to_clear.value) field_to_clear.value="";
} 

function fillField (field_to_clear, initial_value) {
  if (field_to_clear.value == "") field_to_clear.value=initial_value;
}

//information box on validation image rollover
function validationInfo(operation, img_ref){
  if(operation == "show") {
    if (img_ref.alt.length > 0) img_ref.previousSibling.innerHTML = img_ref.alt;
    img_ref.alt = "";
    img_ref.previousSibling.style.display = "block";
  }
  if(operation == "hide") img_ref.previousSibling.style.display = "none";
}

//Limits length of text in textarea "element" (dom reference), writes information in "info" (id), counts characters
function checkLength(element, info, max_length){
  var actual_length = element.value.length;
  var characters_left = max_length - actual_length;
  if (characters_left < 0) characters_left = 0;

  if (actual_length == 0) {
    document.getElementById(info).innerHTML="Maximum text length is "+ max_length +" characters.";
  }
  if ((actual_length > 0)&&(actual_length <= max_length)) {
    document.getElementById(info).innerHTML="You may enter "+ characters_left +" more characters.";
  }
  if (actual_length > max_length) {
    element.value = element.value.substr(0, max_length);
    document.getElementById(info).innerHTML="You may enter "+ characters_left +" more characters.";
  }
}

//init a textarea, textarea_container - id, for charaacters limit
function initLengthInfo(textarea_container, info_id, max_length) {
  if (document.getElementById(textarea_container)) {
    element_ref = document.getElementById(textarea_container).getElementsByTagName("textarea")[0];
    checkLength(element_ref, info_id, max_length);
  }
}

//for search panel
function clearSearchField (field_to_clear) {
  var tmp_obj = field_to_clear.previousSibling;
  tmp_obj=tmp_obj.previousSibling;
  var initial_value = tmp_obj.value;
  if (initial_value == field_to_clear.value) field_to_clear.value="";
} 

//for search panel
function fillSearchField (field_to_fill) {
  var tmp_obj = field_to_fill.previousSibling;
  tmp_obj=tmp_obj.previousSibling;
  var initial_value = tmp_obj.value;
  if (field_to_fill.value == "") field_to_fill.value=initial_value;
}

function __printPage(strUrl) {
  var newWindow = window.open(strUrl,"Print", "toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width=800,height=700");
}
	
function __popupPage(strUrl) {
  var newWindow = window.open(strUrl,"PopUp", "toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width=916,height=600,top=200,left=200");
}


function openWysiwyg(html_source_id, html_destination_id, editor_width, editor_height, popup_width, popup_height, popup_left, popup_top) {
  
  var dynamic_url = "http://" + document.domain;
  if (document.URL.indexOf(":81/") > -1) dynamic_url += ":81";
  if (document.URL.indexOf("localhost") > -1) {
    alert("The editor shouldn't be used on localhost. It should run from the server on which the image storage folder is located ('cms-uploaded' directory).");
    dynamic_url = "http://workplacesystems.obs-group.co.uk:81"
  }

  var ww = window.open("", "EditorWindow", "resizable=no,width=" + popup_width + ",height= " + popup_height + ",top=" + popup_top + ",left= " + popup_left);
    
  //alert(htmlEncode(document.getElementById(html_source_id).value));
  ww.document.open();
  ww.document.writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\n<head>\n<title>Content editor</title>\n<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />\n</head>\n<body>');
  ww.document.writeln('<div style="margin-left:auto; margin-right:auto; width:'+ editor_width +'px;">');
  ww.document.writeln('<object type="application/x-xstandard" id="editor1" width="'+ editor_width +'" height="'+ editor_height +'">');
  ww.document.writeln('<param name="Value" value="'+ htmlEncode(document.getElementById(html_source_id).value) +'" />');

  ww.document.writeln('<param name="ImageLibraryURL"      value="' + dynamic_url + '/xstandard/imagelibrary.aspx" />');
  ww.document.writeln('<param name="AttachmentLibraryURL" value="' + dynamic_url + '/xstandard/attachmentlibrary.aspx" />');
  ww.document.writeln('<param name="LinkLibraryURL"       value="' + dynamic_url + '/xstandard/linklibrary.aspx" />');
  ww.document.writeln('<param name="Styles"               value="' + dynamic_url + '/xstandard/editor-styles.xml" />');
  ww.document.writeln('<param name="License"              value="' + dynamic_url + '/xstandard/license.txt" />');
  ww.document.writeln('<param name="CSS"                  value="' + dynamic_url + '/xstandard/general.css" />');

  ww.document.writeln('<param name="ProxySetting" value="direct" />'); //if default proxy settings stops it from working
	ww.document.writeln('<param name="EnableTimestamp" value="no" />'); //the comment inserted at the begining of html
	ww.document.writeln('<param name="EnablePasteMarkup" value="no" />'); //copying from word with formating retained - doesn't work good enough
	ww.document.writeln('<param name="Lang" value="en" />');
	ww.document.writeln('<param name="Dir" value="ltr" />');
  ww.document.writeln('<param name="ClassImageFloatLeft"  value="LeftImage" />');
	ww.document.writeln('<param name="ClassImageFloatRight" value="RightImage" />');
  ww.document.writeln('<param name="ToolbarWysiwyg" value="ordered-list, unordered-list, definition-list, draw-layout-table, draw-data-table, separator, image, hyperlink, attachment, source, preview, help" />');
  ww.document.writeln('<param name="ToolbarPreview" value="wysiwyg, source, preview" />');
  ww.document.writeln('<param name="ToolbarSource" value="word-wrap, validate,, wysiwyg, source, preview" />');
  //ww.document.writeln('<param name="ShowStyles" value="no" />'); //styles ddlist in the editor
  ww.document.writeln('<param name="Base" value="'+ dynamic_url +'/cms-uploaded/" />');
  ww.document.writeln('</object>');
  
  ww.document.writeln('<div>\n<span style="float:right; height:16px; display:block; margin-top:10px; background-color:#003366;"><a style="font-weight:bold; font-size:10px; font-family:Verdana; text-decoration:none; color:#fff; padding:1px 8px 2px 8px; display:block;" href="javascript:self.opener.document.getElementById(\''+html_destination_id+'\').value=document.getElementById(\'editor1\').value; self.close();" title="Update the page form and close the editor">Close</a></span>\n</div>');
  ww.document.writeln('</body>\n</html>');
  ww.document.close();
}

function htmlEncode(s) {
  var str = new String(s);
  str = str.replace(/&/g, "&amp;");
  str = str.replace(/</g, "&lt;");
  str = str.replace(/>/g, "&gt;");
  str = str.replace(/"/g, "&quot;");
  return str;
}

//function invoked onload at body tag; or just before closiong body tag
function initArrays() {
  select_objects = document.getElementsByTagName('select'); //must be global
  all_selects = new Array();  //must be global, 3 dimensional array [ids of all selects][counter of each option in given select]['value' or 'text']= value or text of given option in given select
  options_selected = new Array(); //must be global, 2 dimensional array [ids of selects on which onchange or onkeydown function was invoked][values of selected options for associative addressing]=true ,but this boolean value is not used, existence of array entry is tested.
   
  for(var i=0; i<select_objects.length; i++) {
    var index = select_objects[i].id;  // id of select tag
    all_selects[index]= new Array();
    options_selected[index]= new Array();
    for (var j=0; j<select_objects[i].length; j++) {  //number of options elements in given select tag
      all_selects[index][j]= new Array();
      all_selects[index][j]['value'] = select_objects[i].options[j].value;
      all_selects[index][j]['text']  = select_objects[i].options[j].text;
    }//for j
  }//for i
}

//function invoked onkeyup of input tag containing search phrase
function filterFunction(container_id, filter) {
  //container_id - id of a div containing related ddl
  //filter - reference to input tag containing filter phrase or "" for bringing back all options into select
  //sel - reference to related dropdownlist (select)
  var container_ref = document.getElementById(container_id);
  var sel = container_ref.getElementsByTagName("select")[0];
  if (sel) { 
      var select_id = sel.id;
  } else {
      return; //if sendForm gives unpredictable id of other listboxes that don't use filter
  }

  if (options_selected[select_id].length == 0) 
     selectFunction(document.getElementById(select_id)); //remember already selected options, not only these selected manualy
  sel.options.length=0; 
  var k=0;
  for (var i=0; i<all_selects[select_id].length; i++) {
    var t = all_selects[select_id][i]['text'];
    var t_for_search = t.toLowerCase();
    var v = all_selects[select_id][i]['value'];
    var filter_text = (typeof filter == "object") ? filter.value.toLowerCase() : '';
    if (t_for_search.indexOf(filter_text) >= 0)
       sel.options[k++] = new Option(t, v, false, (typeof options_selected[select_id][v] != "undefined"));
  }
}

//function invoked onchange at select tag
function selectFunction(this_select) {
  var number_of_options = this_select.options.length;  //in given select
  for (var i=0; i<number_of_options; i++) {
    if (this_select.options[i].selected == true) {
      options_selected[this_select.id][this_select.options[i].value]=true;
    } else {
      if (typeof options_selected[this_select.id][this_select.options[i].value] != "undefined") 
          delete options_selected[this_select.id][this_select.options[i].value];
    }
  }   
}

function __printPage(strUrl) {
  var newWindow = window.open(strUrl,"Print", "toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width=650,height=700");
}
	
function __popupPage(strUrl) {
  var newWindow = window.open(strUrl,"PopUp", "toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width=916,height=600,top=200,left=200");
}

//show single element with given id
function show(id_show, type) {
  switch (type) {
    case 'fade':
      $('#' + id_show).fadeIn(500);
    break;
    case 'slide':
      $('#' + id_show).slideDown(500);
    break;
    case 'blink':
      $('#' + id_show).show();
    break;
  }
}

//hide single element with given id
function hide(id_hide, type) {
  switch (type) {
    case 'fade':
      $('#' + id_hide).fadeOut(700);
    break;
    case 'slide':
      $('#' + id_hide).slideUp(700);
    break;
    case 'blink':
      $('#' + id_hide).hide();
    break;
  }
}


// hide 0 or more elements and show 0 or more oter elements, indicated by id
// css width property is required for every switchable element. Can be in pixels or percents
// use example1: hideShowList(["id1","id2","id3"],["id6"], fade);
// use example2: hideShowList(["-"],["id6","id7","id8"]);
function hideShowList(hide_array, show_array, type) {
  if (hide_array[0] != '-') {
    for(var i=0; i<hide_array.length; i++) {
      hide(hide_array[i], type);
    }
  }
  
  if (show_array[0] != '-') {
    for(var i=0; i<show_array.length; i++) {
      show(show_array[i], type);
    }
  }
}

function CorrectPngForIE(id_or_parent_id) {
  if (msie_old) {
    var img_ref;
    if (document.getElementById(id_or_parent_id).nodeName.toLowerCase() == "img") {
      img_ref = document.getElementById(id_or_parent_id);
    } else {
      img_ref = document.getElementById(id_or_parent_id).getElementsByTagName("img")[0];
    }
    img_ref.src = "/images/spacer.gif";
   }
}

//Add This Button
var addthis_brand = "WorkPlace";
var addthis_header_color = "#FFF";
var addthis_header_background = "#C00";
//END



function glowBox(clicked) {

  var option_selected = $(clicked).find('option:selected').val()
  var c = 0
  $(clicked).parent().parent().parent().parent().parent().find('.WebPartTable .WebPartListBox').find(".GlowSpan").fadeOut(300)
  if (option_selected == 'Header') { $(clicked).parent().parent().parent().parent().parent().find('.WebPartTable .WebPartListBox').eq(0).find(".GlowSpan").fadeIn(400) }
  if (option_selected == 'Left') { $(clicked).parent().parent().parent().parent().parent().find('.WebPartTable .WebPartListBox').eq(1).find(".GlowSpan").fadeIn(400) }
  if (option_selected == 'Middle') { $(clicked).parent().parent().parent().parent().parent().find('.WebPartTable .WebPartListBox').eq(2).find(".GlowSpan").fadeIn(400) }
  if (option_selected == 'Right') { $(clicked).parent().parent().parent().parent().parent().find('.WebPartTable .WebPartListBox').eq(3).find(".GlowSpan").fadeIn(400) }
  if (option_selected == 'Footer') { $(clicked).parent().parent().parent().parent().parent().find('.WebPartTable .WebPartListBox').eq(4).find(".GlowSpan").fadeIn(400) }
    
 }


 function updateWebPage() {
   var listbox_index = $('.WebPartTable select').length;
   for (var j = 0; j < listbox_index; j++) {
     var value_index = $('.WebPartListBox').eq(j).find('select option').length;
     var hidden_field = $('.HiddenFields').find('input').eq(j);

     var list_string = ''
     for (var i = 0; i < value_index; i++) {
       var current_value = $('.WebPartListBox').eq(j).find('select option').eq(i).attr('value') + ','
       var list_string = list_string + current_value
     }

     $(hidden_field).attr('value', list_string)

     //alert($(hidden_field).attr('value'))
   }
 }

var flash_file;
var flash_width;
var flash_height;
var no_flash_div;
 function addFlash() {
   swfobject.embedSWF(flash_file, no_flash_div, flash_width, flash_height, "8.0.22", "", {}, { allowscriptaccess: "always", wmode: "transparent" }, {});
 }


 function popDrag(id1, id2) {
   var this_draglist = document.getElementById(id2);
   var this_contents = ''
  $('#' + id1 + ' option').each(function() {
     var this_value = $(this).attr('value');
     this_contents += '<div value=' + this_value + ' class="drag">' + $(this).html() + '</div>'
   });
   this_draglist.innerHTML = this_contents

}

function dragInit() {

  $('.WebPartPosTable .DragList div').click(function() {
    //alert($(this).attr('value'))
  $('.DragList div').removeClass('SelectedDiv')
    $(this).addClass('SelectedDiv')
    var look_for = $(this).parent().find('div').index(this)
    $(this).closest('td').find('option').eq(look_for).attr('selected', 'selected')
    //alert($(this).closest('td').find('option:selected').html())

    var grab_value = $(this).attr('value')
    $('option[value=' + grab_value + ']').attr('selected', 'selected')
    $('div[value=' + grab_value + ']').addClass('SelectedDiv')
  });

  $('.WebPartPosTable .DragList div').dblclick(function() {
    $('.EditWebPart').click()
    return false
  });

  bindDrag();

} 
function dragInit2() {

  $('.DragList div').click(function() {
    //alert($(this).attr('value'))
    $('.DragList div').removeClass('SelectedDiv')
    $(this).addClass('SelectedDiv')
    var look_for = $(this).parent().find('div').index(this)
    $(this).closest('td').find('option').eq(look_for).attr('selected', 'selected')
    //alert($(this).closest('td').find('option:selected').html())

    var grab_value = $(this).attr('value')
    $('option[value=' + grab_value + ']').attr('selected', 'selected')
    $('div[value=' + grab_value + ']').addClass('SelectedDiv')
  });

  $('.DragList div').dblclick(function() {
    $('#filter_text_id').removeAttr('value')
    filterFunction('DropInit1', '')
    var grab_value = $(this).attr('value')
    $('option[value=' + grab_value + ']').attr('selected', 'selected')
    $('div[value=' + grab_value + ']').addClass('SelectedDiv')
    //popDrag('DropInit1', 'DropList1')
    $('.EditWebPart').click()

    return false
  });

  bindDrag();

}

function bindDrag() {
  /**** DRAG AND DROP FUNCTION ****/

  var drag_contents = 0;
  var drag_value = 0;
  
  $(function() {
    $(".drag")
		.bind("dragstart", function(event) { /**** Clicked item to drag ****/
		  $(this).closest('td').find('option').eq($(this).parent().find('div').index(this)).addClass('RemoveOption')
		  var $drag = $(this), $proxy = $drag.clone();
		  $drag.addClass("outline");
		  $drag.css("opacity", "0.5");
		  return $proxy.appendTo(document.body).addClass("ghost");
		})

		.bind("drag", function(event) { /**** Dragging item ****/
		  $('.SelectedDiv').removeClass('SelectedDiv')
		  $(event.dragProxy).css({
		    position: "absolute",
		    cursor: "move",
		    zIndex: "1000",
		    left: event.offsetX,
		    top: event.offsetY,
		    opacity: "0.5"
		  });
		  drag_contents = $(this).html()
		  drag_value = $(this).attr('value')
		})
		.bind("dragend", function(event) { /**** Let go of mouse button ****/
		  $(event.dragProxy).fadeOut("normal", function() {

		    $(this).remove();
      
		  });
		  $(this).removeClass("outline");
		  $(this).css("opacity", "1");
		  if (drag_contents != 0 && $(this).closest('td').find('select option.RemoveOption').length == 0) {
		    $(this).closest('td').find('select').html($(this).closest('td').find('select').html() + '<option value=' + drag_value + '>' + drag_contents + '</option>')
		  }

		});
    $('.drop') /**** Hover over drop area ****/
		.bind("dropstart", function(event) {
		  if (this == event.dragTarget.parentNode) return false;
		  $(this).addClass("active");
		})
		.bind("drop", function(event) { /**** If item is dropped into drop area ****/
		  $(this).append(event.dragTarget);
		  $('.RemoveOption').remove()
		})
		.bind("dropend", function(event) { /**** Move away from drop area ****/
		  $(this).removeClass("active");
		});

  });
}


$(document).ready(function() {
  var add_this = "this, '', '[URL]', '[TITLE]'"
  $('.AddThisButton').html('<a href="http://www.addthis.com/bookmark.php?v=250" onclick="return addthis_sendto()" onmouseout="addthis_close()" onmouseover="return addthis_open(' + add_this + ')"><img alt="Bookmark and Share" height="16" src="http://s7.addthis.com/static/btn/lg-share-en.gif" style="border:0" width="125" /></a><script src="http://s7.addthis.com/js/250/addthis_widget.js?pub=workplacesystems" type="text/javascript"><![CDATA[]]></script>')

  $(".PublicZoneWidthInner").width($(".PublicZoneWidthInner").closest("body").find("#xStdWidth span").html());

  $(".SubMenu li").find("div").each(function() {
    $(this).parent().addClass("HasChildren")
  })


  $(".LinkHolder").mouseenter(
    function() { //onmouseover function
      if ($(this).find(".SubMenu").is(":visible")) {

      }
      else {
        $(this).parent().find(".SubMenu").slideUp(200);
        $(this).find(".SubMenu").slideDown(200);
      }
    });

  $(".LinkHolder").mouseleave(function() {

    $(this).parent().find(".SubMenu").slideUp(100);

  });

  $(".HeaderLinks ul li").hover(
      function() {

        $(this).find("ul").slideDown(100)

      },
      function() {

        $(this).find("ul").slideUp(100)
      }
    );


  $(".WebPartListBox .DragList").click(function() {

    if ($(this).parent().parent().find(".GlowSpan").is(":hidden")) {
      $(this).parent().parent().parent().parent().find(".GlowSpan").fadeOut(300)
      $(this).parent().parent().find(".GlowSpan").fadeIn(400)
    }
    if ($(this).parent().find('select').is('.ListBoxHeader')) { change_dropdown = 0 }
    else if ($(this).parent().find('select').is('.ListBoxLeft')) { change_dropdown = 1 }
    else if ($(this).parent().find('select').is('.ListBoxMiddle')) { change_dropdown = 2 }
    else if ($(this).parent().find('select').is('.ListBoxRight')) { change_dropdown = 3 }
    else { change_dropdown = 4 }

    $('.PositionDropDown').find('option').eq(change_dropdown).attr('selected', 'selected')


  });

  $('.WebPartTools .WebUp').click(function() {
    var active_option = $(this).closest('.WebPartListBox').find('select option:selected')
    var inactive_option = $(this).closest('.WebPartListBox').find('select option:selected').prev('option')
    var active_div = $(this).closest('td').find('.SelectedDiv')
    var inactive_div = $(this).closest('td').find('.SelectedDiv').prev('div')

    //alert($(active_div).html())
    //alert($(inactive_div).html())

    if ($(this).closest('.WebPartListBox').find('select option').index($(this).closest('.WebPartListBox').find('select option:selected')) == 0) {


    }
    else {
      $(active_option).addClass('RemoveThis')
      $(active_option).clone().insertBefore(inactive_option)
      $('.RemoveThis').eq(1).remove()
      $('.RemoveThis').attr('selected', 'selected')
      $('.RemoveThis').removeClass()

      $(active_div).addClass('RemoveDiv')
      $('.RemoveDiv').clone(true).insertBefore(inactive_div)
      $('.RemoveDiv').eq(1).remove()
      $('.RemoveDiv').addClass('.SelectedDiv')
      $('.RemoveDiv').removeClass('RemoveDiv')

    }
  });
  $('.WebPartTools .WebDown').click(function() {
    var active_option = $(this).closest('.WebPartListBox').find('select option:selected')
    var inactive_option = $(this).closest('.WebPartListBox').find('select option:selected').next('option')
    var active_div = $(this).closest('td').find('.SelectedDiv')
    var inactive_div = $(this).closest('td').find('.SelectedDiv').next('div')

    if ($(this).closest('.WebPartListBox').find('select option').index($(this).closest('.WebPartListBox').find('select option:selected')) == ($(this).closest('.WebPartListBox').find('select option').length) - 1) {


    }
    else {
      $(active_option).addClass('RemoveThis')
      $(active_option).clone().insertAfter(inactive_option)
      $('.RemoveThis').eq(0).remove()
      $('.RemoveThis').attr('selected', 'selected')
      $('.RemoveThis').removeClass()

      $(active_div).addClass('RemoveDiv')
      $(active_div).clone(true).insertAfter(inactive_div)
      $('.RemoveDiv').eq(0).remove()
      $('.RemoveDiv').addClass('.SelectedDiv')
      $('.RemoveDiv').removeClass('RemoveDiv')
    }
  });
  $('.WebPartTools .WebRemove').click(function() {
    var active_option = $(this).closest('.WebPartListBox').find('select option:selected')
    var active_div = $(this).closest('td').find('.SelectedDiv')

    $(active_option).remove()
    $(active_div).remove()
  });

  $(".DotMe").find("div").addClass("DotLine");
  $(".DotMe").find("div:last").removeClass("DotLine");


  //      $('.InitList option').each(function() {
  //        var this_value = $(this).attr('value');
  //        var this_draglist = $(this).closest('.InitList').find('.DragList');
  //        $(this_draglist).html($(this_draglist).html() + '<div value=' + this_value + ' class="drag">' + $(this).html() + '</div>')
  //        //this_draglist.innerHTML = this_draglist.innerHTML + '<div value=' + this_value + ' class="drag">' + $(this).html() + '</div>')
  //      });








});                                           //ready 

///////Filter functions start///////////
//function invoked onload at body tag; or just before closiong body tag
function initArrays() {
  select_objects = document.getElementsByTagName('select'); //must be global
  all_selects = new Array();  //must be global, 3 dimensional array [ids of all selects][counter of each option in given select]['value' or 'text']= value or text of given option in given select
  options_selected = new Array(); //must be global, 2 dimensional array [ids of selects on which onchange or onkeydown function was invoked][values of selected options for associative addressing]=true ,but this boolean value is not used, existence of array entry is tested.
   
  for(var i=0; i<select_objects.length; i++) {
    var index = select_objects[i].id;  // id of select tag
    all_selects[index]= new Array();
    options_selected[index]= new Array();
    for (var j=0; j<select_objects[i].length; j++) {  //number of options elements in given select tag
      all_selects[index][j]= new Array();
      all_selects[index][j]['value'] = select_objects[i].options[j].value;
      all_selects[index][j]['text']  = select_objects[i].options[j].text;
    }//for j
  }//for i
}

//function invoked onkeyup of input tag containing search phrase
function filterFunction(container_id, filter) {
  //container_id - id of a div containing related ddl
  //filter - reference to input tag containing filter phrase or "" for bringing back all options into select
  //sel - reference to related dropdownlist (select)
  var container_ref = document.getElementById(container_id);
  var sel = container_ref.getElementsByTagName("select")[0];
  if (sel) { 
      var select_id = sel.id;
  } else {
      return; //if sendForm gives unpredictable id of other listboxes that don't use filter
  }

  if (options_selected[select_id].length == 0) 
     selectFunction(document.getElementById(select_id)); //remember already selected options, not only these selected manualy
  sel.options.length=0; 
  var k=0;
  for (var i=0; i<all_selects[select_id].length; i++) {
    var t = all_selects[select_id][i]['text'];
    var t_for_search = t.toLowerCase();
    var v = all_selects[select_id][i]['value'];
    var filter_text = (typeof filter == "object") ? filter.value.toLowerCase() : '';
    if (t_for_search.indexOf(filter_text) >= 0)  // == to search from the begining of the listbox item
       sel.options[k++] = new Option(t, v, false, (typeof options_selected[select_id][v] != "undefined"));
  }
}

//function invoked onchange at select tag
function selectFunction(this_select) {
  var number_of_options = this_select.options.length;  //in given select
  for (var i=0; i<number_of_options; i++) {
    if (this_select.options[i].selected == true) {
      options_selected[this_select.id][this_select.options[i].value]=true;
    } else {
      if (typeof options_selected[this_select.id][this_select.options[i].value] != "undefined") 
          delete options_selected[this_select.id][this_select.options[i].value];
    }
  }   
}

///////Filter functions end///////////

function clickSearch(evt) {
  var search_button_ref = document.getElementById('ForJSSearchPanel').getElementsByTagName('a')[0];
  var keyCode = evt.keyCode;
  
  if(keyCode == 13) {
    search_button_href_function = search_button_ref.href.substring(11, search_button_ref.href.length);
    eval(search_button_href_function);
    return false;
  }
  return true;
}


function initCalculator() {
  var entry_length = $('.FormTable:eq(0) .Col1-2').length
  var entry_empty = ''
  for (var i = 0; i < entry_length; i++) {
    //alert($('.FormTable:eq(0) .Col1-2:eq(' + i + ')').parent().next('tr').html())
    if ($('.FormTable:eq(0) .Col1-2:eq(' + i + ')').html() == '' || $('.FormTable:eq(0) .Col1-2:eq(' + i + ') span:eq(0)').html() == '') {
      $('.FormTable:eq(0) .Col1-2:eq(' + i + ')').parent().css('display', 'none')
      $('.FormTable:eq(0) .Col1-2:eq(' + i + ')').parent().prev('tr').css('display', 'none').prev('tr').css('display', 'none').prev('tr').css('display', 'none')
      entry_empty = entry_empty + '1';
    }
    else {
      $('.CalcDisplayNone').css('display', 'block')
      entry_empty = entry_empty + '0';
    }
  }
  if (entry_empty.indexOf('0') == -1) {
    $('.CalcHide').css('display','none')
  }
}
