﻿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) && (safari.indexOf("chrome") == -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 older than 7.0
var msie_80  = false; // MSIE 8.0 or newer
var msie_70  = false; // MSIE 7.0
var msie_60  = false; // MSIE 6.0
var msie_55  = false; // 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_old) {
  document.writeln('.Radio, .CheckBox {top:2px;}\n');
  document.writeln('.Radio2 {position:relative; top:2px;}\n');
  document.writeln('.MainDivAdmin .Content .CentreColumn {height:400px;}\n');
  document.writeln('.MainDivPublic .Content {height:300px;}\n');
  document.writeln('.AccessCBList input {margin-right:0px; position:relative; left:-4px;}\n');
  document.writeln('.AccessCBList label {position:relative; top:-2px; left:-2px;}\n');
  
  document.writeln('.ProductList {background-image:none; border:solid 1px #8BA1A8;} \n');
  document.writeln('.BasketTable {background-image:none;} \n');
  document.writeln('.FormBoxHighlighted {background-image:none;} \n');
  document.writeln('.LoginFormVersion .FormBoxHighlighted {background-image:none;} \n');
  document.writeln('.ViewTable {background-image:none;}\n');
  document.writeln('.LayoutBoxStd {background-image:none;}\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 (msie_70) {
  document.writeln('.Radio, .CheckBox {margin:4px; margin-right:0px; 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 input {margin-right:0px; position:relative; left:-4px;}\n');
  document.writeln('.AccessCBList label {position:relative; top:-2px; left:-2px;}\n');
}

if (msie_80) {
  document.writeln('.Radio, .CheckBox {margin-left:4px; margin-right:2px; top:3px;}\n');
  document.writeln('.Radio2 {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:3px;}\n');
  document.writeln('.Radio2 {position:relative; top:3px;}\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 label {position:relative; top:-2px;}\n');
  document.writeln('.FormTable .FileUpload {width:auto; float:right; margin-right: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('.Radio, .CheckBox {top:3px;}\n');
  document.writeln('.Radio2 {position:relative; top:3px;}\n');
  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('.AccessCBList label {position:relative; top:-2px;}\n');
}
document.writeln('/*]]>*/\n</style>\n');

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;
}

//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 selectAllAccessItem(button_ref) {
  $(button_ref).parent().parent().find("input").attr("checked", "checked");
  return false;
}

function deselectAllAccessItem(button_ref) {
  $(button_ref).parent().parent().find("input").removeAttr("checked");
  return false;
}

function selectAllAccessGlobal() {
  $("#ForJSAllCheckboxesInside").find("input").attr("checked", "checked");
  return false;
}

function deselectAllAccessGlobal() {
  $("#ForJSAllCheckboxesInside").find("input").removeAttr("checked");
  return false;
}

//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);
  }
}

function popupOnClick(url) {
  var new_window = window.open(url, "NewWin1", "toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no,copyhistory=no,width=650,height=650,top=100,left=200");
  new_window.focus();
  return false;
}

