  function sethtml(div,content)
  {
    var search = content;
    var script;

    while( script = search.match(/(<script[^>]+javascript[^>]+>\s*(<!--)?)/i))
    {
      search = search.substr(search.indexOf(RegExp.$1) + RegExp.$1.length);

      if (!(endscript = search.match(/((-->)?\s*<\/script>)/))) break;

      block = search.substr(0, search.indexOf(RegExp.$1));
      search = search.substring(block.length + RegExp.$1.length);

      var oScript = document.createElement('script');
      oScript.text = block;
      document.getElementsByTagName("head").item(0).appendChild(oScript);
    }

    document.getElementById(div).innerHTML=content;
  }

  var http_request = false;
  function makePOSTRequest(url, parameters, div) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = function() { alertContents(div); };
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
  }

   function alertContents(div) {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            //document.getElementById(div).innerHTML = result;            
            sethtml(div, result);
         } else {
            alert('There was a problem with the request.');
         }
      }
   }

function urlPrepare(varnice){
  varnice = encodeURI(decodeURI(varnice));
  return varnice.replace('&', '%26');
}

function programDetails(pid, showLoading){

  if(showLoading){
    $('#'+pid+'_details_info').empty().html('<img src="/images/loading.gif" />');
  }

  $.post('/programdetails', 'program='+pid, function(data){ $('#'+pid+'_details_info').html(data) });
  $('#'+pid+'_details_info').fadeIn('slow');

  return true;
}

function commentProgram(pid){
  $('#'+pid+'_details_content').slideUp('medium');

  $('#'+pid+'_details_content').empty().html('<img src="/images/loading.gif" />');

  $.post('/programcomment', 'program='+pid, function(data){ $('#'+pid+'_details_content').html(data) });
  $('#'+pid+'_details_content').slideDown('medium');
  return true;
}

function editInterview(pid,interview){
  $('#'+pid+'_details_content').slideUp('medium');

  $('#'+pid+'_details_content').empty().html('<img src="/images/loading.gif" />');

  $.post('/editinterview', 'interview='+interview, function(data){ $('#'+pid+'_details_content').html(data) });
  $('#'+pid+'_details_content').slideDown('medium');
}

function deleteInterview(pid,interview){
  if(confirm('Are you sure you want to delete this interview?')){
    $.post('/deleteinterview', 'program='+pid+'&interview='+interview, function(data){ $('#'+pid+'_details_content').html(data) });
  }
}

function submitInterview(pid){
  $('#'+pid+'_details_content').slideUp('medium');

  $('#'+pid+'_details_content').empty().html('<img src="/images/loading.gif" />');

  $.post('/submitinterview', 'program='+pid, function(data){ $('#'+pid+'_details_content').html(data) });
  $('#'+pid+'_details_content').slideDown('medium');
  return true;
}

function submitExperience(pid,interview){
  $('#'+pid+'_details_content').slideUp('medium');

  $('#'+pid+'_details_content').empty().html('<img src="/images/loading.gif" />');

  $.post('/submitexperience', 'interview='+interview, function(data){ $('#'+pid+'_details_content').html(data) });
  $('#'+pid+'_details_content').slideDown('medium');
}


function flagProgram(pid){
  $.post('/flagprogram', 'program_id='+pid, function(data){ $('#'+pid+'_details_flag').html(data) });
  return true;
}

function contactProgram(pid){
  $('#'+pid+'_details_content').slideUp('medium');

  $('#'+pid+'_details_content').empty().html('<img src="/images/loading.gif" />');

  $.post('/contactprogram', 'program='+pid, function(data){ $('#'+pid+'_details_content').html(data) });
  $('#'+pid+'_details_content').slideDown('medium');
  return true;
}

function commentSubmit(form, pid){
  if(form.share.options[form.share.selectedIndex].value == ''){
    alert('Please choose if you want to share your comment with others!');
    form.share.focus();
    return false;
  }else if(form.comment.value == ''){
    alert('You need to type a comment!');
    form.comment.focus();
    return false;
  }else {
    document.getElementById(pid+'_details_info').style.display = '';
    document.getElementById(pid+'_details_content').style.display = '';

    var poststr = "program_id=" + pid;
    poststr += "&comment=" + urlPrepare( form.comment.value );
    poststr += "&share=" + urlPrepare( form.share.options[form.share.selectedIndex].value );

    makePOSTRequest('/programcomment', poststr, pid+'_details_content');
    return false;
  }
}

