var alertIncrement = 0;
var timeOut;
var isUsingIE6 = /MSIE (5\.5|6\.)/.test(navigator.userAgent);

// DD - preloading alert gfx into cache for issue with it not appearing on load
var AlertGfx = new Image();
AlertGfx.src = "./images/common/button_close.png";
AlertGfx.src = "./images/common/alert_header_sm.gif";
AlertGfx.src = "./images/common/alert_body_sm.gif";
AlertGfx.src = "./images/common/alert_footer_sm.gif";

function hideSelects()
{
  if (isUsingIE6)
  {
    var sel = getMainFrame().document.getElementsByTagName('select');
    for (var i=0; i < sel.length; i++)
      sel[i].style.visibility = 'hidden';
  }
}

function showSelects()
{
  if (isUsingIE6)
  {
    var sel = getMainFrame().document.getElementsByTagName('select');
    for (var i=0; i < sel.length; i++)
      sel[i].style.visibility = 'visible';
  }
}

function showAlert(head, msg, close, shadow, clickClose, onClose)
{
  // head - words at top
  // msg - main content of the alert
  // close (true/false) - adds a close button in uppper right hand corner
  // shadow (true/false) - drops a shaded area over the entire screen locking the user out
  // clickClose (true/false) - closes alert window by mouse click anywhere on the alert - used by error alerts
  // onClose (str) - string to be evaluated prior to closing the window.  No return value is analyzed.

  // example: showAlert('Approved','Your email address has been confirmed', true, false);
  // buttons can be added using something like:
  //        var html = '<a href="register.html"><img src="./images/login/register_btn.gif" style="margin: 10px;" border=0></a>'
  //        new Insertion.Bottom(add_buttons, html);
  
  // NOTE: be sure to add 'top' to the front of function where necessary - example: top.showAlert();
  onClose = onClose ? onClose : '';
  hideSelects();
  alertIncrement++;
  var inc = alertIncrement;
  var source = 'standard_alert';
  var shadowSetting = 'false';
  if (shadow)
  {
    source = 'shadow_alert';
    $(source).innerHTML = '';
    shadowSetting = 'true';
  }
  var all_close = '';
  if (clickClose)
  {
    all_close = concat('onclick="', onClose, '; closeAlert(\'alert_wrapper_', inc, '\', ', shadowSetting, ');"');
  }
  var html = concat('<div id="alert_wrapper_', inc, '" ', all_close,'>',
             '  <div id="alert_frame_', inc, '" class="alert_frame">',
             '    <div class="alert_header">',
             '      <div id="add_buttons_', inc, '" class="alert_body_btn"></div>',
             '      <div id="alert_header_', inc, '" class="alert_header_title"></div>',
             '    </div>',
             '    <br class="cleaner" />',
             '    <div class="alert_body">',
             '      <div id="alert_info_', inc, '" class="alert_body_msg"></div>',
             '    </div>',
             '    <div class="alert_footer"></div>',
             '  </div>',
             '</div>');
  new Insertion.Bottom($(source), html);
  $(source).style.display = 'block';
  if (shadow)
  {
    $(concat('alert_frame_', inc)).style.left = '180px';
    $(concat('alert_frame_', inc)).style.top = '200px';
  }
  else
  {
    $(source).style.left = '180px';
    $(source).style.top = '200px';  
  }
  $(concat('alert_header_', inc)).innerHTML = head;
  if (close)
  {
    html = concat('<input type="image" class="alert_btn imgbtn" id="close_btn_', inc, '" src="./images/common/button_close.png" onclick="', onClose, '; closeAlert(\'alert_wrapper_', inc, '\', ', shadowSetting, ');" width="63" height="29" title="Close alert"/>');
    $(concat('add_buttons_', inc)).innerHTML = html;
    $(concat('close_btn_', inc)).focus();
  }
  $(concat('alert_info_', inc)).innerHTML = msg;
  return inc;
}