function openWysiwyg(html_source_id, html_destination_id, editor_width, editor_height, popup_left, popup_top, style_filename, content_type) {
  //content_type = "email" for emails and "page" for page contents editing
  editor_width = parseInt(editor_width) + 18; //18px for editor scrollbars
  var popup_width = parseInt(editor_width) + 20;
  var popup_height = parseInt(editor_height) + 50;

  var dynamic_url = "http://" + document.domain;
  if (document.URL.indexOf(":81/") > -1) dynamic_url += ":81";
  if (document.URL.indexOf("localhost") > -1) dynamic_url = "http://eightsquare.obs-group.co.uk:81"

  var email_prefix = (content_type == "email") ? "email-" : "";

  var ww = window.open("", "EditorWindow", "resizable=no,width=" + popup_width + ",height= " + popup_height + ",top=" + popup_top + ",left= " + popup_left);

  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>WYSIWYG 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/' + email_prefix + 'imagelibrary.aspx" />');
  ww.document.writeln('<param name="AttachmentLibraryURL" value="' + dynamic_url + '/xstandard/' + email_prefix + 'attachmentlibrary.aspx" />');
  ww.document.writeln('<param name="LinkLibraryURL"       value="' + dynamic_url + '/xstandard/linklibrary.aspx" />');
  //ww.document.writeln('<param name="SpellCheckerURL"      value="' + dynamic_url + '/xstandard/spellchecker.aspx" />'); //http://soap.xstandard.com/spellchecker.aspx for test
  ww.document.writeln('<param name="Styles"               value="' + dynamic_url + '/xstandard/' + email_prefix + '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/' + email_prefix + style_filename + '" />');

 //ww.document.writeln('<param name="SpellCheckerLangFilter" value="en-gb, en-us" />');
  ww.document.writeln('<param name="SpellCheckerLang" value="en-us" />');
  ww.document.writeln('<param name="ProxySetting" value="direct" />'); //if default proxy settings stop 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="Debug" value="no" />');
  ww.document.writeln('<param name="Options" value="2128" />');  //hide line numbers, make images decorative by default, make divs don't disappear durind edition.
  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, separator, image, hyperlink, attachment, source, preview, help" />'); // spellchecker,
  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="yes" />'); //styles ddlist in the editor
  ww.document.writeln('<param name="Base" value="' + dynamic_url + '/cms-uploaded/" />'); //URL used to resolve relative URLs for images defined in markup.
  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 openWysiwygForImages(html_source_id, html_destination_id, editor_width, editor_height, popup_left, popup_top, style_filename, content_type) {
  editor_width = parseInt(editor_width) + 18; //18px for editor scrollbars
  var popup_width = parseInt(editor_width) + 20;
  var popup_height = parseInt(editor_height) + 50;

  var dynamic_url = "http://" + document.domain;
  if (document.URL.indexOf(":81/") > -1) dynamic_url += ":81";
  if (document.URL.indexOf("localhost") > -1) dynamic_url = "http://EightSquare.obs-group.co.uk:81"

  var email_prefix = (content_type == "email") ? "email-" : "";

  var ww = window.open("", "EditorWindow", "resizable=no,width=" + popup_width + ",height= " + popup_height + ",top=" + popup_top + ",left= " + popup_left);

  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>WYSIWYG 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/' + email_prefix + 'imagelibrary.aspx" />');
  ww.document.writeln('<param name="AttachmentLibraryURL" value="' + dynamic_url + '/xstandard/' + email_prefix + 'attachmentlibrary.aspx" />');
  ww.document.writeln('<param name="LinkLibraryURL"       value="' + dynamic_url + '/xstandard/linklibrary.aspx" />');
  ww.document.writeln('<param name="Styles"               value="' + dynamic_url + '/xstandard/' + email_prefix + '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/' + email_prefix + style_filename + '" />');

  ww.document.writeln('<param name="ProxySetting" value="direct" />'); //if default proxy settings stop 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="Debug" value="no" />');
  ww.document.writeln('<param name="Options" value="2128" />'); //hide line numbers, make images decorative by default, make divs don't disappear durind edition.
  ww.document.writeln('<param name="ToolbarWysiwyg" value="image, attachment" />'); // spellchecker,
  ww.document.writeln('<param name="ToolbarPreview" value="" />');
  ww.document.writeln('<param name="ToolbarSource" value="" />');
  ww.document.writeln('<param name="ShowStyles" value="no" />'); //styles ddlist in the editor
  ww.document.writeln('<param name="Base" value="' + dynamic_url + '/cms-uploaded/" />'); //URL used to resolve relative URLs for images defined in markup.
  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.close();" title="Close">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;
}

//show single element with given id
function show(id_show, type) {
  switch (type) {
    case 'fade':
      $('#' + id_show).fadeIn(400);
      break;
    case 'slide':
      $('#' + id_show).slideDown(400);
      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(200);
      break;
    case 'slide':
      $('#' + id_hide).slideUp(200);
      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"], slide);
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 toCookie(cookie_id, value) {
  var exp = new Date();
  exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 365));
  var cookie_name = "ES_" + cookie_id + "=" + value;
  var cookie_expires = "expires=" + exp.toGMTString();
  document.cookie = cookie_name + ";" + cookie_expires + "; path=/"; 
}

//Returns value stored in cookie with given id, or -1 if cookie didn't exist (or is expired), multi value cookies safe.
function fromCookie(cookie_id) {
  var labelName="ES_" + cookie_id;
  var labelLen = labelName.length;
  var cookieData = document.cookie;
  var cLen = cookieData.length;
  var i = 0;
  var cEnd;
  while (i < cLen) {
    var j = i + labelLen;
    if (cookieData.substring(i,j) == labelName) {
      cEnd = cookieData.indexOf(";",j);
      if (cEnd == -1) {
        cEnd = cookieData.length;
      }
      return unescape(cookieData.substring(j+1, cEnd));
    }
  i++;
  }
  return "-1";
}

function swapLargeImage(img_index) {
  if (img_index != 0) $('#ProductLargeImages img').eq(0).stop(true, true).fadeOut(250);
  if (img_index != 1) $('#ProductLargeImages img').eq(1).stop(true, true).fadeOut(250);
  if (img_index != 2) $('#ProductLargeImages img').eq(2).stop(true, true).fadeOut(250);
  $('#ProductLargeImages img').eq(img_index).fadeIn(250);
}


$(document).ready(function() {

   //New forms functionality
   //Highlight on focus - selects, textareas, textboxes
   $("select.WithFocusHighlight, textarea.WithFocusHighlight, input.WithFocusHighlight").focus(function() {
     $(this).closest(".FormBox").addClass("FormBoxHighlighted");
   });
   $("select.WithFocusHighlight, textarea.WithFocusHighlight, input.WithFocusHighlight").blur(function() {
     $(this).closest(".FormBox").removeClass("FormBoxHighlighted");
   });

   //Highlight on focus - single checkbox/radiobutton
   $("span.WithFocusHighlight > input").focus(function() {
     $(this).closest(".FormBox").addClass("FormBoxHighlighted");
   });
   $("span.WithFocusHighlight > input").click(function() { //added onclick because of safari
     $(this).focus();
   });
   $("span.WithFocusHighlight > input").blur(function() {
     $(this).closest(".FormBox").removeClass("FormBoxHighlighted");
   });

   //Highlight on focus - radiobutton/checkbox lists
   $("table.WithFocusHighlight").find("input").focus(function() {
     $(this).closest(".FormBox").addClass("FormBoxHighlighted");
   });
   $("table.WithFocusHighlight").find("input").click(function() { //added onclick because of safari
     $(this).focus();
   });
   $("table.WithFocusHighlight").find("input").blur(function() {
     $(this).closest(".FormBox").removeClass("FormBoxHighlighted");
   });

   //validation highlight
   //$(".WithFocusHighlight").closest(".FormBox").find(".ValidationInfo").parent().addClass("FormBoxValidationHighlighted");
   $(".WithFocusHighlight").closest(".FormBox").find(".ValidationInfo").each(function() {
     if ($(this).closest(".InnerFormBox").length == 1) {
       $(this).closest(".InnerFormBox").find("input[type!='radio'][type!='checkbox']").addClass("InputFieldValidationHighlighted");
       $(this).closest(".InnerFormBox").find("select").addClass("InputFieldValidationHighlighted");
       $(this).closest(".InnerFormBox").find("textarea").addClass("InputFieldValidationHighlighted");
     } else {
       $(this).closest(".FormBox").find("input[type!='radio'][type!='checkbox']").addClass("InputFieldValidationHighlighted");
       $(this).closest(".FormBox").find("select").addClass("InputFieldValidationHighlighted");
       $(this).closest(".FormBox").find("textarea").addClass("InputFieldValidationHighlighted");
     }
   });
 


var flash_index = 0;
$('div.CMSWindow img[src$=swf]').each(function() {
  var flash_id = 'flid' + flash_index++;
  var flash_width = $(this).attr('width');
  var flash_height = $(this).attr('height');
  var flash_classname = $(this).attr('class');
  var flash_src = $(this).attr('src');
  
  if (isNaN(flash_width) == true) flash_width = "200";
  if (isNaN(flash_height) == true) flash_height = "200";
  
  $(this).attr('id', flash_id);
  $(this).css('visibility','hidden');
  
   function flashCallback () {
     $('#'+flash_id).attr("class",flash_classname);
   }
    
   swfobject.embedSWF(flash_src, flash_id, flash_width, flash_height, "8.0.22", "", {}, {scale:"exactfit"}, {}, flashCallback);
   
 });

}); // ready function




