﻿/*
  JavaScript functions for EZ1040Extension.com.
  http://www.ez1040extension.com
	
  Copyright 2009  Yoichiro Endo
*/

var FULLNAME_KEY = "fullname=";
var EMAIL_KEY = "email=";
var SSN_SELF_KEY = "ssnSelf=";
var SSN_SPOUSE_KEY = "ssnSpouse=";
var WITH_SPOUSE_KEY = "withSpouse=";
var SPOUSE_NAME_KEY = "spouseName=";
var STREET_KEY = "street=";
var CITY_KEY = "city=";
var STATE_KEY = "state=";
var ZIP_KEY = "zip=";
var OUT_OF_COUNTRY_KEY = "outOfCountry=";
var LIABILITY_KEY = "liability=";
var PAYMENT_KEY = "payment=";
var BALANCE_KEY = "balance=";
var COPAY_KEY = "copay=";
var NO_WAGES_KEY = "noWages=";
var SEMICOLON = ";";
var NEWLINE = "\n";
var DOLLAR = "$";
var CHECKED= "checked";
var YES = "Yes";
var NO = "No";
var FULLNAME_LABEL =       "Your Name:         \t\t";
var EMAIL_LABEL =          "Your Email:        \t\t";
var SSN_SELF_LABEL =       "Your SSN:          \t\t";
var SSN_SPOUSE_LABEL =     "Spouse's SSN:      \t";
var WITH_SPOUSE_LABEL =    "Filing w/ Spouse?: \t";
var SPOUSE_NAME_LABEL =    "Spouse's Name:     \t";
var STREET_LABEL =         "Address:           \t\t";
var CITY_LABEL =           "City/Town:         \t\t";
var STATE_LABEL =          "State Abbr.:       \t\t";
var ZIP_LABEL =            "ZIP Code:          \t\t";
var OUT_OF_COUNTRY_LABEL = "Ouf of Country?:   \t";
var LIABILITY_LABEL =      "Est. Tax Liability: \t\t";
var PAYMENT_LABEL =        "Est. Tax Payment:  \t";
var BALANCE_LABEL =        "Balance Due to IRS: \t";
var COPAY_LABEL =          "Pay Balance Now:   \t";
var NO_WAGES_LABEL =       "Wages Unreceived:  \t";
var ERROR_MESSAGE =        "Error has occured. Please go back to the previous page and review your entries. If the problem persists, please contact us (email: contact@ez1040extension.com).";
var TOS_MESSAGE =          "Please indicate that you read and agree with the EZ1040Extension.com Terms of Service and EZ1040Extension.com Privacy Policy.";
var CLOSED_MESSAGE =       "This service is closed. Please come back next year.";
var SERVICE_CHARGE = "5.00";

function closeWindow()
{
	window.close();
}

function isNumeric(strString)
   //  check for valid numeric strings	
{
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0)
   {
	   return true;
   }

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
   {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
      {
         blnResult = false;
      }
   }
   return blnResult;
}

function calcBalance()
{
    if (!isNumeric(document.tax.liability.value))
    {
    	alert("Error: 'Estimate of total tax liability for 2008' has to be a number!");
    	document.tax.liability.value = '0';
    	return;
    }
    if (!isNumeric(document.tax.payment.value))
    {
    	alert("Error: 'Total 2008 payments' has to be a number!");
    	document.tax.payment.value = '0';
    	return;
    }
	
	var val = document.tax.liability.value-document.tax.payment.value;
	if (val > 0)
	{
		document.tax.balance.value = val;
		document.tax.balance.style.background="#FFE4E1";
		document.tax.balanceNote.style.visibility="visible";
		document.tax.copay.disabled = "";
	}
	else
	{
		document.tax.balance.value = 0;
		document.tax.balance.style.background="#D8D8D8";
		document.tax.balanceNote.style.visibility = "hidden";
		document.tax.copay.disabled = "disabled";
	}
}

