// how many alternate locations they have
var locationCount = 1;
// how many state unemployment informations they have
var suiCount;
// how many benefits are offered
var benCount = 1;
//array of states
var state = new Array("Select","AK","AL","AR","AZ","CA","CO","CT","DC","DE","FL","GA","HI","IA","ID","IL","IN","KS","KY","LA","MA","MD","ME","MI","MN","MO","MS","MT","NC","ND","NE","NH","NJ","NM","NV","NY","OH","OK","OR","PA","PR","RI","SC","SD","TN","TX","UT","VA","VI","VT","WA","WI","WV","WY");
//active tab;
var activeTab = "compData";

function changeVolBen(){
	if(document.getElementById("volBen").options[document.getElementById("volBen").selectedIndex].value == "Other")
		change("volBenDesc","onOff");
	else
		change("volBenDesc","off");
}

function setValues()
{
	locationCount = parseInt(document.getElementById("numLoc").value);
	//suiCount = parseInt(document.getElementById("numSUI").value);
	benCount = parseInt(document.getElementById("numBen").value);
	
	//added by ztg 4-3
	// destroyed by ABC 6-14
	/*
	outPay = document.getElementById("outPay").checked;
	if(outPay)
		change('outPayB','onOff')
	else
		change('outPayB','off')
		
	cobra = document.getElementById("cobra").checked;
	if(cobra)
		change('cobraB','onOff')
	else
		change('cobraB','off')
		
	outHR = document.getElementById("outHR").checked;
	if(outHR)
		change('hrbox','onOff')
	else
		change('hrbox','off')
	
	cafePlan = document.getElementById("cafePlan").checked;
	if(cafePlan)
		change('cafePlanB','onOff')
	else
		change('cafePlanB','off')
	*/
}

// change the class of any element by id

function change(id, newClass)
{
	identity=document.getElementById(id);
	identity.className=newClass;
}

// Multiple Locations functions

function expandLoc()
{
	var data = new Array();
	locationCount++;
	
	for(i=0; i < locationCount; i++)
	{
		var state,taxId,suta,uiNum,numEE;
		
		
		try{
			state = document.getElementById("state"+i).value;}
		catch(err){			
			state = "blank";}
			
		try{
			taxId = document.getElementById("taxId"+i).value;}
		catch(err){
			taxId = "blank";}

		try{
			suta = document.getElementById("suta"+i).value;}
		catch(err){
			suta = "blank";}
			
		try{
			uiNum = document.getElementById("uiNum"+i).value;}
		catch(err){
			uiNum = "blank";}
			
		try{
			numEE = document.getElementById("numEE"+i).value;}
		catch(err){
			numEE = "blank";}
		
		if (state.length < 1)
			state = "blank";
		if (taxId.length < 1)
			taxId = "blank";
		if (suta.length < 1)
			suta = "blank";
		if (uiNum.length < 1)
			uiNum = "blank";
		if (numEE.length < 1)
			numEE = "blank";
		
		data[i] = new Array(state,taxId,suta,uiNum,numEE);
	}
	
	writeLocationTable(data);
}

// removes a location
function removeLoc(index)
{
	var data = new Array();
	var position = 0;
	
		for(i=0; i < locationCount; i++)
	{
		if(i != index)
		{
			var state,taxId,suta,uiNum,numEE;
			
			
			try{
				state = document.getElementById("state"+i).value;}
			catch(err){			
				state = "blank";}
				
			try{
				taxId = document.getElementById("taxId"+i).value;}
			catch(err){
				taxId = "blank";}

			try{
				suta = document.getElementById("suta"+i).value;}
			catch(err){
				suta = "blank";}
				
			try{
				uiNum = document.getElementById("uiNum"+i).value;}
			catch(err){
				uiNum = "blank";}
				
			try{
				numEE = document.getElementById("numEE"+i).value;}
			catch(err){
				numEE = "blank";}
			
			if (state.length < 1)
				state = "blank";
			if (taxId.length < 1)
				taxId = "blank";
			if (suta.length < 1)
				suta = "blank";
			if (uiNum.length < 1)
				uiNum = "blank";
			if (numEE.length < 1)
				numEE = "blank";
			
			data[position] = new Array(state,taxId,suta,uiNum,numEE);
			position++;
		}
	}
	
	locationCount--;
	writeLocationTable(data);
}

