/* function makes sure, the language switch on the Product and Service pages keeps you on the right subpage */
function pandsLangSwitch(id) {
  $('#languageswitch li a').each(function() {
    $(this).querystring('subpage='+id);
  });
}


/* used in combination with mediacollection for publications and a form with a special textarea publications presentation */
function switchPublicationOrder(orderid, title, checked) {
  var textarea = $('#publicationorders textarea');
  var inputName = textarea.attr('name');
  var inputId = textarea.attr('id') + '_' + inputName;
  var textareaValue = textarea.val();
  var publicationIdentifier;
  if (orderid) {
    publicationIdentifier = '<!--' + orderid + '-->' + title;
  } else {
    publicationIdentifier = title;
  }
  

  if (checked) {
    // checked
    if (textareaValue.search(/^\s*$/) != -1) {
      textarea.val(publicationIdentifier);
    } else {
      textarea.val(textareaValue + ';\r\n' + publicationIdentifier);
    }
  } else {
    // unchecked
    var orders = String(textareaValue).split(';\n');
    var newtextareaValue = '';
    for (i=0; i < orders.length; i++) {
      if (orders[i] != publicationIdentifier) {
        if (newtextareaValue != '') {
          newtextareaValue += ';\r\n';
        }
        newtextareaValue = newtextareaValue + orders[i];
      }
    }
    textarea.val(newtextareaValue);
  }
}

function setCookie(name, value, expire) {
  if (expire == '') {
    document.cookie = name + '=' + escape(value) + '; path=/';
  } else {
    var expires = new Date();
    expires.setTime(expires.getTime() + expire);

    document.cookie = name + '=' + escape(value) + ((expire == null) ? '' : ('; expires=' + expires.toGMTString())) + '; path=/';
  }
}

function getCookie(name) {
   var search = name + "=";
   var val = "";
   var offset,end;
   
   if(document.cookie.length > 0) { // if there are any cookies
      offset = document.cookie.indexOf(search) 

      if(offset != -1) { // if cookie exists 
         offset += search.length;

         // set index of beginning of value
         end = document.cookie.indexOf(";", offset) 

         // set index of end of cookie value
         if (end == -1) {
            end = document.cookie.length;
         }

         val = unescape(document.cookie.substring(offset, end));
      } 
   }

   return val;
}

function acceptDisclaimer(cname, accept) {
  if (accept != undefined && !accept) {
    history.back();
  } else {
    setCookie(cname, 'true');
    $('#disclaimedwarning').hide();
    $('#disclaimedcontent').show();
  }
}

function setUsaHome(siteselected) {
  var cookiename = 'home_ingcom';
  if (siteselected) {
    var usahomeChoice = $('input:radio[name=siteselect]:checked').val();
    var rememberSetting = $('input:checkbox[name=remember]:checked').val();
    
    if (usahomeChoice == '2') {
      cookiename = 'home_usa';
      alternative_home = 'http://ing.us/';
    } else if (usahomeChoice == '3') {
      cookiename = 'home_usadirect';
      alternative_home = 'http://home.ingdirect.com/index.html';
    }

    if (rememberSetting == 'on') {
      setCookie(cookiename, cookiename, 1000*60*60*24*365*10);  // 10 years minus 3 days for leap years
      removeOtherCookies(cookiename);
    }
    
  } else {
    setCookie(cookiename, cookiename, 1000*60*60*24*365*10);
    removeOtherCookies(cookiename);
  }
  
  if (cookiename == 'home_ingcom') {
    $('#splash').hide();
  } else {
    document.location = alternative_home;
  }
}

function removeOtherCookies(cookiename) {
  if (cookiename != 'home_usa') {
    setCookie('home_usa', 'home_usa', -1000*60*60*24*365*10);
  }
  if (cookiename != 'home_usadirect') {
    setCookie('home_usadirect', 'home_usadirect', -1000*60*60*24*365*10);
  }
}