function smallAlert(head, msg, close, shadow, clickClose, onClose)
{  
  // same as showAlert but half the size
  onClose = onClose ? onClose : '';
  alertIncrement++;
  //hideSelects();
  var inc = alertIncrement;
  var source = 'standard_alert';
  var shadowSetting = 'false';
  if (shadow)
  {
    source = 'shadow_alert';
    $(source).innerHTML = '';
    shadowSetting = 'true';
  }
  var all_close = '';
  if (clickClose)
  {
    all_close = concat('onclick="', onClose, '; closeAlert(\'alert_wrapper_', inc, '\', ', shadowSetting, ');"');
  }
  var html = concat('<div id="alert_wrapper_', inc, '" ', all_close,'>',
             '  <div id="alert_frame_', inc, '" class="alert_frame">',
             '    <div class="alert_header_sm">',
             '      <div id="add_buttons_', inc, '" class="alert_body_btn"></div>',
             '      <div id="alert_header_', inc, '" class="alert_header_title"></div>',
             '    </div>',
             '    <div class="alert_body_sm" >',
             '      <div id="alert_info_', inc, '"></div>',
             '      <div id="add_buttons_', inc, '"></div>',
             '    </div>',
             '    <div class="alert_footer_sm"></div>',
             '  </div>',
             '</div>');
  new Insertion.Bottom($(source), html);
  $(source).style.display = 'block';
  if (shadow)
  {
    $(concat('alert_frame_', inc)).style.left = '375px';
    $(concat('alert_frame_', inc)).style.top = '200px';
  }
  else
  {
    $(source).style.left = '375px';
    $(source).style.top = '200px';  
  }
  $(concat('alert_header_', inc)).innerHTML = head;
  if (close)
  {
    html = concat('<input type="image" class="alert_btn imgbtn" id="close_btn_', inc, '" src="./images/common/close_btn.gif" onclick="', onClose, '; closeAlert(\'alert_wrapper_', inc, '\', ', shadowSetting, ');" width="20" height="20" title="Close alert"/>');
    $(concat('add_buttons_', inc)).innerHTML = html;
    $(concat('close_btn_', inc)).focus();
  }
  $(concat('alert_info_', inc)).innerHTML = msg;
  return inc;
}

function subtleAlert(head, msg)
{
  // very small alert that does a fade in/fade out in the lower right hand corner 
  alertIncrement++;
  var inc = alertIncrement;
  var html = concat('<div id="alert_wrapper_', inc, '">',
             '  <div id="alert_frame_', inc, '" class="alert_frame_subtle">',
             '    <div class="alert_header_subtle">',
             '      <div id="alert_header_', inc, '" class="alert_header_subtle_msg"></div>',
             '      <div style="float: right; padding: 10px 10px 0px 10px;">',
             '        <input type="image" id="close_btn_', inc, '" class="alert_btn imgbtn" src="./images/common/close_btn.gif" onclick="closeAlert(\'alert_wrapper_', inc, '\');" title="Close alert" ',
             '         onmouseover="$(\'close_btn_', inc, '\').src = \'./images/common/close_hvr.gif\';" onmouseout="$(\'close_btn_', inc, '\').src = \'./images/common/close_btn.gif\';" />',
             '      </div>',
             '    </div>',
             '    <div class="alert_body_subtle">',
             '      <div id="alert_info_', inc, '"></div>',
             '    </div>',
             '    <div class="alert_footer_subtle"></div>',
             '  </div>',
             '</div>');
  new Effect.Appear($('subtle_alert'),{duration:2});
  new Insertion.Top($('subtle_alert'), html);
  $(concat('alert_header_', inc)).innerHTML = head;
  $(concat('alert_info_', inc)).innerHTML = msg;
  timeOut = setTimeout("fadeSubtleAlerts()",10000); 
}

function fadeSubtleAlerts()
{
  clearTimeout(timeOut);
  new Effect.Fade('subtle_alert',{duration:2}); 
  timeOut = setTimeout("clearSubtleAlerts()",2000); 
}

function clearSubtleAlerts()
{
  clearTimeout(timeOut);
  $('subtle_alert').innerHTML = '';
}

function errorAlert(head, msg, onClose)
{
  // used to replace the standard java alert
  // head - words at top
  // msg - main content of the alert
  var inc = smallAlert(head, msg, true, true, true, onClose);
  return inc;
}

function closeAlert(wrapper, shadow)
{
  // closes open alerts
  //showSelects();
  if (shadow)
  {
    $('shadow_alert').innerHTML = '';
    $('shadow_alert').style.display = 'none';
    if (wrapper == 0 || wrapper == '')
    {
      $('standard_alert').innerHTML = '';
      $('standard_alert').style.display = 'none';    
    }
  }
  else
  {
    $(wrapper).innerHTML = '';
    $(wrapper).style.display = 'none';
  }
}

////////////////////////////////////////////////////////////////////////////
//  THE FUNCTIONS BELOW ARE USED TO CHANGE AND SAVE CONFIRMATION SETTINGS //
////////////////////////////////////////////////////////////////////////////