//writes the location table
function writeLocationTable(data)
{
	var locTable = document.getElementById("locations");
	var html = "\n<table>\n\t<tr>\n\t\t<th>State</th>\n\t\t<th>St. Tax ID</th>\n\t\t<th>SUTA Rate %</th>\n\t\t<th>UI ID</th>\n\t\t<th># of EE</th>\n\t\t<th>&nbsp;</th>\n\t</tr>";
	for(i=0; i < locationCount; i++)
	{
		var rowClass = "gBG";
		if(i%2 == 0)
			rowClass = "yBG";	
		
		html += "\n\t<tr class='"+rowClass+"'>";
		html += "\n\t\t<td>";
		html += "\n\t\t\t<select id=\"state"+i+"\" name=\"state"+i+"\">";
		for(s=0; s < 51; s++)
		{
			if(data[i][0] == state[s])
				html += "\n\t\t\t\t<option selected value='"+state[s]+"'>"+state[s]+"</option>";
			else
				html += "\n\t\t\t\t<option value='"+state[s]+"'>"+state[s]+"</option>";
		}
		html += "\n\t\t\t</select>";
		html += "\n\t\t</td>";
		html += "\n\t\t<td><input value=\""+checkWrite(data[i][1])+"\" type=\"text\" size=\"7\" onblur=\"numCheck('taxId"+i+"')\" id=\"taxId"+i+"\" name=\"taxId"+i+"\" /></td>";
		html += "\n\t\t<td><input value=\""+checkWrite(data[i][2])+"\" type=\"text\" size=\"7\" onblur=\"numCheck('suta"+i+"')\" id=\"suta"+i+"\" name=\"suta"+i+"\" /></td>";
		html += "\n\t\t<td><input value=\""+checkWrite(data[i][3])+"\" type=\"text\" size=\"7\" id=\"uiNum"+i+"\" name=\"uiNum"+i+"\" /></td>";
		html += "\n\t\t<td><input value=\""+checkWrite(data[i][4])+"\" type=\"text\" size=\"7\" onblur=\"numCheck('numEE"+i+"')\" id=\"numEE"+i+"\" name=\"numEE"+i+"\" /></td>";		
		
		if (locationCount == 1)
		{
			html += "\n\t\t<td>&nbsp;</td>\n\t</tr>";
		}
		else
		{
			html += "\n\t\t<td>&nbsp;&nbsp;<a href='javascript: removeLoc("+i+")'>remove</a></td>\n\t</tr>";
		}
	}
	html += "\n</table>\n\n<span onclick='expandLoc()' class='btn'>add another location</span><input type='hidden' id='numLoc' name='numLoc' value='"+locationCount+"' />";
	locTable.innerHTML = html;
}

function checkWrite(text){
	if(text == "blank")
		return "";
	else
		return text;
}

//runs a basic check to see if an element of a form contains data
function basicCheck(element)
{
	var text,valid;
	valid = "true";
	text = document.getElementById(element).value;
	if (text == "")
	{
		document.getElementById(element).className = "err";
		try{
			document.getElementById(element + "Error").className = "errMsg";
		}
		catch(err){
			// do nothing
		}
		valid = "false";		
	}
	else
	{
		document.getElementById(element).className = "blank";
		try{
			document.getElementById(element + "Error").className = "hidden";
		}
		catch(err){
			// do nothing
		}
	}
	
	return valid;
}

