/**** Start moved from content.js ***/
window.onload = function(){
  localOnload();
  if((typeof initCalendarOnload != "undefined")&&(initCalendarOnload)) {
		initCalendarAndClock();
	}
	if((typeof initTheme != "undefined")&&(initTheme)){
		initTheme();
	}
}

/*
  localOnload  - This function should be overloaded on the jsp,
  to perform the onload actions/methods specific to that jsp
*/
function localOnload(){}

function selectCheckBox( formName, formField ){
	document.forms[formName][formField].checked = true;
}

function clearCheckBox(cb){
	cb.checked = false;
}

function clearLoop( formName, fieldName, value, isAll ){
	var cbForm = document[ formName ];
	var cbArray = cbForm.elements.length;
	for (var i = 0; i < cbArray; i++) {
		var cb = cbForm.elements[i];
		if (cb.name == fieldName && cb.value != value && isAll ) {
			clearCheckBox(cb);
		}
		if( cb.name == fieldName && cb.value == value && !isAll){
			clearCheckBox(cb);
		}
	}
}

function clearAll( object ){
	clearLoop( document.forms[0].name, object.name, object.value, true );
}

function clearSingle( object, clearVal ){
	clearLoop( document.forms[0].name, object.name, clearVal, false );
}

function clearCheck( object , clearVal ){
	if( object.value == clearVal ){
		clearAll( object );
	} else {
		clearSingle( object , clearVal );
	}
}

function emptyCompany(){
	document.speakerForm.companyName.value='';
}
function verifyRemove(){
	if (confirm('Do you want to remove this proposed speaker?')){
		return true;
	}
	return false;
}
function nextPage(inURL,speakers){
	var numSpeakers = speakers;
	if( numSpeakers > 0 ){
		goTo(inURL);
	} else {
		alert('At least one speaker is required for each session.');
	}
}
function goTo(inURL) {
	var URL;
	if(navigator.appName != "Microsoft Internet Explorer"){
		URL = 'cfp/'+inURL;
	} else {
		URL = inURL;
	}
	window.location = URL;
}


function clearList(list){ // used in generalInfoContent.jsp
	for (i=0; i < list.length; i++){
		list.options[i] = null;
	}
}
function populateLengths(){ // used in generalInfoContent.jsp
	var sessionLengthMessageElement = document.getElementById("sessionLengthMessage");
	sessionLengthMessageElement.className = "hide";
	var frm = document.reqSessionForm;
	for (i=1;i<frm.sessionLengthID.length;i++){
		frm.sessionLengthID.options[i] = null;
	}
	if (frm.sessionTypeID.selectedIndex == 0){
		return;
	}
	var ses_type_value = frm.sessionTypeID.options[frm.sessionTypeID.selectedIndex].value;
	var lengthArray = eval('lengthArray'+ses_type_value);
	var lengthArrayValues = eval('lengthValueArray'+ses_type_value);
	if(lengthArray.length > 0){
		for (i=0;i<lengthArray.length;i++){
			var option = new Option(lengthArray[i], lengthArrayValues[i]);
			frm.sessionLengthID.options[i]=option;
		}
		frm.sessionLengthID.options[0].selected = true;
	} else {
		var sessionLengthElement = document.getElementById("sessionLength");
		sessionLengthElement.options.length = 1;
		var option = new Option("0", "");
		sessionLengthElement.options[0] = option;
		var sessionLengthMessageElement = document.getElementById("sessionLengthMessage");
		sessionLengthMessageElement.className = "fontError";
		sessionLengthMessageElement.innerHTML = "ERROR: session lengths are not configured.";
	}
}
function validateState(countryValue){
	var stateReqTdElement = document.getElementById("stateReqTd");
	var stateIDElement = document.getElementById("stateID");
	if(countryValue == "CA" || countryValue == "US"){
		stateReqTdElement.innerHTML = "*";
		if(stateIDElement.value == ""){
			stateIDElement.focus();
		}
	} else {
		stateReqTdElement.innerHTML = "";
		stateIDElement.options[0].selected = true;
	}
}
function validateAreaCode(countryValue){
/*
	var reqTdElement = document.getElementById("areaCodeReqTd");
  if (countryValue == "CA" || countryValue == "US") {
		reqTdElement.innerHTML = "*";
  } else {
		reqTdElement.innerHTML = "";
	}
*/
}
function defaultCountryCode(countryValue){
  var countryCodeEl = document.getElementById("countryCode");
  if (countryCodeEl) {
    if (countryValue == "CA" || countryValue == "US") {
      countryCodeEl.value = "1";
      countryCodeEl.readOnly = true;
    } else if (countryCodeEl.readOnly) {
      countryCodeEl.value = "";
      countryCodeEl.readOnly = false;
    }
  }
}