function checkSpouse(notify)
{
	if (document.name.withSpouse.checked)
	{
		// Spouse Name
		document.name.spouseName.style.background = "#FFFFFF";
		document.name.spouseName.disabled = "";

		// Spouse SSN
		for (i = 0; i < 3; i++)
		{
			document.ssn.spouse[i].style.background = "#FFFFFF";
			document.ssn.spouse[i].disabled="";
		}
		
		if (notify)
		{
			popupNote("#spouse");
		}
	}
	else
	{
		// Spouse Name
		document.name.spouseName.style.background = "#D8D8D8";
		document.name.spouseName.disabled = "disabled";
		document.name.spouseName.value = "";
		
		// Spouse SSN
		for (i = 0; i < 3; i++)
		{
			document.ssn.spouse[i].style.background = "#D8D8D8";
			document.ssn.spouse[i].disabled="disabled";
			document.ssn.spouse[i].value = "";
		}
	}
}

function processSSN(ssnType, boxNum)
{
	var ssn, maxLength;

	if (ssnType == "self")
	{
		ssn = document.ssn.self;
	}
	else
	{
		ssn = document.ssn.spouse;
	}

	if (!isNumeric(ssn[boxNum].value))
	{
		alert("Error: Social Security Number has to be a number.");
		ssn[boxNum].value = "";
		return;
	}

	switch (boxNum) {
	
	case 0:
		maxLength = 3;
		break;
	case 1:
		maxLength = 2;
		break;
	case 2:
		maxLength = 4;
		break;
	}

	if (ssn[boxNum].value.length >= maxLength)
	{
		ssn[boxNum].blur()
		
		if (boxNum < (ssn.length-1))
		{
			ssn[boxNum+1].focus();
		}
	}
}

function checkZIP()
{
	if (!isNumeric(document.address.zip.value))
	{
		alert("Error: ZIP code has to be a number.");
		document.address.zip.value = "";
	}
}

function popupNote(section)
{
	var win = window.open("note.html"+section,"","height=400,width=350,scrollbars=1");
	
	if (window.focus)
	{
		win.focus()
	}
}

function processCopay()
{
	if (document.tax.copay.checked)
	{
		popupNote("#copay");
		document.tax.copay.checked = false;
	}
}

function clearForms()
{
	for (i = 0; i < document.forms.length; i++)
	{
		document.forms[i].reset();
		
		if (document.forms[i].name == "name")
		{
			for (j = 0; j < document.forms[i].length; j++)
			{
				if ((document.forms[i][j].name == "fullname") ||
					(document.forms[i][j].name == "email"))
				{
					document.forms[i][j].style.background="#FFFFFF";
				}
				else if (document.forms[i][j].name == "spouseName")
				{
					if (document.name.withSpouse.checked)
					{
						document.forms[i][j].style.background="#FFFFFF";
					}
				}
			}
		}
		else if (document.forms[i].name == "address")
		{
			for (j = 0; j < document.forms[i].length; j++)
			{
				if ((document.forms[i][j].name == "street") ||
					(document.forms[i][j].name == "city"))
				{
					document.forms[i][j].style.background="#FFFFFF";
				}
				else if (document.forms[i][j].name == "zip")
				{
					document.forms[i][j].style.background="#FFFFFF";
				}
				else if (document.forms[i][j].name == "state")
				{
					document.forms[i][j].style.background="#FFFFFF";
				}
			}
		}
		else if (document.forms[i].name == "ssn")
		{
			document.forms[i].self[0].style.background="#FFFFFF";
			document.forms[i].self[1].style.background="#FFFFFF";
			document.forms[i].self[2].style.background="#FFFFFF";
			
			if (document.name.withSpouse.checked)
			{
				document.forms[i].spouse[0].style.background="#FFFFFF";
				document.forms[i].spouse[1].style.background="#FFFFFF";
				document.forms[i].spouse[2].style.background="#FFFFFF";
			}
		}
	}
	
	document.extraInfo.formError.value = "";
	checkSpouse(false);
	calcBalance();
	window.scrollTo(0,0);
}