function phoneCheck(element)
{
	var text,valid;
	valid = "true";
	numCheck(element + "1");
	numCheck(element + "2");
	numCheck(element + "3");
	
	text = document.getElementById(element + "1").value;
	text += document.getElementById(element + "2").value;
	text += document.getElementById(element + "3").value;
	
	if (text == "" && element == "phone")
	{
		document.getElementById(element + "1").className = "err";
		document.getElementById(element + "2").className = "err";
		document.getElementById(element + "3").className = "err";
		valid = "false";		
		try{
			document.getElementById(element + "Error").className = "errMsg";
		}
		catch(err){
			// do nothing
		}
	}
	else
	{
		if(text.length == 10 || text.length == 0 && element == "fax")
		{
			document.getElementById(element + "1").className = "blank";
			document.getElementById(element + "2").className = "blank";
			document.getElementById(element + "3").className = "blank";
			try{
			document.getElementById(element + "Error").className = "hidden";
			}
			catch(err){
				// do nothing
			}
		}
		else
		{
			document.getElementById(element + "1").className = "err";
			document.getElementById(element + "2").className = "err";
			document.getElementById(element + "3").className = "err";
			try{
			document.getElementById(element + "Error").className = "errMsg";
			}
			catch(err){
				// do nothing
			}
			valid = "false";
			
		}
		
	}
	
	document.getElementById(element).value = text;
	return valid;
}

function numCheck(element)
{
	var temp,myRE;
	text = document.getElementById(element).value;
	temp = text;
	text = "";
	myRE = new RegExp("[0-9]","i");
	for(i=0; i < temp.length; i++)
		if(temp.charAt(i).match(myRE) != null || temp.charAt(i) == '.')
			text += temp.charAt(i);

	document.getElementById(element).value = text;
	
	return text;
}

//checks email
function emailCheck(element)
{
	var dot = "false";
	var at = "false";
	var valid = "false";
	var mail = document.getElementById(element).value;
	
	
	for(i=0; i < mail.length; i++)
	{
		if(mail.charAt(i) == '.')
			dot = "true";
		else if(mail.charAt(i) == '@')
			at = "true";
	}
	
	if(dot == "true" && at == "true")
		valid = "true";
		
	if (valid == "false")
	{
		document.getElementById(element).className = "err";
		try{
			document.getElementById(element + "Error").className = "errMsg";
		}
		catch(err){
			// do nothing
		}		
	}
	else
	{
		document.getElementById(element).className = "blank";
		try{
			document.getElementById(element + "Error").className = "hidden";
		}
		catch(err){
			// do nothing
		}
	}
		
	return valid;
}

function emailCheckTwo()
{
	var valid = "false";
	if (document.getElementById("eMail").value != document.getElementById("eMail2").value)
	{
		document.getElementById("eMail2").className = "err";
		try{
			document.getElementById("eMail2Error").className = "errMsg";
		}
		catch(err){
			// do nothing
		}
	}
	else
	{	
		document.getElementById("eMail2").className = "blank";
		valid = "true";
		try{
			document.getElementById("eMail2Error").className = "hidden";
		}
		catch(err){
			// do nothing
		}
	}
	
	return valid;
}

function stateCheck(element)
{
	var valid = "true";
	if (document.getElementById(element).value == "Select")
		valid = "false";
	
	if (valid == "false")
	{
		document.getElementById(element).className = "err";
		try{
			document.getElementById(element + "Error").className = "errMsg";
		}
		catch(err){
			// do nothing
		}		
	}
	else
	{
		document.getElementById(element).className = "blank";
		try{
			document.getElementById(element + "Error").className = "hidden";
		}
		catch(err){
			// do nothing
		}
	}
		
	return valid;
}

//opens a function window of the element passed to it
function help(page) {
  window.open("help/" + page + ".htm",page,"width=475,height=450");
}