function displayRegCodeRow(){
	var hasRegCodeYesElement = document.getElementById("hasRegCodeYes");
	var regCodeRowElement = document.getElementById("regCodeRow");
	var regCodeFormTextElement = document.getElementById("regCodeFormText");
	var regCodeFormHiddenElement = document.getElementById("regCodeFormHidden");
	if(hasRegCodeYesElement.checked){
		regCodeRowElement.className = "show";
		regCodeFormTextElement.disabled = false;
		regCodeFormTextElement.className = "formField";
		regCodeFormHiddenElement.disabled = true;
	} else {
		regCodeRowElement.className = "hide";
		regCodeFormTextElement.disabled = true;
		regCodeFormHiddenElement.disabled = false;
	}
}
function displayZip(){
	var hasZipYesElement = document.getElementById("hasZipYes");
	var zipRowElement = document.getElementById("zipRow");
	var zipFormTextElement = document.getElementById("zipFormText");
	var zipFormHiddenElement = document.getElementById("zipFormHidden");
	if(hasZipYesElement.checked){
		zipRowElement.className = "show";
		zipFormTextElement.disabled = false;
		zipFormTextElement.className = "formField";
		zipFormHiddenElement.disabled = true;
	} else {
		zipRowElement.className = "hide";
		zipFormTextElement.disabled = true;
		zipFormHiddenElement.disabled = false;
	}
}
/**** End moved from content.js ***/

function changeVal(){
	selectRoleElement = document.getElementById('role');
	selectPVElement = document.getElementById('profileValue');
	document.location = 'taskMap.do?roleID=' + selectRoleElement.value + '&profileValueID=' + selectPVElement.value;
}
function sortBy(col){
	exhibitorSearchForm.orderBy.value = col;
	exhibitorSearchForm.submit();
}
function showEmailOption()
{
  if((typeof idArray != "undefined") && idArray.length > 0)
  {
    roleElement = document.getElementById('roleId');
    emailRowElement = document.getElementById('emailRow');
    sendEmailElement = document.getElementById('sendEmail');
    if(idArray[roleElement.value] > 0){
      emailRowElement.className = 'show';
      sendEmailElement.checked = true;
    } else {
      emailRowElement.className = 'hide';
      sendEmailElement.checked = false;
    }
  }
}

var tmpStatement = '';
var tmpBullet1 = '';
var tmpBullet2 = '';
var tmpBullet3 = '';

function clearAndDisable(item)
{
  item.value = '';
  item.disabled = true;
}

function enable(item)
{
  item.disabled = false;
}

function updateSignUI(frm, initial)
{
  if (frm.signageStatementFormat[0].checked)
  {
    // benefit statement, clear and disable the bullet points

    if (!initial)
    {
      tmpBullet1 = frm.signageBulletPoint1.value;
      tmpBullet2 = frm.signageBulletPoint2.value;
      tmpBullet3 = frm.signageBulletPoint3.value;
      frm.signageBenefitStatement.value = tmpStatement;
    }

    clearAndDisable(frm.signageBulletPoint1);
    clearAndDisable(frm.signageBulletPoint2);
    clearAndDisable(frm.signageBulletPoint3);
    enable(frm.signageBenefitStatement);
  }
  else
  {
    // bullet points, clear the benefit statement and enable fields
    if (!initial)
    {
      tmpStatement = frm.signageBenefitStatement.value;
      frm.signageBulletPoint1.value = tmpBullet1;
      frm.signageBulletPoint2.value = tmpBullet2;
      frm.signageBulletPoint3.value = tmpBullet3;
    }

    clearAndDisable(frm.signageBenefitStatement);
    enable(frm.signageBulletPoint1);
    enable(frm.signageBulletPoint2);
    enable(frm.signageBulletPoint3);
  }
}