function experienceSubmit(pid,interview){

  $('#'+pid+'_details_info').show();
  $('#'+pid+'_details_content').show();

  var poststr = 'interview=' + interview;
  poststr += '&experience=' + urlPrepare( $('#experience_'+pid).val() );

  $.post('/submitexperience', poststr, function(data){ $('#'+pid+'_details_content').html(data) });

  return false;
}

function interviewSubmit(pid){

  $('#'+pid+'_details_info').show();
  $('#'+pid+'_details_content').show();

  var poststr = 'program_id=' + pid;
  poststr += '&comments=' + urlPrepare( $('#comments_'+pid).val() );
  poststr += '&interview_date=' + urlPrepare( $('#date_'+pid).val() + ' ' + $('#date_hours_'+pid).val() + ':' + $('#date_minutes_'+pid).val() + $('#date_ampm_'+pid).val() );

  $.post('/submitinterview', poststr, function(data){ $('#'+pid+'_details_content').html(data) });

  return false;
}

function editinterviewSubmit(pid,interview){

  $('#'+pid+'_details_info').show();
  $('#'+pid+'_details_content').show();

  var poststr = 'save_interview=' + interview;
  poststr += '&comments=' + urlPrepare( $('#comments_'+pid).val() );
  poststr += '&interview_date=' + urlPrepare( $('#date_'+pid).val() + ' ' + $('#date_hours_'+pid).val() + ':' + $('#date_minutes_'+pid).val() + $('#date_ampm_'+pid).val() );

  $.post('/editinterview', poststr, function(data){ $('#'+pid+'_details_content').html(data) });

  return false;
}

function contactSubmit(form, pid){
  if(form.subject.value == ''){
    alert('You need to type an email subject!');
    form.subject.focus();
    return false;
  }else if(form.reply_address.value == ''){
    alert('You need to give an email reply address!');
    form.reply_address.focus();
    return false;
  }else if(form.body.value == ''){
    alert('You need to type a body for the message!');
    form.body.focus();
    return false;
  }else {
    document.getElementById(pid+'_details_info').style.display = '';
    document.getElementById(pid+'_details_content').style.display = '';

    var poststr = "program_id=" + pid;
    poststr += "&subject=" + urlPrepare( form.subject.value );
    poststr += "&reply_address=" + urlPrepare( form.reply_address.value );
    poststr += "&body=" + urlPrepare( form.body.value );

    makePOSTRequest('/contactprogram', poststr, pid+'_details_content');
    return false;
  }
}


function expressResults(form){
  if (form.img_fmg.value == ""){
    alert("You must select an option for IMG/FMG status.");
    form.img_fmg.focus();
    return false;
  }else if (form.clinical.value == ""){
    alert("You must select an option for Clinical Exp.");
    form.clinical.focus();
    return false;
  }else if(form.visa.value == ""){
    alert("You must select an option for Student Visa.");
    form.visa.focus();
    return false;
  }else if (form.grad_time.value == ""){
    alert("Time since graduation is Required.");
    form.grad_time.focus();
    return false;
  }else if (((form.step1_2.value == "") || (form.step1_3.value == "")) && ((form.step2_2.value == "") || (form.step2_3.value == ""))) {
    alert('You must submit both test results for at least one USMLE Exam.');
    form.step1_2.focus();
    return false;
  }else if ((form.step1_2.value != "" && form.step1_3.value != "") && ( (form.step1_2.value < "75" || form.step1_3.value < "160") || (form.step1_2.value > "99" || form.step1_3.value > "282") ) ){
    alert("You must enter USMLE Step 1 scores between 75/160 and 99/282.");
    form.step1_2.focus();
    return false;
  }else if ((form.step2_2.value != "" && form.step2_3.value != "") && ( (form.step2_2.value < "75" || form.step2_3.value < "160") || (form.step2_2.value > "99" || form.step2_3.value > "282") ) ){
    alert("You must enter USMLE Step 2 scores between 75/160 and 99/282.");
    form.step2_2.focus();
    return false;

  }else {

    $('#express_form').fadeOut('fast');
    $('#express_results').fadeIn('slow');

    var poststr = $('#express_form_form').serialize();

    $('#express_results').html('<p align="center"><img src="/images/loading.gif" /></p>');

    $.post('/express_ajax', poststr, function(data){ 
	//$('#express_results').html(data); 
        $('#express_results').fadeOut('fast');
        $('#express_form').fadeIn('slow');	
	$.fn.colorbox({html: data}); 
	});
	
    //var thehref = '/express_ajax?'+poststr;
    //$('#expres_resultsfix').colorbox({href:thehref});

    return false;
  }
}