//validates and submits the form, if not valid then an error message is displayed
function submitInfo(action)
{
  var valid = "true";
  var errorMessage = "There are errors in your information. Please check all fields that are red!";
  
	if(basicCheck("compName") == "false")
		valid = "false";			
	if(basicCheck("busiNature") == "false")
		valid = "false";	
	if(stateCheck("civilService") == "false")
		valid = "false";
	if(stateCheck("union") == "false")
		valid = "false";
	if(stateCheck("state") == "false")
		valid = "false";
	if(basicCheck("city") == "false")
		valid = "false";
	if(basicCheck("contName") == "false")
		valid = "false";
	if(basicCheck("fullEmp") == "false")
		valid = "false";
	if(basicCheck("turnOver") == "false")
		valid = "false"
	if(zeroCheck("payFullEmp") == "false")
		valid = "false";
	if(basicCheck("partEmp") == "false")
		valid = "false";
	if(phoneCheck("phone") == "false")
		valid = "false";

	validState = "true";
		
	if(locationCount != 1 || getValue("state0") != "Select" || getValue("taxId0") != "" || getValue("suta0") != "" || getValue("uiNum0") != "" || getValue("numEE") != "")
	{			
		for(i=0; i<locationCount && validState == "true"; i++)
		{
			if(stateCheck("state" + i) == "false")
			{
				validState = "false";
				valid = "false";
				errorMessage = "You must provde a state for all locations in the 'State Unemployment Information' section. (See the 'Payroll Admin' tab)";
			}			
		}
	}
	
	if(emailCheck("eMail") == "true" && valid == "true")
	{
		if (emailCheckTwo() == "false")
		{	
			valid = "false";
			errorMessage = "Your email's must match! (See the 'Company Info' tab.)";
		}
	}
	else
		valid = "false";

  if (valid == "true")
  {
	locWorkForce = 0;
	
	if(locationCount != 1 || getValue("state0") != "Select" || getValue("taxId0") != "" || getValue("suta0") != "" || getValue("uiNum0") != "" || getValue("numEE") != "")
	{
		var check = "true";

		for(i=0; i < locationCount; i++)
		{
			try{
			emp = document.getElementById("numEE"+i).value;}
			catch(err){			
			emp = 0;}
			locWorkForce += parseInt(emp);
		}
	}
	else
	{
		check = "false";
	}
	
	fullEmp = parseInt(document.getElementById("fullEmp").value);
	partEmp = parseInt(document.getElementById("partEmp").value);
	totalEmp = fullEmp + partEmp;
	
	if(!totalEmp)
		totalEmp = 0;
	
	if(locWorkForce == totalEmp || check == "false")
	{
		document.getElementById("submitAction").value = action;
		isValidEmail(document.getElementById("eMail").value);
	}
	else
	{
		alert("Your total number of employees in the the SUTA table (green section) must match your total employees(blue section).");
		document.getElementById("section1").className = "blue";
		document.getElementById("section2").className = "green";
	}
  }
  else
  {
	alert(errorMessage);
  }
}

function getValue(id)
{
	try{
		return document.getElementById(id).value;
	}
	catch(err)
	{
		return "";
	}
}

//expands the ben table
function expandBen()
{
	var data = new Array();
	benCount++;
	
	for(i=0; i < benCount; i++)
	{
		var ben,cont,ren;
		
		
		try{
			ben = document.getElementById("benefit"+i).value;}
		catch(err){			
			ben = "blank";}
			
		try{
			volBen = document.getElementById("volBen"+i).value;}
		catch(err){
			volBen="blank";}
		
		try{
			cont = document.getElementById("contribution"+i).value;}
		catch(err){
			cont="blank";
		}
						
		try{
			ren = document.getElementById("renewal"+i).value;}
		catch(err){	
			ren = "blank";}
		
		if (ren.length < 1)
			ren = "blank";
		if (cont.length < 1)
			cont = "blank";
		if (ben.length < 1)
			ben = "blank";
		if (volBen.length < 1)
			volBen = "blank";
		
		data[i] = new Array(ben,cont,ren,volBen);
	}
	
	writeBenTable(data);	
}

