var g_fadingObj = null;
var g_fadeType = '';
var g_fadeIt = null;
var g_currOpacity = 0;
var g_currOpacityDir = 1;
var g_bDoneFading = false;
var g_arr_ids_to_show_after_fade = null;


function openURLScrolling(url, w, h) {
    openURL(url, w, h, 'yes' );
}
function openURL(url, w, h, scroll) {
	
	wopen(url, 'window', w, h);
	
    function wopen(url, name, w, h)
    {
      // Fudge factors for window decoration space.
      // In my tests these work well on all platforms & browsers.
      w += 32;
      h += 96;
      wleft = (screen.width - w) / 2;
      wtop = (screen.height - h) / 2;
      
      if (scroll == undefined) {
        scroll = 'no';
      }
      
      var win = window.open(url,
        name,
        'width=' + w + ', height=' + h + ', ' +
        'left=' + wleft + ', top=' + wtop + ', ' +
        'location=no, menubar=no, ' +
        'status=no, toolbar=no, scrollbars=' + scroll + ', resizable=no');
      // Just in case width and height are ignored
      win.resizeTo(w, h);
      // Just in case left and top are ignored
      win.moveTo(wleft, wtop);
      win.focus();
    }
	
}


function hideObjs(arrToHide)
{
    for(var i = 0; i < arrToHide.length; i++)
    {   
        var id_to_hide = arrToHide[i];
        var objHideMe = document.getElementById(id_to_hide);
        if(objHideMe != null)
        {
            objHideMe.style.display = 'none';
        } 
    }
}

function showObjs(arrToShow)
{
    for(var i = 0; i < arrToShow.length; i++)
    {   
        var id_to_show = arrToShow[i];
        var objShowMe = document.getElementById(id_to_show);
        if(objShowMe != null)
        {
            objShowMe.style.display = '';
        } 
    }
}

function fadeDiv(on_or_off, id_to_fade, arr_ids_to_hide, arr_ids_to_show_after_fade)
{
    
    var fadeType = '';
    if(on_or_off == 'off')
    {
        g_currOpacity = 100;
        g_currOpacityDir = -1;
        fadeType = 'none';
    }
    else
    {
        g_currOpacity = 0;
        g_currOpacityDir = 1;
    }
    
    g_fadeType = fadeType;
    g_arr_ids_to_show_after_fade = arr_ids_to_show_after_fade;
    
    hideObjs(arr_ids_to_hide);
    
    
    var objToFade = document.getElementById(id_to_fade);
    if(objToFade != null)
    {
        g_fadingObj = objToFade;
        g_fadeType = fadeType;
        objToFade.style.opacity = g_currOpacity;
        g_fadeIt = setInterval("fadeEngine()",1) 
    }
    
    
}

function fadeEngine()
{
    //window.status = 'fading...curr at ' + g_currOpacity;
    
     if(g_currOpacityDir == 1)
    { 
      g_fadingObj.style.display = g_fadeType;
      if (g_currOpacity > 100 )
      {
          g_currOpacity = 100;
          showObjs(g_arr_ids_to_show_after_fade);
          clearInterval(g_fadeIt);
      }  
    }
    else
    { 
     if (g_currOpacity < 0 )
      {
          g_fadingObj.style.display = g_fadeType;
          showObjs(g_arr_ids_to_show_after_fade);
          
          clearInterval(g_fadeIt);
      }  
    }
    
    
    g_currOpacity += g_currOpacityDir*2;
    g_fadingObj.style.opacity = (g_currOpacity / 100);
    g_fadingObj.style.MozOpacity = (g_currOpacity / 100);
    g_fadingObj.style.KhtmlOpacity = (g_currOpacity / 100);
    g_fadingObj.style.filter = "alpha(opacity=" + g_currOpacity + ")";
    
   
    
}

function show(objID)
{
        var objShowMe = document.getElementById(objID);
        if(objShowMe != null)
        {
            objShowMe.style.display = '';
        } 
}

