﻿/*@cc_on @*/

var dateAllowed = '0123456789/';

function reformatSocialSecurityNumber( element )
{
  var newvalue = element.value;
  newvalue = newvalue.replace(/-/g,'');
  
  if(newvalue.replace(/[\D]/g,'').length == 9)
  {
      newvalue = newvalue.substr(0,3) + '-' +
                 newvalue.substr(3,2) + '-' +
                 newvalue.substr(5,4);
  }
  
  element.value = newvalue;
}


function reformatDate( element, y2k )
{
   var now=new Date();
   var year=now.getYear();
   // change to current year + 1.  so in 2009 10 and below will go to 2000
   element.value = formatDateStr(element.value,'MM/dd/yyyy',year-1998);
}

function reformatYear( element )
{
   var now=new Date();
   var year=now.getYear();

    var date = '01/01/' + element.value;
    date = formatDateStr(date,'MM/dd/yyyy',year-1998);
    if( date.length == 10 )
        date = date.substr(6,4);
    element.value = date;
}

function reformatZip( element )
{
    var newvalue = element.value;
    newvalue = newvalue.replace(/-/g,'');
    if( element.value.length > 5 )
      element.value = newvalue.substr(0,5) + '-' +
                      newvalue.substr(5,4);
    else
        element.value = newvalue.substr(0,5);
}

function reformatPhone( element )
{
    var newvalue = element.value;
    newvalue = newvalue.replace(/[-(). /]/g,'');

    if( newvalue.length == 10 )
        element.value = newvalue.substr(0,3) + "-" +
                        newvalue.substr(3,3) + "-" + 
                        newvalue.substr(6,4);
}

function defaultQuoteName()
{
    var txtQuoteName = document.getElementById("ctl00_Main_txtQuoteName");

    if( txtQuoteName.value.length == 0 )
    {
        var txtApplicantLastName = document.getElementById("ctl00_Main_txtApplicantLastName");
        var txtApplicantFirstName = document.getElementById("ctl00_Main_txtApplicantFirstName");
        var txtApplicantMiddleName = document.getElementById("ctl00_Main_txtApplicantMiddleName");
    
        txtQuoteName.value = txtApplicantLastName.value;
        if( txtApplicantFirstName.value.length > 0 )
        {
            if( txtApplicantLastName.value.length > 0 )
                txtQuoteName.value += ", ";
                
            txtQuoteName.value += txtApplicantFirstName.value;
        }
                
        if( txtApplicantMiddleName.value.length > 0 )
            if( txtApplicantFirstName.value.length > 0 ||
                txtApplicantLastName.value.length > 0 )
                txtQuoteName.value += " " + txtApplicantMiddleName.value;

        txtQuoteName.value += txtApplicantMiddleName.value;
    }
}

function exitQuote()
{
  if( document.getElementById("ctl00_hdnQuoteKey").value.length > 0 )
  {
/*@if (@_win32 && @_jscript_version>=5)
  execScript('result = msgbox("Do you want to save your changes before continuing?","547","Save Changes")', "vbscript");
  return result;
@else @*/
    var result = confirm("Exit without saving changes?\nIf you want to save the changes to your quote, click \"Cancel\" and then click the \"Save\" button before exiting.");
    if (result)
        return 7;
    else
        return 2;
/*@end @*/
  }
  return 7;

}

function IsValidEmail(address) 
{
    if( address.indexOf('@') == -1 || address.indexOf('.') == -1 )
        return false;
    else
        return (address.match(/^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/));// crashes with long string of letters
}

function ValidateEmail(caller) {
    if (caller == null)
        return;

    if (IsValidEmail(caller.value)) {
        caller.className = 'inputlink';
        caller.title = 'Double click to send an email';
    }
    else {
        caller.className = 'input';
        caller.title = '';
    }
}

function StartEmail(caller) {
    if (IsValidEmail(caller.value))
        window.open('mailto:' + caller.value, '_blank', '', '');
}

var CalendarTextBoxID = null;



function SetFocus() {
    for (i = 0; i < document.forms[0].elements.length; i++) {
        if (document.forms[0].elements[i].getAttribute('initialfocus') != null) {
            if(!document.forms[0].elements[i].disabled)
                document.forms[0].elements[i].focus();
            break;
        }
    }
}

function KeyDown(inEvent, activate) {
    var caller = null;
    var e = inEvent || window.event;
    
    if (e.keyCode == 27)
        DisplayNotesBox(false);

    if (e.target)
        caller = e.target;
    else if (e.srcElement)
        caller = e.srcElement;
        
    if ((e.keyCode == 13) && (caller.tagName.toLowerCase() != "textarea")) {
        if ((!caller) || ((caller.tagName.toLowerCase() != "a") && (caller.tagName.toLowerCase() != "input") && (caller.tagName.toLowerCase() != "select"))) {
            e.keyCode = -1;
            return false;
        }
        else if ((caller.getAttribute("id")) && (caller.getAttribute("id") == "ctl00_Main_Login2_Password"))
        {
            //document.getElementById("ctl00_Main_Login2_LoginButton").click();
            __doPostBack('ctl00$Main$Login2$LoginButton','');
            return false;
        }
        else if (caller.tagName.toLowerCase() != "a")
            e.keyCode = 9;
    }

    // Tabbing is disabled when simulating a modal popup.
    if ((e.keyCode == 9) && (document.getElementById('divModal') != null) && (document.getElementById('divModal').className == 'modal')) {
        e.keyCode = -1;
        return false;
    }


    return true;
}

