// JavaScript Document

function isChecked(object) {
    if (object.checked) return true;
    else return false;
}

function changeDiv(the_div,the_change,object) {
	if(isChecked(object)) {
		var the_style = getStyleObject(the_div);
		//var the_parent = getStyleObject(the_div+"_parent");
  		if (the_style != false) {
    		the_style.display = the_change;
  		}
	} else {
		var the_style = getStyleObject(the_div);
		the_style.display = 'none';
	}
/*
	if ( the_parent != false )
	{
		the_parent.display = 'block';
	}

	else
	{
		var the_parent = getStyleObject(the_div+"_parent");
		the_parent.display = 'none';
	} */
}

function switchIfDone(the_form, this_div, next_div)
{

	var complete = true;

  if ((complete == true) && (next_div == "finished")) {
    submitTheInfo();
  } 
  else if (complete == true) {
  	if(this_div = 'part1') {
  		alert(document.tax_status.elements);
  		SetCookie('gross_income', document.tax_status.gross_income.value, 1 );
  		SetCookie('filingStatus', document.tax_status.filingStatus.checked, 1 );
  		SetCookie('Dependents', document.tax_status.Dependents.checked, 1 );
  		
  	}
  	switchDiv(this_div, next_div);
  } else {
    alert('please complete the form before moving on');
  }
}

function switchDiv(this_div, next_div)
{
  if (getStyleObject(this_div) && getStyleObject(next_div)) {
    changeObjectVisibility(this_div, "hidden");
    changeObjectVisibility(next_div, "visible");
  }
}

function submitTheInfo()
{
  var submission_string="";
  for (var form_loop=0; form_loop<document.forms.length; form_loop++) 
  {
    for (var elems=0; elems<document.forms[form_loop].length;elems++)
    {
      if (document.forms[form_loop].elements[elems].name != "")
      {
        submission_string += document.forms[form_loop].name + "_" +
          document.forms[form_loop].elements[elems].name + "=" +
          document.forms[form_loop].elements[elems].value + "\n";
      }
    }
  }
  document.hiddenform.the_text.value = submission_string;

  // the next two lines are written for debugging - 
  // to put the script into action
  // comment out the changeObjectVisibility() line
  // and uncomment the document.hidden.form.submit() line
  //

  //document.hiddenform.submit(); 
  changeObjectVisibility("hiddenstuff","visible");
}

function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
	return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
	return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
	return document.layers[objectId];
	
    } else {
	return false;
    }
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.visibility = newVisibility;
	return true;
    } else {
	// we couldn't find the object, so we can't change its visibility

	return false;
    }
} // changeObjectVisibility

function moveObject(objectId, newXCoordinate, newYCoordinate) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.left = newXCoordinate;
	styleObject.top = newYCoordinate;
	return true;
    } else {
	// we couldn't find the object, so we can't very well move it
	return false;
    }
} // moveObject

function valbutton(thisform,whichRadio) {
// place any other field validations that you require here
// validate myradiobuttons
	myOption = -1;
	for (i=thisform.elements[whichRadio].length-1; i > -1; i--) {
		if (thisform.elements[whichRadio[i]].checked) {
			myOption = i;
		}
	}
	if (myOption == -1) {
		alert("You must select a radio button");
		return false;
	}

	alert("You selected button number " + myOption + " which has a value of " + thisform.whichRadio[myOption].value);

// place any other field validations that you require here
thisform.submit(); // this line submits the form after validation
}


function calculate(num_field,cost_field,total_field) {
	var num_visits = num_field;
	var cost_visit = cost_field;
	var total_visit = total_field;
		document.getElementById(total_visit).value = document.getElementById(num_visits).value * document.getElementById(cost_visit).value;
		return true;
}

function setvalue(cost_field,total_field) {
	var cost_visit = cost_field;
	var total_visit = total_field;
		document.getElementById(total_visit).value = document.getElementById(cost_visit).value;
		return true;
}

function setMonthlyValue(cost_field,total_field) {
	var cost_visit = cost_field;
	var total_visit = total_field;
		document.getElementById(total_visit).value = (document.getElementById(cost_visit).value) * 12;
		return true;
}

function readTheCookie(the_info) {

	// load the cookie into a variable and unescape it
	var the_cookie = document.cookie;
	var the_cookie = unescape(the_cookie);	
	// separate the values from the cookie name
	var broken_cookie = the_cookie.split("=");
	var the_values = broken_cookie[1];

	// break each name:value pair into an array
	var separated_values = the_values.split("/");

	// loop through the list of name:values and load
	// up the associate array
	var property_value = "";
	for (var loop = 0; loop < separated_values.length; loop++)
	{
		property_value = separated_values[loop];
		var broken_info = property_value.split(":");
		var the_property = broken_info[0];
		var the_value = broken_info[1];
		the_info[the_property] = the_value;
	}
}

