function printpdf() {
	var x = document.getElementById("pdf");
	x.click();
	x.setActive();
	x.focus();
	x.print();
}
function change_div1(game_frame_bg,content)
{
  document.getElementById(game_frame_bg).style.backgroundImage = 'url(/metoyou/images/game_example1.gif)';
}
function change_div2(game_frame_bg,content)
{
  document.getElementById(game_frame_bg).style.backgroundImage = 'url(/metoyou/images/game_example2.gif)';
}

function kill_div(game_frame_bg)
{
  document.getElementById(game_frame_bg).style.background = 'none ' // or whatever default you want
}


/*
 * NoPostie javascript below
 */

window.no_recipients = 0;
window.no_contributors = 0;

var max_people_per_group = 50; // Was 25 until 2010-01-04. Was 10 until 2009-12-04
var DEFAULT_NAME = 'Name: ';
var DEFAULT_EMAIL = 'Email: ';
/**
 * 
 * @param {Object} contact_id
 * @param {Object} destination_id
 * @param {Object} type                             ['recipient','contributor'] Flag indicating what type of field to add.
 */
function addContactToMessage(contact_id, destination_id,type) {
    if (type == 'contributor') {
    newContributorLine(destination_id,$('#name_' + contact_id).text(),$('#email_' + contact_id).text());
  }else if(type=='recipient') {
    newRecipientLine(destination_id, $('#name_' + contact_id).text(), $('#email_' + contact_id).text());
  }else{
        alert('Unknown contact type: '+type);
    }
  return false;
}

//  TODO    newRecipientLine and newContributorLine duplicate allot of code 
function newRecipientLine(destination_id, name, email){
    
  // Check if the number of recipients matches the number of recipient fields
  // Used when editing a card where multiple recipients may already be in place
  // If not same, update no_recipients with the correct value
  if (document.getElementById('add_recipient').getElementsByTagName('tr').length != window.no_recipients)
  {
     no_recipients = document.getElementById('add_recipient').getElementsByTagName('tr').length;
  }
          
  if(window.no_recipients < max_people_per_group) // Do not let user add more than XX recipients
  {
    var tr = addRowTo(destination_id);
    var td1 = addCellTo(tr);
    addFieldTo(td1, 'f_names[]', (name || DEFAULT_NAME),false,'recipient', DEFAULT_NAME);   
    addFieldTo(td1, 'f_emails[]', (email || DEFAULT_EMAIL), true, '', DEFAULT_EMAIL);
    var deleteButton = document.createElement('a');
    deleteButton.href='#';
    deleteButton.id='remove';
    deleteButton.title='Remove this recipient';
    td1.appendChild(deleteButton);
    deleteButton.onclick = removeMyLine;
    var deleteButtonSpan = document.createElement('span');
    deleteButton.appendChild(deleteButtonSpan);
    addTextTo(deleteButtonSpan, '[-]');
    window.no_recipients++;
  }
  
  if(window.no_recipients == max_people_per_group)
  {
    $('#add_recipient_link').slideUp(200);
  }
  return false;
}

function newContributorLine(destination_id,name,email){
    
  // As for recipients, check value for contributors
  if (document.getElementById('add_contributor').getElementsByTagName('tr').length != window.no_contributors){
    no_contributors = document.getElementById('add_contributor').getElementsByTagName('tr').length;
  }
    // Do not add more than XX contributors         
  if(window.no_contributors < max_people_per_group) {
    var tr = addRowTo(destination_id); 
    var td1 = addCellTo(tr);
    addFieldTo(td1, 'c_names[]', (name || DEFAULT_NAME),false,'contributor', DEFAULT_NAME); 
    addFieldTo(td1, 'c_emails[]', (email || DEFAULT_EMAIL), true, '', DEFAULT_EMAIL);
    var deleteButton = document.createElement('a');
    deleteButton.href='#';
    deleteButton.id='remove';
    deleteButton.title='Remove this contributor';
    td1.appendChild(deleteButton);
    deleteButton.onclick = removeMyLine2;
    var deleteButtonSpan = document.createElement('span');
    deleteButton.appendChild(deleteButtonSpan);
    addTextTo(deleteButtonSpan, '[-]');
    window.no_contributors++;
  }
  
  if(window.no_contributors == max_people_per_group){
    $('#add_contributor_link').slideUp(200);
  }
  return false;
}
/**
 * 
 * @param {Object} my_element
 */
function removeMyLine(my_element){
  this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
  
    removeRecipient();
  
    //  Update the recipients address book
    updateContactsBook('recipient');
    
  return false;
}
/**
 * 
 */
