// Submit forms to a popup window. This script will rebuild the form in the pop window
// filling in values from the form on the original page

// Usage: popupSubmit([action],[method],[inputs][,windowname])
//
//	where:  action=form action
//		method=[GET|POST]
//		inputs=comma separated list of input names
//		windowname=optional name for the popup window

function popupsubmit(formaction,formmethod,forminputs) {
  // Was this function passed an optional window name? If not the window name
  // defaults to 'popup', otherwise use the name passed
  var name='popup';
  var parentinputs=forminputs;
  if ( arguments.length >= 4 ) { parentinputs=arguments[3]; }

  // Open the pop-up window first
  // IE seems to work better if the window is opened larger then resized smaller
  if ( /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent))
  {
    width=1000;
    height=1000;
    y=5000;
    x=5000;
  }

  else
  {
    width=200;
    height=100;
    y=screen.height/2-50;
    x=screen.width/2-100;
  }
    popup=window.open("",name,"height="+height+",width="+width+",screenX="+x+",left="+x+",screenY="+y+",top="+y+",scrollbars=yes,status=yes,location=no");
  //popup.document.writeln("<html><body></body></html>");
  //popup.document.close();
  
  //popup.document.open();
  //popup.document.writeln("<html><head><script src='http://my.colby.edu/ics/clientconfig/HtmlContent/popupSubmit.js'></"+"script></head>");
  popup.document.writeln("<html><head><script language='javascript' type='text/javascript'>");
  popup.document.writeln("function setInputs(forminputs,parentinputs)");
  popup.document.writeln("{");
  popup.document.writeln("  var undef;");
  popup.document.writeln("");
  popup.document.writeln("  inputs=parentinputs.split(',');");
  popup.document.writeln("  cinputs=forminputs.split(',');");
  popup.document.writeln("");
  popup.document.writeln("  for (i=0; i<inputs.length; i=i+1) {");
  popup.document.writeln("    var inputvalue='';");
  popup.document.writeln("");
  popup.document.writeln("    if ( window.opener.document.forms[0].elements[inputs[i]].length !== undef && window.opener.document.forms[0].elements[inputs[i]][0].type == 'radio' ) inputtype='radio';");
  popup.document.writeln("      else inputtype=window.opener.document.forms[0].elements[inputs[i]].type;");
  popup.document.writeln("");
  popup.document.writeln("    switch(inputtype) {");
  popup.document.writeln("      case 'hidden':");
  popup.document.writeln("      case 'submit':");
  popup.document.writeln("      case 'text':");
  popup.document.writeln("      case 'textarea':");
  popup.document.writeln("	  inputvalue=window.opener.document.forms[0].elements[inputs[i]].value;");
  popup.document.writeln("        break;");
  popup.document.writeln("      case 'select-one':");
  popup.document.writeln("        inputvalue=window.opener.document.forms[0].elements[inputs[i]].options[window.opener.document.forms[0].elements[inputs[i]].selectedIndex].value;");
  popup.document.writeln("        break;");
  popup.document.writeln("      case 'checkbox':");
  popup.document.writeln("        if ( window.opener.document.forms[0].elements[inputs[i]].checked == true )");
  popup.document.writeln("          inputvalue=window.opener.document.forms[0].elements[inputs[i]].value;");
  popup.document.writeln("        break;");
  popup.document.writeln("      case 'radio':");
  popup.document.writeln("        for (j=0; j<window.opener.document.forms[0].elements[inputs[i]].length; j=j+1) {");
  popup.document.writeln("          if ( window.opener.document.forms[0].elements[inputs[i]][j].checked == true )");
  popup.document.writeln("            inputvalue=window.opener.document.forms[0].elements[inputs[i]][j].value;");
  popup.document.writeln("        }");
  popup.document.writeln("        break;");
  popup.document.writeln("    }");
  popup.document.writeln("");
  popup.document.writeln("    document.popup.elements[cinputs[i]].value=inputvalue;");
  popup.document.writeln("  }");
  popup.document.writeln("}");
  popup.document.writeln("</"+"script></head>");
  popup.document.writeln("<body xonload='window.opener.name=\"openerwindow\";document.popup.target=\"openerwindow\";setInputs(\""+forminputs+"\",\""+parentinputs+"\");document.popup.submit();window.close();'>");
  popup.document.writeln("<form name='popup' method='"+formmethod+"' action='"+arguments[0]+"'>");

  inputs=forminputs.split(',');
  for (i=0; i<inputs.length; i=i+1) {
    popup.document.writeln('<input type="hidden" name="'+inputs[i]+'">');
  }
  
  popup.document.writeln("</form>");
  popup.document.writeln("</body><script language='javascript'>window.opener.name=\"openerwindow\";document.popup.target=\"openerwindow\";setInputs(\""+forminputs+"\",\""+parentinputs+"\");document.popup.submit();window.close();</"+"script></html>");

  // with IE hide the popup window beyind the main browser window
  //if ( /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent) ) { popup.blur(); }
  //else { popup.focus(); }

  //popup.document.close();
}

function setInputs(forminputs,parentinputs)
{
  var undef;

  inputs=parentinputs.split(',');
  cinputs=forminputs.split(',');

  for (i=0; i<inputs.length; i=i+1) {
    var inputvalue='';

    if ( window.opener.document.forms[0].elements[inputs[i]].length !== undef && window.opener.document.forms[0].elements[inputs[i]][0].type == 'radio' ) inputtype='radio';
      else inputtype=window.opener.document.forms[0].elements[inputs[i]].type;

    switch(inputtype) {
      case 'hidden':
      case 'submit':
      case 'text':
      case 'textarea':
	  inputvalue=window.opener.document.forms[0].elements[inputs[i]].value;
        break;
      case 'select-one':
        inputvalue=window.opener.document.forms[0].elements[inputs[i]].options[window.opener.document.forms[0].elements[inputs[i]].selectedIndex].value;
        break;
      case 'checkbox':
        if ( window.opener.document.forms[0].elements[inputs[i]].checked == true )
          inputvalue=window.opener.document.forms[0].elements[inputs[i]].value;
        break;
      case 'radio':
        for (j=0; j<window.opener.document.forms[0].elements[inputs[i]].length; j=j+1) {
          if ( window.opener.document.forms[0].elements[inputs[i]][j].checked == true )
            inputvalue=window.opener.document.forms[0].elements[inputs[i]][j].value;
        }
        break;
    }

    document.popup.elements[cinputs[i]].value=inputvalue;
  }
}

function isenter(e)
{
  var characterCode;

  if (e && e.which) {
    e = e;
    characterCode = e.which;
  }

  else {
    e = event;
    characterCode = e.keyCode;
  }

  //alert("Code is "+characterCode);

  if (characterCode == 13) {
    return true;
  }

  else {
    return false;
  }
}

function isEnter(e)
{
  var characterCode;

  if (e && e.which) {
    e = e;
    characterCode = e.which;
  }

  else {
    e = event;
    characterCode = e.keyCode;
  }

  //alert("Code is "+characterCode);

  if (characterCode == 13) {
    return true;
  }

  else {
    return false;
  }
}

// show an HTML element based on ID
function showLayer(id) {
  if (element = document.getElementById(id)) {
    element.style.display='block';
  }
}

// hide an HTML element based on ID
function hideLayer(id) {
  if (element = document.getElementById(id)) {
    element.style.display='none';
  }
}