function validateForms()
{
	var hadError = false;

	for (i = 0; i < document.forms.length; i++)
	{
		if (document.forms[i].name == "name")
		{
			for (j = 0; j < document.forms[i].length; j++)
			{
				if ((document.forms[i][j].name == "fullname") ||
					(document.forms[i][j].name == "email"))
				{
					if (document.forms[i][j].value.length == 0)
					{
						document.forms[i][j].style.background="#F5D0A9";
						hadError = true;
					}
					else
					{
						document.forms[i][j].style.background="#FFFFFF";
					}
				}
				else if (document.forms[i][j].name == "spouseName")
				{
					if (document.name.withSpouse.checked)
					{
						if (document.forms[i][j].value.length == 0)
						{
							document.forms[i][j].style.background="#F5D0A9";
							hadError = true;
						}
						else
						{
							document.forms[i][j].style.background="#FFFFFF";
						}
					}
				}
			}
		}
		else if (document.forms[i].name == "address")
		{
			for (j = 0; j < document.forms[i].length; j++)
			{
				if ((document.forms[i][j].name == "street") ||
					(document.forms[i][j].name == "city"))
				{
					if (document.forms[i][j].value.length == 0)
					{
						document.forms[i][j].style.background="#F5D0A9";
						hadError = true;
					}
					else
					{
						document.forms[i][j].style.background="#FFFFFF";
					}
				}
				else if (document.forms[i][j].name == "zip")
				{
					if (document.forms[i][j].value.length < 5)
					{
						document.forms[i][j].style.background="#F5D0A9";
						hadError = true;
					}
					else
					{
						document.forms[i][j].style.background="#FFFFFF";
					}
				}
				else if (document.forms[i][j].name == "state")
				{
					if (document.forms[i][j].value == "--")
					{
						document.forms[i][j].style.background="#F5D0A9";
						hadError = true;
					}
					else
					{
						document.forms[i][j].style.background="#FFFFFF";
					}
				}
			}
		}
		else if (document.forms[i].name == "ssn")
		{
			if ((document.forms[i].self[0].value.length < 3) ||
				(document.forms[i].self[1].value.length < 2) ||
				(document.forms[i].self[2].value.length < 4))
			{
				document.forms[i].self[0].style.background="#F5D0A9";
				document.forms[i].self[1].style.background="#F5D0A9";
				document.forms[i].self[2].style.background="#F5D0A9";
				hadError = true;
			}
			else
			{
				document.forms[i].self[0].style.background="#FFFFFF";
				document.forms[i].self[1].style.background="#FFFFFF";
				document.forms[i].self[2].style.background="#FFFFFF";
			}
			
			if (document.name.withSpouse.checked)
			{
				if ((document.forms[i].spouse[0].value.length < 3) ||
					(document.forms[i].spouse[1].value.length < 2) ||
					(document.forms[i].spouse[2].value.length < 4))
				{
					document.forms[i].spouse[0].style.background="#F5D0A9";
					document.forms[i].spouse[1].style.background="#F5D0A9";
					document.forms[i].spouse[2].style.background="#F5D0A9";
					hadError = true;
				}
				else
				{
					document.forms[i].spouse[0].style.background="#FFFFFF";
					document.forms[i].spouse[1].style.background="#FFFFFF";
					document.forms[i].spouse[2].style.background="#FFFFFF";
				}
			}
		}
	}
	
	if (hadError)
	{
		document.extraInfo.formError.value = "Error in the form. Please check the field highlighted in orange.";
		
 		window.scrollTo(0,0);
		
		return false;
	}
	else
	{
		document.extraInfo.formError.value = "";
		return true;
	}
}