function loadGroup(ID)
{
  window.location = 'regCode1.do?action=load&regGroupID=' + ID;
}

/* //////  Manage Registrants  /////// */
var currentRegCode = 0;
ERCRegManager = function(tableId, regCode, params){
  currentRegCode = regCode;
  var States = {
    NONE: 0,
    EDIT: 1,
    ADD: 2
  };

  var thisObj = this;
  var rowAddId = 0;
  var currentState = States.NONE;
  var regUsedElement, regTable, headerRow;
  var options = params || {};
  var showSendInvitation = options.showSendInvitation || false;
  var showCCForum = options.showCCForm || false;
  var ccObject = new CreditCardForm();
  
  
  init(tableId);
  
  function init(elementId){
    regTable = document.getElementById(elementId);
    headerRow = regTable.rows[0];
    regUsedElement = document.getElementById('regUsed');
    
    var links, size = regTable.rows.length;
    for(var i=1; i<size; i++){
      links = regTable.rows[i].cells[0].getElementsByTagName("a");
      try{
        /*
        if(links.item(0).getElementsByTagName("img").item(0).className == "imageDelete"){
          links.item(0).personId = getPersonId(regTable.rows[i]);
          links.item(0).onclick = function(){
            alert("delete " + this.personId);
          }
        }
        else throw "Error, could not apply delete action! Image removed?";
        */
        
        if(links.item(1).getElementsByTagName("img").item(0).className == "imageEdit"){
          links.item(1).personId = getPersonId(regTable.rows[i]);
          links.item(1).onclick = function(){
            thisObj.editRegistrant(this.personId);
          }
        }
        else throw "Error, could not apply edit action! Image removed?";
        
        regTable.rows[i].firstName = regTable.rows[i].cells[1].getElementsByTagName('span').item(0).innerHTML;
        regTable.rows[i].lastName = regTable.rows[i].cells[1].getElementsByTagName('span').item(1).innerHTML;
        regTable.rows[i].jobTitle = regTable.rows[i].cells[1].getElementsByTagName('span').item(2).innerHTML;
        regTable.rows[i].email = regTable.rows[i].cells[2].innerHTML;
        
      }
      catch(err){ alert(err); break; }
    }
    
    regUsedElement.num = 0;
    addRegUseNumber(regTable.rows.length-1);
    
    setFrameHeight();
  }
  
  function getPersonId(rowElement){
    if(rowElement.personId == null){
      rowElement.personId = rowElement.id.split("_")[1];
    }
    return rowElement.personId;
  }
  
  this.addRegistrant = function(){
    if(currentState != States.NONE){
      alert('Please finish editing the current user before adding a new one.');
      return;
    }
    currentState = States.ADD;
    
    var newRow = regTable.insertRow(1);
    var newRow2 = regTable.insertRow(2);
    newRow.id = "registrant_aR"+rowAddId;
    newRow.className = "newRow";
    newRow2.className = newRow.className;
    newRow.personId = 'aR'+rowAddId;
    
    appendRowCells(newRow, newRow2, showCCForum);
    newRow.firstNameInput.focus();
    rowAddId++;
    setFrameHeight();
    addRegUseNumber(1);
  }
  
  function appendRowCells(row, row2){
    
    createUserRow(row);
    
    row2.cancelBtn = getButton('button', 'Cancel');
    row2.cancelBtn.row = row;
    row2.cancelBtn.onclick = function(){
      //alert("cancel for " + this.row.personId);
      thisObj.cancel(this.row.personId);
    }
    row2.saveBtn = getButton('submit', "Save");
    
    if(showCCForum){
      row2.saveBtn.value = "Submit";
      ccObject.appendToRow(row2, [row2.cancelBtn, row2.saveBtn]);
      ccObject.row = row2;
    }
    else createEmailRow(row2);

  }
  
  function createUserRow(row, values){
    var rowArray = new Array();
    values = values || {};
    
    rowArray[0] = document.createElement('td');
    rowArray[0].colSpan = 2;
    row.firstNameInput = getTextInput("firstName", {defaultClass:"subtle", defaultValue:"First Name", value:values.firstName});
    row.lastNameInput = getTextInput("lastName", {defaultClass:"subtle", defaultValue:"Last Name", value:values.lastName});
    rowArray[0].appendChild(row.firstNameInput);
    rowArray[0].appendChild(row.lastNameInput);
  
    rowArray[1] = document.createElement('td');
    rowArray[1].colSpan = 2;
    row.emailInput = getTextInput("email", {defaultClass:"subtle", defaultValue:"Email", value:values.email});
    row.jobTitleInput = getTextInput("jobTitle", {defaultClass:"subtle", defaultValue:"Job Title", value:values.jobTitle});
    rowArray[1].appendChild(row.emailInput);
    rowArray[1].appendChild(row.jobTitleInput);
    
    for(var i=0; i<rowArray.length; i++) row.appendChild(rowArray[i]);
  }
  
  function createEmailRow(row){
    var rowArray = new Array();
    
    rowArray[0] = document.createElement('td');
    rowArray[0].colSpan = 2;
    rowArray[0].className = "right border-bottom";
    
    rowArray[1] = document.createElement('td');
    rowArray[1].colSpan = 2;
    rowArray[1].className = "border-bottom";

    if (showSendInvitation){
      row.sendConf = document.createElement('span');
      row.sendConf.style.whiteSpace = "nowrap";
      var textNode = document.createTextNode("Send Confirmation Email");
      row.sendConf.checkbox = getInput("checkbox", "sendEmail");
      row.sendConf.checkbox.checked = true;
      row.sendConf.appendChild(row.sendConf.checkbox);
      row.sendConf.appendChild(textNode);
      rowArray[0].appendChild(row.sendConf);
      row.saveBtn.value = "Send Invitation";
    }
    
    rowArray[1].appendChild(row.cancelBtn);
    rowArray[1].appendChild(row.saveBtn);
    
    for(var i=0; i<rowArray.length; i++) row.appendChild(rowArray[i]);
  }
  
  function getButton(type, value){
    var btn = document.createElement("input");
    btn.type = type;
    btn.value = value;
    btn.style.margin = "0 5px";
    return btn;
  }
  
  function getTextInput(name, params){
    var input = getInput('text', name);
    input.defaultClass = params.defaultClass || "";
    input.defaultValue = params.defaultValue || "";
    if(params.size) input.size = params.size;
    if(params.value){
      input.value = params.value;
    }
    else{
      input.className = input.defaultClass;
      input.value = input.defaultValue;
    }
    input.onfocus = function(){
      if(this.value == this.defaultValue){
        this.className = "";
        this.value = "";
      }
    }
    input.onblur = function(){
      if(this.value == ""){
        this.className = this.defaultClass;
        this.value = this.defaultValue;
      }
    }
    return input;
  }
  function getInput(type, name){
    var input = document.createElement("input");
    input.type = type;
    input.name = name;
    input.style.marginLeft = "8px";
    return input;
  }
  
  var userEditRow = null;
  this.editRegistrant = function(personId){
    if(currentState != States.NONE){
      alert('Please finish editing the current user before adding a new one.');
      return;
    }
    currentState = States.EDIT;
    userEditRow = document.getElementById('registrant_'+personId);
    userEditRow.oldClassName = userEditRow.className;
    userEditRow.className = "hide";
    
    var newRow = regTable.insertRow(userEditRow.rowIndex+1);
    var newRow2 = regTable.insertRow(userEditRow.rowIndex+2);
    newRow.id = "registrant_eR"+userEditRow.personId;
    newRow.className = "newRow border-top";
    newRow2.className = "newRow";
    newRow.personId = 'eR'+userEditRow.personId;
    
    createUserRow(newRow, userEditRow);
    var hiddenEdit = getInput("hidden", "editPersonID");
    newRow.cells[0].appendChild(hiddenEdit);
    hiddenEdit.value = personId;
    
    newRow2.cancelBtn = getButton('button', 'Cancel');
    newRow2.cancelBtn.row = newRow;
    newRow2.cancelBtn.onclick = function(){
      thisObj.cancel(this.row.personId);
    }
    newRow2.saveBtn = getButton('submit', "Save");
    
    createEmailRow(newRow2);
    
    setFrameHeight();
  }
  
  function addRegUseNumber(newNumber){
    regUsedElement.num += newNumber;
    regUsedElement.innerHTML = regUsedElement.num;
    return regUsedElement.num;
  }
    
  this.cancel = function(personId){
    if(currentState == States.ADD) cancelAdd(personId);
    else if(currentState == States.EDIT) cancelEdit(personId);
    else alert("There was an error while trying to cancel the current action.");
  }
  
  function cancelEdit(personId){
    userEditRow.className = userEditRow.oldClassName;
    removeEditRow(personId);
    currentState = States.NONE;
  }
    
  function cancelAdd(personId){
    try{
      var element = document.getElementById('registrant_'+personId);
      if(ccObject && ccObject.row){
        ccObject.remove();
        ccObject.row = null;
      }
      removeEditRow(personId);
      rowAddId--;
      currentState = States.NONE;
      addRegUseNumber(-1);
    }
    catch(e){ alert('There was an error while removing this user.') }
  }
  
  function removeEditRow(personId){
    try{
      var element = document.getElementById('registrant_'+personId);
      if(element){
        regTable.deleteRow(element.rowIndex+1);
        regTable.deleteRow(element.rowIndex);
      }
      setFrameHeight();
    }
    catch(e){ alert('There was an error while trying to edit this user.') }
  }
  
}