function RestrictInput(inEvent, allowed) {
    var e = inEvent || window.event;
    
    // Referencing e after this doesn't work in non-IE browsers???

    // Exemptions:
    // 35 - Home
    // 36 - End 
    // 46 - Delete
    if ((e.keyCode >= 32) && !(e.altKey | e.ctrlKey) && (e.keyCode != 35) && (e.keyCode != 36) && (e.keyCode != 46) && (allowed.indexOf(String.fromCharCode(e.keyCode)) == -1))
    {
        e.keyCode = -1;
        return false;
    }
        
    return true;
}

function ModalBackground(show) {
    if (document.getElementById('divModal') != null) {
        if (show)
            document.getElementById('divModal').className = 'modal';
        else
            document.getElementById('divModal').className = 'modal_hidden';
    }
}

function DisplayNotesBox(show) {
    if (document.getElementById('divNotes') != null) {
        if ((show) && (document.getElementById('divNotes').style.display == 'none')) {
            ModalBackground(show);
            UpdateNotesDisplay();
            document.getElementById('divNotes').style.display = '';
            document.getElementById('ctl00_txtQuoteNotes').focus();
            hdnNotesOld = document.getElementById('ctl00_txtQuoteNotes').value;
        }
        else if ((!show) && (document.getElementById('divNotes').style.display == '')) {
            ModalBackground(show);
            document.getElementById('divNotes').style.display = 'none';
            if (hdnNotesOld != null) {
                document.getElementById('ctl00_txtQuoteNotes').value = hdnNotesOld;
                hdnNotesOld = null;
            }
        }
    }
}

function SaveNotes() {
    hdnNotesOld = null;
    DisplayNotesBox(false);
}

var notesMaxLength = 400;

function UpdateNotesDisplay() {
    try {
        var noteBox = document.getElementById('ctl00_txtQuoteNotes');

        if (noteBox.value.length > notesMaxLength)
            noteBox.value = noteBox.value.substring(0, notesMaxLength);

        document.getElementById('NotesStatus').innerHTML = noteBox.value.length + '/' + notesMaxLength + ' characters';
    }
    catch (e) {
    }
}

function NotesKeyPress(e, caller) {
    // Enter key generates 2 characters to make a line break.  Only allow it if there's room for both.
    if ((e.keyCode == 13) && (caller.value.length >= notesMaxLength - 1)) {
        e.keyCode = -1;
        return false;
    }
    UpdateNotesDisplay();
}

findPosY = function(obj){
    var curtop = 0;
    if (document.getElementById || document.all) {
	    while (obj.offsetParent) {
		    curtop += obj.offsetTop;
		    if (typeof(obj.scrollTop)  == 'number')
			    curtop -= obj.scrollTop;
		    obj = obj.offsetParent;
	    }
    }
    else if (document.layers)
	    curtop += obj.y;
    return curtop;
}

findPosX = function(obj) {
    var curleft = 0;
    if (document.getElementById || document.all) {
	    while (obj.offsetParent) {
		    curleft += obj.offsetLeft
		    obj = obj.offsetParent;
	    }
    }
    else if (document.layers)
	    curleft += obj.x;
    return curleft;
}

var calendarIsOpen;

function cancelCalendar()
{
    if( calendarIsOpen )
    {
        var calendar = document.getElementById('ctl00_pnlCalendar');
        calendar.style.display = 'none';
        document.body.onclick = null;
    }
    else
        calendarIsOpen = true;
}

function showCalendar(link,textboxid)
{
    CalendarTextBoxID = textboxid;
    var calendar = document.getElementById('ctl00_pnlCalendar');
    calendar.style.position = 'absolute';
    calendar.style.left = findPosX(link) + 'px';
    calendar.style.top = findPosY(link) + 'px';
    document.body.onclick = window.parent.cancelCalendar;
    window.frames[0].location = 'Calendar.aspx?DefaultSelection=' + document.getElementById('ctl00_Main_' + CalendarTextBoxID).value;
    calendarIsOpen = false;
}

function updateCalendarTextBox(value)
{
    var tmpElement = document.getElementById('ctl00_Main_' + CalendarTextBoxID);
    if(tmpElement != null)
    {
        tmpElement.value = value;
        // Fire events.
        if(tmpElement.onchange)
            tmpElement.onchange();
        tmpElement.blur();
    }
}