function readCookie ( name )
{
	// is a cookie present
	if ( document.cookie )
	{
		// split up the cookie
		var theCookies = document.cookie.split(';');

		// current cookie we're looking for
		var cookieName = name + '=';

		// loop through the cookies
		for ( var i = 0; i < theCookies.length; i++ )
		{
			// setup the current cookie
			var theCookie = theCookies[i];

			// check for and get rid of leading spaces
			while ( theCookie.charAt(0) == ' ' )
			{
				// get rid of it by just redefining theCookie
				var theCookie = theCookie.substr(1, theCookie.length);
			}

			// is it the cookie we're looking for
			if ( theCookie.indexOf(cookieName) == 0 )
			{
				// return, because it be he that we search for
				return unescape(theCookie.substr(cookieName.length, theCookie.length));
			}
		}
	}
}

function parseCookie ( cookieString )
{
	// our big array
	var retArr = new Array();

	// make sure the cookie has data
	if ( cookieString == undefined )
	{
		return(false);
	}

	// split the string
	cookieString = cookieString.split('/');

	// and loop through
	for ( var i = 0; i < cookieString.length; i++ )
	{
		// split it again
		var tmp = cookieString[i].split(':');

		// and put it into the array
		retArr[tmp[0]] = tmp[1];
	}

	// debug
	//for ( aKey in retArr )
	//{
		// debug
	//	alert ( 'property: ' + aKey + '\nvalue: ' + retArr[aKey] );
	//}

	// and return
	return retArr;
}

function cookieVal ( cookieName )
{
	// get the cookie values
	var thisCookie = readCookie(cookieName);

	// now split it up
	thisCookie = thisCookie.split('=');

	// return value initialized
	var retVal = new Array();
	retVal['name'] = thisCookie[0];
	retVal['data'] = thisCookie[1];

	// and parse the data
	var retVal = parseCookie(retVal['data']);

	// and return
	return retVal;
}

function numeralsOnly(evt) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : 
        ((evt.which) ? evt.which : 0));
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        alert("Enter numerals only in this field.");
        return false;
    }
    return true;
}



function checkAllCookies() {
	var all_ck = new Array("medical_cookie", "prescription_cookie", "dental_cookie", "eyecare_cookie", "dependent_cookie");
	var all_cookie_values = new Array();

	for (var arr_loop = 0; arr_loop < 4; arr_loop++) {

		arr_cookies = unescape(cookieVal(all_ck[arr_loop]));
		

 		var the_values = arr_cookies;
 		
		// break each name:value pair into an array
		var separated_values = the_values.split("/");
		// loop through the list of name:values and load
		// up the associate array
		var property_value = "";
		for (var loop = 0; loop < separated_values.length; loop++)	{
			property_value = separated_values[loop];
			var broken_info = property_value.split(":");
			var the_property = broken_info[0];
			var the_value = broken_info[1];
			all_cookie_values[the_property] = the_value;
			//alert(all_cookie_values[loop]);
				}
	}
}

function outputComma ( n )
{
	var arr = new Array('0'), i = 0;

	while ( n > 0 )
	{
		arr[i] = '' + (n % 1000);
		n = Math.floor(n/1000);
		i++;
	}

	arr = arr.reverse();

	for ( var i in arr )
	{
		if ( i > 0 )
		{
			while ( arr[i].length < 3 )
			{
				arr[i] = '0' + arr[i];
			}
		}
	}

	num = arr.join();
	myNum = num.split(',');
	decNum = myNum[myNum.length - 1];
	decNum = (decNum == Math.floor(decNum)) ? decNum + "" : ((decNum * 10 == Math.floor(decNum*10)) ? decNum.substr(0, decNum.length() - 1) : decNum);
	myNum[myNum.length - 1] = decNum;
	return(myNum.join());
}

function clearContents ( cookie )
{
	document.cookie = cookie + "=;";
	loadValue();
}

function clearAllContents()
{

	document.cookie = "medical_cookie=;";
	document.cookie = "prescription_cookie=;";
	document.cookie = "eyecare_cookie=;";
	document.cookie = "dental_cookie=;";
	document.cookie = "dependent_cookie=;";
}