var currentFrame = null;
function setFrameHeight(){
  var height = document.body.offsetHeight;
  if(currentFrame == null){
    if(window.parent && currentRegCode != null){
      currentFrame = window.parent.document.getElementById('regFrame_'+currentRegCode);
      if(currentFrame) currentFrame.style.height = height+"px";
    }
  }
  else currentFrame.style.height = height+"px";
}

CreditCardForm = function(){

  var ccObject = this;
  ccObject.table = document.getElementById('creditCardTable');
  ccObject.div = document.getElementById('creditCardDiv');
  ccObject.row = null;
  
  this.appendToRow = function(row, buttonArray){
    ccObject.row = row;
    
    ccObject.table.buttonRow = ccObject.table.insertRow(ccObject.table.rows.length);
    ccObject.table.buttonRow.td = ccObject.table.buttonRow.insertCell(0);
    ccObject.table.buttonRow.td.style.textAlign = "center";
    ccObject.table.buttonRow.td.colSpan = '3';
    
    for(var i=0; i<buttonArray.length; i++) ccObject.table.buttonRow.td.appendChild(buttonArray[i]);
    
    ccObject.radioSelect = document.getElementsByName("useFileCC");
    ccObject.radioSelect[0].onclick = function() {
      ccObject.formVisibility(this.checked? "hide" : "show");
    }
    ccObject.radioSelect[1].onclick = function() {
      ccObject.formVisibility(!this.checked? "hide" : "show");
      document.getElementById('name').focus();
    }
  
    var rowTd = document.createElement('td');
    rowTd.colSpan = 6;
    rowTd.className = "border-bottom";
    rowTd.style.textAlign = "center";
    rowTd.appendChild(ccObject.div);
    ccObject.div.style.display = "block";
    ccObject.row.appendChild(rowTd);
  }
  
  this.formVisibility = function( visClass ){
    var rowArray = ccObject.table.rows;
    
    var start = 1;
    var end = rowArray.length-1;
    
    for(var i=1; i< end; i++) rowArray[i].className = visClass;
    setFrameHeight();
  }
  
  this.remove = function(){
    ccObject.table.deleteRow(ccObject.table.buttonRow.rowIndex);
    ccObject.div.style.display = 'none';
    document.body.appendChild(ccObject.div);
  }
}