function processNext(key)
{
	var userDataRaw = "";
	var userDataEnc = "";
	var userDataDec = "";

	if (validateForms())
	{
		userDataRaw = createUserData();
		userDataEnc = GibberishAES.enc(userDataRaw, key);
		userDataDec = GibberishAES.dec(userDataEnc, key);

		if (DEBUG)
		{
			document.debug.text.value =
				"userDataRaw = [" +
				userDataRaw +
				"]\nuserDataEnc = [" +
				userDataEnc +
				"]\nuserDataDec = [" +
				userDataDec +
				"]\nkey = " +
				key;
		}
		
		self.location.href = "verification.html?" + userDataEnc;
	}
}

function createUserData()
{
	var userData = "";
	var ssn = "";
	var userSSN = "xxx-xx-xxxx";


	for (i = 0; i < document.forms.length; i++)
	{
		if ((document.forms[i].name == "name") ||
			(document.forms[i].name == "address") ||
			(document.forms[i].name == "tax"))
		{
			for (j = 0; j < document.forms[i].length; j++)
			{
				if (document.forms[i][j].type == "text")
				{
					userData += (document.forms[i][j].name +
						"=" +
						document.forms[i][j].value +
						";");
				}
				else if (document.forms[i][j].type == "checkbox")
				{
					userData += (document.forms[i][j].name +
						"=" +
						((document.forms[i][j].checked)? "checked" : "unchecked") +
						";");
				}
				else if (document.forms[i][j].name == "state")
				{
					userData += (document.forms[i][j].name +
						"=" +
						document.forms[i][j].value +
						";");
				}
			}
		}
		else if (document.forms[i].name == "ssn")
		{
			ssn = document.forms[i].self[0].value +
				"-" +
				document.forms[i].self[1].value +
				"-" +
				document.forms[i].self[2].value;
			
			userData += (document.forms[i].name +
				"Self=" + 
				ssn +
				";");

		    userSSN = ssn;

			if (document.name.withSpouse.checked)
			{
				ssn = document.forms[i].spouse[0].value +
					"-" +
					document.forms[i].spouse[1].value +
					"-" +
					document.forms[i].spouse[2].value;

				userData += (document.forms[i].name +
					"Spouse=" +
					ssn +
					";");
			}
		}
	}
	
	return (userSSN + userData);
}

function extractString(string, key, endmark)
{
	var value = "";
	var substr;
	var index1, index2;

	index1 = string.indexOf(key);
	
	if (index1 >= 0)
	{
		substr = string.substring(index1+key.length);
		index2 = substr.indexOf(endmark);
		
		if (index2 >= 0)
		{
			value = substr.substring(0, index2);
		}
	}
	
	return value;
}