function confirmationAlert(replyFunction, head, msg, small, yes, no)
{
  // used to replace the standard java confirmation
  // replyFunction - name of the return function that will receive the answer
  // head - words at top
  // msg - main content of the alert
  // small - if set, confirmation will use the small alert instead
  // yes - defaults to 'yes' but can be changed to 'agree', 'true', etc.
  // no - defaults to 'no' but can be changed to 'disagree', 'false', etc.
  
  /* EXAMPLE SETUP:
    function test()
    { confirmationAlert('reply', 'Alert', 'Do you want to make this change?', true); }

    function reply(answer)
    { if (answer) alert('Yes'); else alert('No'); }
  */
  var inc;
  if (small)
    inc = smallAlert(head, msg, false, true);
  else
    inc = showAlert(head, msg, false, true);
  if (!yes) yes = 'Yes';
  if (!no) no = 'No';
  var html = concat('<input class="imgbtn" type="image" src="./images/common/button_continue.png" id="yes_btn_', inc, '" width="77px" height="29px" onclick="confirmationReply(true, ', replyFunction, ', ', inc, ');" title="Continue"/>&nbsp;',
             '<input class="imgbtn" type="image" src="./images/common/button_cancel.png" id="no_btn_', inc, '" width="63px" height="29px" onclick="confirmationReply(false, ', replyFunction, ', ', inc, ');" title="Cancel"/>',
             '<input type="hidden" id="myAnswer" value="false" />');
  $(concat('add_buttons_', inc)).innerHTML = html;
  $(concat('yes_btn_', inc)).focus();
}

function changedAlert(replyFunction, head, msg, saveOK, dontSaveOK)
{
  closeAlert(0, true);
	// used to implement a tri-state function: save and continue, don't save but continue, and cancel.
	// replyFunction - name of the return function that will receive the answer
	// head - words at top
	// msg - main content of the alert
	// saveOK - if true, the save and continue button is displayed.
	// dontSaveOK - if true, the don't save but continue button is displayed.
  
	/* EXAMPLE SETUP:
		function test()
			{ changedAlert('reply', 'Alert', 'Your stuff has changed. What do you want to do?'); }

		function reply(answer)
			{ if (answer == 0) alert('Cancel'); else if (answer == 1) alert('Dont Save'); else alert('Save'); }
	*/
	var inc = showAlert(head, msg, false, true);
	var html = '';
	if (saveOK) html = concat(html, '<input type="image" id="save_btn_', inc, '" class="imgbtn" src="./images/common/button_save_continue.png" width="115px" height="29px" title="Save and continue" onclick="confirmationReply(2, ', replyFunction, ', ', inc, ');" tabindex="1100" />&nbsp;');
	if (dontSaveOK) html = concat(html, '<input type="image" id="dont_save_btn_', inc, '" class="imgbtn" src="./images/common/button_dont_save_continue.png" width="195px" height="29px" title="Don\'t save but continue" onclick="confirmationReply(1, ', replyFunction, ', ', inc, ');" tabindex="1101" />&nbsp;');
	html = concat(html, '<input type="image" id="cancel_save_btn_', inc, '" class="imgbtn" src="./images/common/button_cancel.png" width="63px" height="29px" title="Cancel" onclick="confirmationReply(0, ', replyFunction, ', ', inc, ');" tabindex="1102" />');
	$(concat('add_buttons_', inc)).innerHTML = html;
	if (saveOK) $(concat('save_btn_', inc)).focus();
	else if (dontSaveOK) $(concat('dont_save_btn_', inc)).focus();
	else $(concat('cancel_save_btn_', inc)).focus();
}	

function confirmationReply(answer, replyFunction, inc)
{
  closeAlert(concat('alert_wrapper_', inc), true);
  try
  {
    replyFunction(answer);
  }
  catch(e){}
}

/////////////////////////////////////////////////////////////////////
//   THE FUNCTIONS BELOW ARE USED TO EMAIL AN OPPORTUNITY          //
/////////////////////////////////////////////////////////////////////

function emailAProfile(sendStr, bodyStr, subject, inc)
{
  var email = $(concat('referEmail_', inc)).value;
  var msg = $(concat('referMsg_', inc)).value;
  if (msg.length > 2000)
    msg = msg.substring(0,2000);
  var verifyEmail = /^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
  var isEmail = verifyEmail.test(email);
  
  if (email == '')
  {
    $('refer_error').innerHTML = '<div style="color: red">No email address entered. Please input a valid email address.</div>';
    return false;  
  }
  else if (!isEmail)
  {
    $('refer_error').innerHTML = '<div style="color: red">Invalid email address. Please try Again.</div>';
    return false;
  }
  
  var xml = concat('<EMAIL_PROFILEINFO>', sendStr,
            '<TO><![CDATA[', email, ']]></TO>',
            '<CC></CC>',
            '<MSG><![CDATA[', msg, ']]></MSG>',
            '<SUBJECT><![CDATA[A friend has sent you this profile from itzbig: ', subject, ']]></SUBJECT>',
            bodyStr, '</EMAIL_PROFILEINFO>');
  closeAlert(concat('alert_wrapper_', inc), true);
  getConnFrame().ST.Stream.sendStandardXML('ReferProfileInfoFromRecruiter', xml, 'sendEmailResponse');
  return true;
}
