﻿/* for COrporate public */
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 versions older than 7
var msie_80 = false; // MSIE version 8.0 or newer
var msie_70 = false; // MSIE version 7.0
var msie_60 = false; // only MSIE 6.0

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;
}

document.writeln('<style type="text/css">\n/*<![CDATA[*/\n');
if (msie_80) {
  document.writeln('.Radio {}\n');
  document.writeln('.Radio input {}\n');
  document.writeln('.CheckBox {}\n');
  document.writeln('.CheckBox input {}\n');
  document.writeln('.RbVerticalList {}\n');
  document.writeln('.RbVerticalList input {position:relative; margin-bottom:6px; top:4px}\n');
  document.writeln('.RbVerticalList label {top:1px;}\n');
  document.writeln('.CbVerticalList {left:-6px; top:-2px;}\n');
  document.writeln('.CbVerticalList input {position:relative; top:3px}\n');
}
if (msie_70) {
  document.writeln('.Radio {left:-4px;}\n');
  document.writeln('.Radio input {margin-bottom:0px; margin-right:0px;}\n');
  document.writeln('.CheckBox {left:-4px;}\n');
  document.writeln('.CheckBox input {margin-bottom:0px; margin-right:0px;}\n');
  document.writeln('.RbVerticalList {left:-5px;}\n');
  document.writeln('.RbVerticalList input {position:relative; top:2px}\n');
  document.writeln('.RbVerticalList label {top:1px;}\n');
  document.writeln('.CbVerticalList {left:-6px; top:-2px;}\n');
  document.writeln('.CbVerticalList input {position:relative; top:3px}\n');
  document.writeln('hr {margin-top:0px; margin-bottom:0px;}\n');
}
if (msie_old) {
  document.writeln('.Radio {left:-4px;}\n');
  document.writeln('.Radio input {margin-bottom:0px; margin-right:0px;}\n');
  document.writeln('.CheckBox {left:-4px;}\n');
  document.writeln('.CheckBox input {margin-bottom:0px; margin-right:0px;}\n');
  document.writeln('.CbVerticalList {left:-6px; top:-2px;}\n');
  document.writeln('.RbHorizontalList input {left:-7px; top:3px}\n');
  document.writeln('.RbHorizontalList label {left:-7px;}\n');
  document.writeln('.RbVerticalList {left:-5px;}\n');
  document.writeln('.RbVerticalList label {top:-1px;}\n');
  document.writeln('.RbVerticalList input {}\n');
  document.writeln('.MainDiv .Content .SideMargins .LeftSlot  {height:200px;}\n');
  document.writeln('.MainDiv .Content .SideMargins .RightSlot {height:200px;}\n');
  document.writeln('hr {margin-top:0px; margin-bottom:0px;}\n');
}
if (opera) {
  document.writeln('.Radio {}\n');
  document.writeln('.Radio input {}\n');
  document.writeln('.CheckBox {}\n');
  document.writeln('.CheckBox input {}\n');
  document.writeln('.RbHorizontalList input {}\n');
  document.writeln('.RbHorizontalList label {top:0px;}\n');
  document.writeln('.RbVerticalList {}\n');
  document.writeln('.RbVerticalList input {margin-top:4px; margin-bottom:2px;}\n');
  document.writeln('.CbVerticalList {left:-1px;}\n');
  document.writeln('.CbVerticalList input {margin-top:2px; margin-bottom:3px;}\n');
}
if (firefox) {
  document.writeln('.Radio {}\n');
  document.writeln('.Radio input {}\n');
  document.writeln('.CheckBox {}\n');
  document.writeln('.CheckBox input {}\n');
  document.writeln('.RbHorizontalList label {top:-3px;}\n');
  document.writeln('.RbVerticalList {left:-1px;}\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');
}
if (safari) {
  document.writeln('.Radio {}\n');
  document.writeln('.Radio input {}\n');
  document.writeln('.CheckBox {}\n');
  document.writeln('.CheckBox input {}\n');
  document.writeln('.RbVerticalList input {margin-top:4px; margin-bottom:3px;}\n');
  document.writeln('.RbVerticalList label {top:-3px; 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 {}\n');
  document.writeln('.Radio input {}\n');
  document.writeln('.CheckBox {}\n');
  document.writeln('.CheckBox input {}\n');
  document.writeln('.RbVerticalList input {margin-top:4px; margin-bottom:3px;}\n');
  document.writeln('.RbVerticalList label {top:-2px; margin-left:5px;}\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('.FormField .TextAreaW1 {width:258px;}\n');
}
document.writeln('.ValidationComplianceTag {display:none;}\n');
document.writeln('/*]]>*/\n</style>\n');


//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);
  }
}

//Returns value stored in cookie with given id, or -1 if cookie didn't exist (or is expired), multi value cookies safe.
function toCookie(cookie_id, value) {
  var exp = new Date();
  exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 365));
  var cookie_name = "MarjanCorporate" + 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 resistant.
function fromCookie(cookie_id) {
  var labelName = "MarjanCorporate" + 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 getYear(id) {
  if (document.getElementById(id)) {
    var d = new Date();
    document.getElementById(id).innerHTML = d.getFullYear()
  }
}

//initial example value in a form field; disappears on focus, appears back on blur if is empty
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;
}

// a standard popup window
function openPopup(url) {
  var win_handle = window.open(url, 'PopUpWindow', 'toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width=690,height=640,top=100,left=150');
  win_handle.focus();
}

$(document).ready(function() {
  
  if (msie_70 || msie_old) $("table").attr('cellspacing', '0');

  getYear("CurrentYear");

  //Form fields on focus highlight - selects, textareas, textboxes
  $("select.WithFocusHighlight, textarea.WithFocusHighlight, input.WithFocusHighlight").focus(function() {
    $(this).closest(".FormBox").addClass("FormBoxFocusHighlighted");
  });
  $("select.WithFocusHighlight, textarea.WithFocusHighlight, input.WithFocusHighlight").blur(function() {
    $(this).closest(".FormBox").removeClass("FormBoxFocusHighlighted");
  });

  //Highlight on focus - single checkbox/radiobutton
  $("span.WithFocusHighlight > input").focus(function() {
    $(this).closest(".FormBox").addClass("FormBoxFocusHighlighted");
  });
  $("span.WithFocusHighlight > input").click(function() { //added onclick because of safari
    $(this).focus();
  });
  $("span.WithFocusHighlight > input").blur(function() {
    $(this).closest(".FormBox").removeClass("FormBoxFocusHighlighted");
  });

  //Highlight on focus - radiobutton/checkbox lists
  $("table.WithFocusHighlight").find("input").focus(function() {
    $(this).closest(".FormBox").addClass("FormBoxFocusHighlighted");
  });
  $("table.WithFocusHighlight").find("input").click(function() { //added onclick because of safari
    $(this).focus();
  });
  $("table.WithFocusHighlight").find("input").blur(function() {
    $(this).closest(".FormBox").removeClass("FormBoxFocusHighlighted");
  });

  //form fields validation highlight - parent box
  $(".WithFocusHighlight").closest(".FormBox").find(".ValidationInfo").each(function() {
    $(this).closest(".FormBox").addClass("FormBoxValidationHighlighted");
  });

  //form fields validation highlight - each field except for radio and checkbox
  $(".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");
    }
  });

  //sometimes page background is not completly displayed in ie7 and ie8
  if ((msie_70 || msie_80) && (document.getElementById('JSContent'))) setTimeout("document.getElementById('JSContent').style.backgroundImage = 'url(/Images/bg-content.gif)'", 300);

});   //ready