function postUserInfo(userData)
{
	var userInfo = "";
	var value;
	var line = 0;
	
	if (userData.length == 0)
	{
		document.userInfo.display.value = ERROR_MESSAGE;
		return userInfo;
	}
	
	// Get the full name.
	value = extractString(userData, FULLNAME_KEY, SEMICOLON);
	
	if (value.length == 0)
	{
		document.userInfo.display.value = ERROR_MESSAGE;
		return userInfo;
	}
	
	userInfo += FULLNAME_LABEL + value + NEWLINE;
	line++;
	
	// Get the SSN.
	value = extractString(userData, SSN_SELF_KEY, SEMICOLON);
	
	if (value.length == 0)
	{
		document.userInfo.display.value = ERROR_MESSAGE;
		return userInfo;
	}
	
	userInfo += SSN_SELF_LABEL + value + NEWLINE;
	line++;
	
	// Get the email.
	value = extractString(userData, EMAIL_KEY, SEMICOLON);
	
	if (value.length == 0)
	{
		document.userInfo.display.value = ERROR_MESSAGE;
		return userInfo;
	}
	
	userInfo += EMAIL_LABEL + value + NEWLINE + NEWLINE;
	line += 2;

	// Check spouse.
	value = extractString(userData, WITH_SPOUSE_KEY, SEMICOLON);
	
	if (value.length == 0)
	{
		document.userInfo.display.value = ERROR_MESSAGE;
		return userInfo;
	}
	
	if (value == CHECKED)
	{
		userInfo += WITH_SPOUSE_LABEL + YES + NEWLINE;
		line++;

		// Get the spouse's name.
		value = extractString(userData, SPOUSE_NAME_KEY, SEMICOLON);
	
		if (value.length == 0)
		{
			document.userInfo.display.value = ERROR_MESSAGE;
			return userInfo;
		}

		userInfo += SPOUSE_NAME_LABEL + value + NEWLINE;
		line++;
	
		// Get the spouse's SSN.
		value = extractString(userData, SSN_SPOUSE_KEY, SEMICOLON);
	
		if (value.length == 0)
		{
			document.userInfo.display.value = ERROR_MESSAGE;
			return userInfo;
		}
	
		userInfo += SSN_SPOUSE_LABEL + value + NEWLINE + NEWLINE;
		line += 2;
	}
	else
	{
		userInfo += WITH_SPOUSE_LABEL + NO + NEWLINE + NEWLINE;
		line += 2;
	}

	// Get the street name.
	value = extractString(userData, STREET_KEY, SEMICOLON);
	
	if (value.length == 0)
	{
		document.userInfo.display.value = ERROR_MESSAGE;
		return userInfo;
	}
	
	userInfo += STREET_LABEL + value + NEWLINE;
	line++;
	
	// Get the city.
	value = extractString(userData, CITY_KEY, SEMICOLON);
	
	if (value.length == 0)
	{
		document.userInfo.display.value = ERROR_MESSAGE;
		return userInfo;
	}
	
	userInfo += CITY_LABEL + value + NEWLINE;
	line++;
	
	// Get the state.
	value = extractString(userData, STATE_KEY, SEMICOLON);
	
	if (value.length == 0)
	{
		document.userInfo.display.value = ERROR_MESSAGE;
		return userInfo;
	}
	
	userInfo += STATE_LABEL + value + NEWLINE;
	line++;
	
	// Get the ZIP.
	value = extractString(userData, ZIP_KEY, SEMICOLON);
	
	if (value.length == 0)
	{
		document.userInfo.display.value = ERROR_MESSAGE;
		return userInfo;
	}
	
	userInfo += ZIP_LABEL + value + NEWLINE;
	line++;
	
	// Check out-of-country.
	value = extractString(userData, OUT_OF_COUNTRY_KEY, SEMICOLON);
	
	if (value.length == 0)
	{
		document.userInfo.display.value = ERROR_MESSAGE;
		return userInfo;
	}

	userInfo += OUT_OF_COUNTRY_LABEL + ((value == CHECKED)? YES : NO ) + NEWLINE + NEWLINE;
	line+=2;

	// Get the liability.
	value = extractString(userData, LIABILITY_KEY, SEMICOLON);

	if (value.length == 0)
	{
		document.userInfo.display.value = ERROR_MESSAGE;
		return userInfo;
	}

	userInfo += LIABILITY_LABEL + DOLLAR + value + NEWLINE;
	line++;

	// Get the payment.
	value = extractString(userData, PAYMENT_KEY, SEMICOLON);

	if (value.length == 0)
	{
		document.userInfo.display.value = ERROR_MESSAGE;
		return userInfo;
	}

	userInfo += PAYMENT_LABEL + DOLLAR + value + NEWLINE;
	line++;

	// Get the balance.
	value = extractString(userData, BALANCE_KEY, SEMICOLON);

	if (value.length == 0)
	{
		document.userInfo.display.value = ERROR_MESSAGE;
		return userInfo;
	}

	userInfo += BALANCE_LABEL + DOLLAR + value + NEWLINE;
	line++;

	// Check the pay-balance.
	value = extractString(userData, COPAY_KEY, SEMICOLON);
	
	if (value.length == 0)
	{
		document.userInfo.display.value = ERROR_MESSAGE;
		return userInfo;
	}

	userInfo += COPAY_LABEL + ((value == CHECKED)? YES : NO ) + NEWLINE;
	line++;

	// Check the no-wages.
	value = extractString(userData, NO_WAGES_KEY, SEMICOLON);
	
	if (value.length == 0)
	{
		document.userInfo.display.value = ERROR_MESSAGE;
		return userInfo;
	}

	userInfo += NO_WAGES_LABEL + ((value == CHECKED)? YES : NO );
	line++;

	document.userInfo.display.rows = line;
	document.userInfo.display.value = userInfo;
	
	return userInfo;
}