//removes an ben
function removeBen(index)
{
	var data = new Array();
	var position = 0;
	
	for(i=0; i < benCount; i++)
	{
		if(i != index)
		{
				var ben,cont,ren,volBen;
		
		
		try{
			ben = document.getElementById("benefit"+i).value;}
		catch(err){			
			ben = "blank";}
		
		try{
			volBen = document.getElementById("volBen"+i).value;}
		catch(err){
			volBen="blank";
		}
		
		try{
			cont = document.getElementById("contribution"+i).value;}
		catch(err){
			cont="blank";
		}
						
		try{
			ren = document.getElementById("renewal"+i).value;}
		catch(err){	
			remd = "blank";}
		
		if (ren.length < 1)
			ren = "blank";
		if (cont.length < 1)
			cont = "blank";
		if (ben.length < 1)
			ben = "blank";
		if (volBen.length < 1)
			volBen = "blank";
		
			
			data[position] = new Array(ben,cont,ren,volBen);
			position++;
		}
	}
	
	benCount--;
	writeBenTable(data);
}

//writes the ben table when it needs more fields
function writeBenTable(data)
{
	var benTable = document.getElementById("suiTable2");
	var html = "<table><tr><th>Benefit Offered</th><th>Voluntary</th><th>Employer Contribution (% or $)</th><th>Renewal Date</th><th>&nbsp;</th></tr>";
	for(i=0; i < benCount; i++)
	{
		var rowClass = "gBG";
		if(i%2 == 0)
			rowClass = "yBG";	
		
		html += "<tr class='"+rowClass+"'><td>";
		
		var options = new Array("Select",
								"Critical Illness",
								"Dental Insurance",
								"Life Insurance",
								"Dental",
								"Vision",
								"Accident Insurance",
								"Cancer",
								"Heart/Stroke",
								"Long-Term Care",
								"Life Insurance",
								"Disability");
		
		html += "<select id=\"benefit"+i+"\" name=\"benefit"+i+"\">";
		
		for(o=0; o < options.length; o++)
		{
			html += "<option value=\""+options[o]+"\"";
			if(data[i][0] == options[o])
				html += " selected ";
			html += ">" + options[o] + "</option>";
		}
		
		html += "</select><td><select id=\"volBen"+i+"\" name=\"volBen"+i+"\"><option value=\"no\">No</option><option ";
		
		if(data[i][3] == "yes")
			html += "selected ";			
		
		html += "value=\"yes\">Yes</option></select></td><td><input";
		
		if(data[i][1] != "blank")
			html += " value='"+data[i][1]+"'";
		
		html += " type='text' onblur=\"numCheck('contribution"+i+"')\" id='contribution"+i+"' name='contribution"+i+"' /></td><td><input class='small'";
		
		if(data[i][2] != "blank")
			html += " value='"+data[i][2]+"'";
		
		html += " type='text' id='renewal"+i+"' name='renewal"+i+"' /></td>";
		
		if (benCount == 1)
		{
			html += "<td>&nbsp;</td></tr>";
		}
		else
		{
			html += "<td>&nbsp;&nbsp;<a href='javascript: removeBen("+i+")'>remove</a></td></tr>";
		}
	}
	html += "</table><span class='btn' onclick='expandBen()'>add another</span><input type='hidden' id='numBen' name='numBen' value='"+benCount+"' />";
	benTable.innerHTML = html;
}

function changeTab(tab){
	change(activeTab,"off");
	change(activeTab+"Tab","");
	change(tab,"on");
	change(tab+"Tab","active");
	activeTab = tab;
}

function zeroCheck(element){
	numCheck(element);
	var valid = basicCheck(element);
	
	if (parseInt(document.getElementById(element).value) <= 0)
	{
		document.getElementById(element).className = "err";
		try{
			document.getElementById(element + "Error").className = "errMsg";
		}
		catch(err){
			// do nothing
		}
		valid = "false";		
	}
	
	return valid;

}

function isValidEmail(email){
	ajaxConn = new XHConn();
	if(!ajaxConn) return;
	ajaxConn.abort;
	var fnWhenDone = function(oXML) {
		//alert(oXML.responseText);
		if(oXML.responseText == "true"){
			document.info.submit();
		}
		else{
			alert("The email is already in use.");
		}
	};
	ajaxConn.connect('emailTest.asp','GET','email='+email+'&id='+document.info.cid.value, fnWhenDone);
}