function hide(objID)
{
        var objShowMe = document.getElementById(objID);
        if(objShowMe != null)
        {
            objShowMe.style.display = 'none';
        } 
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  return '';
}
function submitMortgageForm()
{
    var type = getQueryVariable("t");
    var num = getQueryVariable("n");
    if(type == '' || num == '')
    {
        return;
    }
    
    var arrIDsAndMessages = new Array();
    regexp_email = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
    regexp_zip = /\d{5}(-\d{4})?/;
    regexp_ssn = /\d{3}-\d{2}-\d{4}/;
    regexp_phone = /((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}/;
    regexp_notempty = /^.+$/;
    regexp_number = /^\d*$/;
    
    
    if(num == "j")
    {
        //default joint
        arrIDsAndMessages.push(Array("txtFullName", regexp_notempty, "Applicant #1 is missing their full name."));
        arrIDsAndMessages.push(Array("txtEmail", regexp_notempty, "Applicant #1 is missing their email address."));
        arrIDsAndMessages.push(Array("txtEmail", regexp_email, "Applicant #1 has an invalid email address."));
        arrIDsAndMessages.push(Array("txtSSN", regexp_notempty, "Applicant #1 is missing their SSN."));
        arrIDsAndMessages.push(Array("txtSSN", regexp_ssn, "Applicant #1 has an invalid SSN."));
        arrIDsAndMessages.push(Array("txtCurrentAddress", regexp_notempty, "Applicant #1 is missing their current address."));
        arrIDsAndMessages.push(Array("txtHomeTelephone", regexp_notempty, "Applicant #1 is missing their home telephone."));
        arrIDsAndMessages.push(Array("txtHomeTelephone", regexp_phone, "Applicant #1 has an invalid home telephone. Expected format is 123-456-7890."));
        arrIDsAndMessages.push(Array("txtEmployerName", regexp_notempty, "Applicant #1 is missing their employer name."));
        arrIDsAndMessages.push(Array("txtPositionAtJob", regexp_notempty, "Applicant #1 is missing their position at job."));
        arrIDsAndMessages.push(Array("txtLengthAtJob", regexp_notempty, "Applicant #1 is missing their length at job."));
        arrIDsAndMessages.push(Array("txtWorkTelephone", regexp_notempty, "Applicant #1 is missing their work telephone."));
        arrIDsAndMessages.push(Array("txtWorkTelephone", regexp_phone, "Applicant #1 has an invalid work telephone. Expected format is 123-456-7890."));
        arrIDsAndMessages.push(Array("txtIncomePerYear", regexp_number, "Applicant #1 has an invalid income per year."));
        
                
        if(type == "b") 
        {
            //buying form
            arrIDsAndMessages.push(Array("txtAdditionalIncome", regexp_number, "Applicant #1 has an invalid additional income."));
            arrIDsAndMessages.push(Array("txtApproxTotalAssets", regexp_notempty, "Applicant #1 is missing their approximate total assets."));
            arrIDsAndMessages.push(Array("txtApproxTotalAssets", regexp_number, "Applicant #1 has an invalid approximate total assets."));
            arrIDsAndMessages.push(Array("txtApproxDownPayment", regexp_notempty, "Applicant #1 is missing their approximate down payment."));
            arrIDsAndMessages.push(Array("txtApproxDownPayment", regexp_number, "Applicant #1 has an invalid approximate down payment."));
            arrIDsAndMessages.push(Array("txtDesiredMonthlyPayment", regexp_number, "Applicant #1 has an invalid desired monthly payment."));
        }
        
        //second applicant default stuff
        arrIDsAndMessages.push(Array("txtFullName2", regexp_notempty, "Applicant #2 is missing their full name."));
        arrIDsAndMessages.push(Array("txtEmail2", regexp_notempty, "Applicant #2 is missing their email address."));
        arrIDsAndMessages.push(Array("txtEmail2", regexp_email, "Applicant #2 has an invalid email address."));
        arrIDsAndMessages.push(Array("txtSSN2", regexp_notempty, "Applicant #2 is missing their SSN."));
        arrIDsAndMessages.push(Array("txtSSN2", regexp_ssn, "Applicant #2 has an invalid SSN."));
        arrIDsAndMessages.push(Array("txtCurrentAddress2", regexp_notempty, "Applicant #2 is missing their current address."));
        arrIDsAndMessages.push(Array("txtHomeTelephone2", regexp_notempty, "Applicant #2 is missing their home telephone."));
        arrIDsAndMessages.push(Array("txtHomeTelephone2", regexp_phone, "Applicant #2 has an invalid home telephone. Expected format is 123-456-7890."));
        arrIDsAndMessages.push(Array("txtEmployerName2", regexp_notempty, "Applicant #2 is missing their employer name."));
        arrIDsAndMessages.push(Array("txtPositionAtJob2", regexp_notempty, "Applicant #2 is missing their position at job."));
        arrIDsAndMessages.push(Array("txtLengthAtJob2", regexp_notempty, "Applicant #2 is missing their length at job."));
        arrIDsAndMessages.push(Array("txtWorkTelephone2", regexp_notempty, "Applicant #2 is missing their work telephone."));
        arrIDsAndMessages.push(Array("txtWorkTelephone2", regexp_phone, "Applicant #2 has an invalid work telephone. Expected format is 123-456-7890."));
        arrIDsAndMessages.push(Array("txtIncomePerYear2", regexp_number, "Applicant #2 has an invalid income per year."));
        
        
        
         if(type == "b") 
        {
            //buying form
            arrIDsAndMessages.push(Array("txtAdditionalIncome2", regexp_number, "Applicant #2 has an invalid additional income."));
            arrIDsAndMessages.push(Array("txtApproxTotalAssets2", regexp_notempty, "Applicant #2 is missing their approximate total assets."));
            arrIDsAndMessages.push(Array("txtApproxTotalAssets2", regexp_number, "Applicant #2 has an invalid approximate total assets."));
            arrIDsAndMessages.push(Array("txtApproxDownPayment2", regexp_notempty, "Applicant #2 is missing their approximate down payment."));
            arrIDsAndMessages.push(Array("txtApproxDownPayment2", regexp_number, "Applicant #2 has an invalid approximate down payment."));
            arrIDsAndMessages.push(Array("txtDesiredMonthlyPayment2", regexp_number, "Applicant #2 has an invalid desired monthly payment."));
        }
        
        
       
        
    }
    else
    {
    //single
     //default joint
        arrIDsAndMessages.push(Array("txtFullName", regexp_notempty, "Applicant is missing their full name."));
        arrIDsAndMessages.push(Array("txtEmail", regexp_notempty, "Applicant is missing their email address."));
        arrIDsAndMessages.push(Array("txtEmail", regexp_email, "Applicant has an invalid email address."));
        arrIDsAndMessages.push(Array("txtSSN", regexp_notempty, "Applicant is missing their SSN."));
        arrIDsAndMessages.push(Array("txtSSN", regexp_ssn, "Applicant has an invalid SSN."));
        arrIDsAndMessages.push(Array("txtCurrentAddress", regexp_notempty, "Applicant is missing their current address."));
        arrIDsAndMessages.push(Array("txtHomeTelephone", regexp_notempty, "Applicant is missing their home telephone."));
        arrIDsAndMessages.push(Array("txtHomeTelephone", regexp_phone, "Applicant has an invalid telephone number. Expected format is 123-456-7890."));
        arrIDsAndMessages.push(Array("txtEmployerName", regexp_notempty, "Applicant is missing their employer name."));
        arrIDsAndMessages.push(Array("txtPositionAtJob", regexp_notempty, "Applicant is missing their position at job."));
        arrIDsAndMessages.push(Array("txtLengthAtJob", regexp_notempty, "Applicant is missing their length at job."));
        arrIDsAndMessages.push(Array("txtWorkTelephone", regexp_notempty, "Applicant is missing their work telephone."));
        arrIDsAndMessages.push(Array("txtWorkTelephone", regexp_phone, "Applicant has an invalid work telephone. Expected format is 123-456-7890."));
        arrIDsAndMessages.push(Array("txtLengthAtJob", regexp_notempty, "Applicant is missing their length at job."));
        arrIDsAndMessages.push(Array("txtIncomePerYear", regexp_number, "Applicant has an invalid income per year."));
        
        if(type == "b") 
        {
            //buying form
             arrIDsAndMessages.push(Array("txtAdditionalIncome", regexp_number, "Applicanthas an invalid additional income."));
            arrIDsAndMessages.push(Array("txtApproxTotalAssets", regexp_notempty, "Applicant is missing their approximate total assets."));
            arrIDsAndMessages.push(Array("txtApproxTotalAssets", regexp_number, "Applicant has an invalid approximate total assets."));
            arrIDsAndMessages.push(Array("txtApproxDownPayment", regexp_notempty, "Applicant is missing their approximate down payment."));
            arrIDsAndMessages.push(Array("txtApproxDownPayment", regexp_number, "Applicant has an invalid approximate down payment."));
            arrIDsAndMessages.push(Array("txtDesiredMonthlyPayment", regexp_number, "Applicant has an invalid desired monthly payment."));
        } 
    }
    
    arrIDsAndMessages.push(Array("txtPriceRange", regexp_notempty, "Application is missing price range."));
    arrIDsAndMessages.push(Array("txtBedrooms", regexp_notempty, "Application is missing the number of bedrooms."));
    arrIDsAndMessages.push(Array("txtBaths", regexp_notempty, "Application is missing the number of baths."));
    arrIDsAndMessages.push(Array("txtBedrooms", regexp_number, "Application has an invalid number of bedrooms."));
    arrIDsAndMessages.push(Array("txtBaths", regexp_number, "Application has an invalid number of baths."));
    arrIDsAndMessages.push(Array("chkNeighborhoods", '', "Application is missing a neighborhood."));
    arrIDsAndMessages.push(Array("txtMoveInDate", regexp_notempty, "Application is missing the move in date."));
    
    var arrResponse = validateObjects(arrIDsAndMessages);
    if(!arrResponse.length)
    {
       //submit
       
       document.getElementById('errorSummary').style.display = 'none';
       document.forms[0].submit();
    }
    else
    {
        var finalMess = '';
        for(x in arrResponse)
        {
            finalMess += "<p>" + arrResponse[x] + "</p>";
        }
        //alert(finalMess);
        document.getElementById('errorSummary').innerHTML = finalMess;
        document.getElementById('errorSummary').style.display = '';
    }
    
}

function validateObjects(arr)
{
//arr is array of arrays. each item has following 
//[0] = id of obj to check
//[1] = reg exp to test against. if empty, it is a checkbox so just check that something is checked
//[2] = the error message

    var finalArr = new Array();
    for(x in arr)
    {
        var curr = arr[x];
        var currID = curr[0];
        var currRegExp = curr[1];
        var currErrorMess = curr[2];
        
        //alert('checking id: ' + currID);
        if(currRegExp != '')
        {
            var currVal = document.getElementById(currID).value;
            if(!currVal.match(currRegExp))
            {
                finalArr.push(currErrorMess);
            }
        }
        else
        {
            //special case, check box
            
        }
        
    }
    
    return finalArr;
}