function loadUserInfo(key)
{
	var SSN_FORMAT = "xxx-xx-xxxx";
	var userDataEnc = "";
	var userDataDec = "";
	var userSSN = "";
	var userInfo = "";
	var userInfoFormat = "";
	var userInfoEnc = "";
	
	userDataEnc = self.location.search;
	
	if ((userDataEnc!= null) && (userDataEnc.length > 0))
	{
		// Get rid of the first '?' sign.
		userDataEnc = userDataEnc.substring(1);
	}
	else
	{
		document.userInfo.display.value = ERROR_MESSAGE;
		return userInfo;
	}
	
	userDataDec = GibberishAES.dec(userDataEnc, key);
	userSSN = userDataDec.substring(0, SSN_FORMAT.length);
	userInfo = userDataDec.substring(SSN_FORMAT.length)

	userInfoFormat = postUserInfo(userInfo);
	userInfoEnc = GibberishAES.enc((userSSN + userInfoFormat), key);
	
	return userInfoEnc;
}

function processProceed(key)
{
	var SSN_FORMAT = "xxx-xx-xxxx";
	var userInfoEnc = "";
	var userInfoDec = "";
	var userInfo = "";
	var userSSN = "";
	var SERVICE_CLOSED = true;
	
	if (SERVICE_CLOSED)
	{
		alert(CLOSED_MESSAGE);
		return;
	}

	if (!document.tos.agreeBox.checked)
	{
		alert(TOS_MESSAGE);
		return;
	}

	userInfoEnc = self.location.search;

	if ((userInfoEnc!= null) && (userInfoEnc.length > 0))
	{
		// Get rid of the first '?' sign.
		userInfoEnc = userInfoEnc.substring(1);
	}
	else
	{
		document.tos.display.value = ERROR_MESSAGE;
		return;
	}
		
	document.trans.bg.src = "images/trans-bg.png";
	document.trans.bg.style.width = "100%";
	document.trans.bg.style.height = "100%";
	document.trans.bg.style.top = "0px";
	document.trans.bg.style.left = "0px";
	document.trans.bg.style.position = "absolute";
	
	document.trans.fg.src = "images/trans-fg.png";
	document.trans.fg.style.top = "40%";
	document.trans.fg.style.left = "40%";
	document.trans.fg.style.position = "absolute";
	
	//document.counter.src = "service-counter.html";
	
	userInfoDec = GibberishAES.dec(userInfoEnc, key);
	userSSN = userInfoDec.substring(0, SSN_FORMAT.length);
	userInfo = userInfoDec.substring(SSN_FORMAT.length)
	
	document.order.ssn.value = userSSN;
	document.order.msg.value = userInfo;
	document.order.action += ("?" + GibberishAES.enc(userSSN, key));
	
	window.scrollTo(0,0);
	
	setTimeout("document.order.submit()", 2000);
}

function checkAgreement()
{
	if (document.tos.agreeBox.checked)
	{
		document.action.proceed.src = "images/paypal-proceed.gif";
	}
	else
	{
		document.action.proceed.src = "images/paypal-proceed-fade.gif";
	}
}

function pay(ssn)
{
	document.order.amount.value = SERVICE_CHARGE;
	document.order.item_name.value += (" (" + ssn + ")");
	document.order.submit();
}