function removeRecipient(){
  if(window.no_recipients == max_people_per_group){
    $('#add_recipient_link').slideDown(200);
  } 
  no_recipients--; // Decrement number of recipients    
}
/**
 * 
 * @param {Object} my_element
 */
function removeContributor(){
  if(window.no_contributors == max_people_per_group){
    $('#add_contributor_link').slideDown(200);
  }
  no_contributors--; // Decrement number of contributors    
}
/**
 *  Removes the row from the contributor list
 * 
 * @param {Object} my_element
 */
function removeMyLine2(my_element){
  this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
    removeContributor();
    updateContactsBook('contributor');
  return false;
}

function addRowTo(destination_id)
{
  var destination_obj = document.getElementById(destination_id);
  if(destination_obj == undefined)
  {
    alert('Issue finding ' + destination_id);
    return false;
  }
  
  var new_input = document.createElement('tr');
  
  destination_obj.appendChild(new_input);
  return new_input;
}

function addTextTo(destination_obj, value)
{
  destination_obj.appendChild(document.createTextNode(value));
}
function addCellTo(destination_obj) {
   var new_cell = document.createElement('td');
   destination_obj.appendChild(new_cell);
   return new_cell;
}

function addFieldTo(destination_obj, input_name, input_value, is_email,type, input_label){
    
    //  Flag to indicate what type of field is this
    //type = type | 'recipient';
    if(type==undefined)type = 'recipient';
    
  // Generate and ID
  var id = randomString(10);
  
  // And make sure it's unique
  while(document.getElementById(id) != undefined){
    id = randomString(10);
  }
  
  // Create the div
  var new_div = document.createElement('div');
  new_div.className = 'personalise_form_inputs';
  destination_obj.appendChild(new_div);
  
  // Create the label

  var new_label = document.createElement('label');
  new_label.id = 'l' + id;
  new_label.setAttribute('for', id);
  new_label.appendChild(document.createTextNode(input_label));
  new_div.appendChild(new_label);
  
//    alert('Created label '+input_name+' '+input_value);
  
  // Create the input field
  var new_input = document.createElement('input');
  new_input.id = id;
  new_input.type = 'text';
  if (input_value == DEFAULT_NAME || input_value == DEFAULT_EMAIL) {
    new_input.value = '';
  } else {
    new_input.value = input_value;
  } 
  new_input.name = input_name;
  new_input.onfocus = clearDefaultValue;
  new_input.onblur = resetToDefault;
  new_input.className = 'required ie_input';
    if (is_email == true) {
        new_input.className = 'required email ie_input';
  }
  new_div.appendChild(new_input);
  
  //    Create the contacts link
  //if ((!is_email)&&(loggedIn)) {
  //  switch(type){
  //      case 'recipient':
  //        $(new_div).append("<a class='add_recipient_contact_icon' href='#'><span>Add from My Contacts</span></a>");
  //          $('.add_recipient_contact_icon').click(openRecipientContactList);
  //          $('.add_recipient_contact_link').click(openRecipientContactList);           
  //          break;
  //      case 'contributor':
  //          $(new_div).append("<a class='add_contributor_contact_icon' href='#'><span>Add from My Contacts</span></a>");
  //          $('.add_contributor_contact_link').click(openContributorContactList);
  //          $('.add_contributor_contact_icon').click(openContributorContactList);  
  //          break;
  //      default:
  //          alert('-------- unknown field type: '+type);
  //          break;
  //      }
  //}

  //$(new_label).labelOver('over-apply');
  
  if(is_email == true){
        /*
    new_input.className = 'required email ie_input';
    $(new_input).rules("add", { email: true });
        */
        //alert($(new_input).length);
        $(new_input).rules("add", { email: true });
  }else{
        //alert($(new_input).rules);
        $(new_input).rules("add", { required: true });
  }
  
  return new_div;
}

function randomString(length)
{
  var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'.split('');
  
  if (! length)
  {
    length = Math.floor(Math.random() * chars.length);
  }
  
  var str = '';
  for (var i = 0; i < length; i++)
  {
    str += chars[Math.floor(Math.random() * chars.length)];
  }
  return str;
}

function clearDefaultValue()
{
  if (this.value == this.defaultValue)
  {
    this.value = '';
  }
}

function resetToDefault()
{
  if (this.value == '')
  {
    this.value = this.defaultValue;
  }
}

/**
 * Check if any fields are empty
 */
function hasCompletedField(){
    var fieldContainer = $('#add_contributor input');
    for(var i=0;i<fieldContainer.length;i++){
        if(fieldContainer[i].value!=''){
            return true;
      }
    }
    return false;
}     

/* Call the form */

function callForm() {
    $('#personalise_nopostie_form').submit(); 
}


