var How;
function setHow(theHow) {
	How = theHow;
}

function showIt(whichLayer) {
	var elem, vis; 
	if( document.getElementById ) // this is the way the standards work
		elem = document.getElementById( whichLayer );
	else if( document.all ) // this is the way old msie versions work
		elem = document.all[whichLayer];
	else if( document.layers ) // this is the way nn4 works
		elem = document.layers[whichLayer];
	vis = elem.style;  // if the style.display value is blank we try to figure it out here
	vis.display = 'block';
}
function hideIt(whichLayer) {
	var elem, vis; 
	if( document.getElementById ) // this is the way the standards work
		elem = document.getElementById( whichLayer );
	else if( document.all ) // this is the way old msie versions work
		elem = document.all[whichLayer];
	else if( document.layers ) // this is the way nn4 works
		elem = document.layers[whichLayer];
	vis = elem.style;  // if the style.display value is blank we try to figure it out here
	vis.display = 'none';
}

function validateInfo4(theType,theID) {
	var phone;
	var email;
	var interest;
	var name = Validation.validate(theType+"_Name_"+theID);
	var street = Validation.validate(theType+"_Street_"+theID);
	var city = Validation.validate(theType+"_City_"+theID);
	var state = Validation.validate(theType+"_State_"+theID);
	var zip = Validation.validate(theType+"_Zip_"+theID);

	if ('man' == How && theType.indexOf('GRANTEE') == -1  ) {
		phone = Validation.validate(theType+"_Phone_"+theID);
		email = Validation.validate(theType+"_Email_"+theID);
	} else {
		phone = true;
		email = true;
	}

	if ( theType.indexOf('GRANTEE') > -1 ){
		interest = Validation.validate(theType+"_Int_"+theID);
	} else {
		interest = true;
	}

	if (name && street && city && state && zip && phone && email && interest) {
		return true;
	}
	return false;
}

function interestCheck(theType, theLatest) {
	var val = 0;
	for (i = 1; i <= theLatest; i++) {
		val = val + parseInt(getRawObject(theType+"_Int_"+i).value);
	}
	if (val != 100) {
		alert('The total percentage of all beneficiaries must equal exactly 100');
		return false;
	}
	return true;
}

var currentStep;
var highGrantor = "";
var highPRI = "";
var highSUC = "";
function showStep(step) {
//	alert("Step: "+step+"\nCurrent Step: "+currentStep);
// ---------------------------------------------------
	if (step == "Instructions") {
			hideIt("Part1");
			showIt("instructions");
			currentStep = 'instructions';
	}
// ---------------------------------------------------
	if (step == "Part1") {
		hideIt("instructions");
		hideIt("Part2");
		showIt("Part1");
		if (highGrantor != "") {
			currentStep = highGrantor;
		} else {
			currentStep = 'Part1';
		}
	}

	if (step == "grantor2") {
		if (validateInfo4('Grantor','1') ) {
			showIt("grantor2");
			hide("GaddButton");
			currentStep = 'grantor2';
			highGrantor = currentStep;
		}
	}
// ---------------------------------------------------
	if (step == "Part2") {
		if (validateInfo4('Grantor','1') ) {
			if (currentStep == 'grantor2') {
				if (validateInfo4('Grantor','2') ) {
					hideIt("Part1");
					hideIt("Part3");
					showIt("Part2");
					currentStep = 'Part2';
					if (highPRI != "") {
						currentStep = highPRI;
					}
				}
			} else {
				show("PaddButton");
				hideIt("Part1");
				hideIt("Part3");
				showIt("Part2");
				currentStep = 'Part2';
				if (highPRI != "") {
					currentStep = highPRI;
				}
			}				
		}
	}
// ---------------------------------------------------
	if (step == "_Part2_") {
		if (highPRI != "") {
			currentStep = highPRI;
		} else {
			currentStep = 'Part2';
		}
		hideIt("Part1");
		hideIt("Part3");
		showIt("Part2");
	}
// ---------------------------------------------------
	if (step == "nextPri") {
		if (currentStep == 'prigrantee4') {
			if (validateInfo4('PRIGRANTEE','4') ) {
				currentStep = 'prigrantee5'; 
				hide('PaddButton');
			}
		}
		if (currentStep == 'prigrantee3') {
			if (validateInfo4('PRIGRANTEE','3') ) {
				currentStep = 'prigrantee4';
			}
		}
		if (currentStep == 'prigrantee2') {
			if (validateInfo4('PRIGRANTEE','2') ) {
				currentStep = 'prigrantee3';
			}
		}
		if (currentStep == 'grantor2' || currentStep == 'Part2') {
			if (validateInfo4('PRIGRANTEE','1') ) {
				currentStep = 'prigrantee2';
			}
		}
		highPRI = currentStep;
		showIt(currentStep);
	}
// ---------------------------------------------------
	if (step == "Part3") {
		if (currentStep == 'Part2') {
			if (validateInfo4('PRIGRANTEE','1') ) {
				if (interestCheck('PRIGRANTEE','1')) {
					hideIt("Part2");
					showIt("Part3");
					currentStep = 'Part3';
					if (highSUC != "") {
						currentStep = highSUC;
					}
				}
			}
		}
		if (currentStep == 'prigrantee2') {
			if (validateInfo4('PRIGRANTEE','2') ) {
				if (interestCheck('PRIGRANTEE','2')) {
					hideIt("Part2");
					showIt("Part3");
					currentStep = 'Part3';
					highPRI = 'prigrantee2';
					if (highSUC != "") {
						currentStep = highSUC;
					}
				}
			}
		}
		if (currentStep == 'prigrantee3') {
			if (validateInfo4('PRIGRANTEE','3') ) {
				if (interestCheck('PRIGRANTEE','3')) {
					hideIt("Part2");
					showIt("Part3");
					currentStep = 'Part3';
					highPRI = 'prigrantee3';
					if (highSUC != "") {
						currentStep = highSUC;
					}
				}
			}
		}
		if (currentStep == 'prigrantee4') {
			if (validateInfo4('PRIGRANTEE','4') ) {
				if (interestCheck('PRIGRANTEE','4')) {
					hideIt("Part2");
					showIt("Part3");
					currentStep = 'Part3';
					highPRI = 'prigrantee4';
					if (highSUC != "") {
						currentStep = highSUC;
					}
				}
			}
		}
		if (currentStep == 'prigrantee5') {
			if (validateInfo4('PRIGRANTEE','5') ) {
				if (interestCheck('PRIGRANTEE','5')) {
					hideIt("Part2");
					showIt("Part3");
					currentStep = 'Part3';
					highPRI = 'prigrantee5';
					if (highSUC != "") {
						currentStep = highSUC;
					}
				}
			}
		}
	}
// ---------------------------------------------------
	if (step == "_Part3_") {
		if (highSUC != "") {
			currentStep = highSUC;
		} else {
			currentStep = 'Part3';
		}
		hideIt("Part2");
		hideIt("Part4");
		showIt("Part3");
	}
// ---------------------------------------------------
	if (step == "nextSuc") {
		if (currentStep == 'sucgrantee3') {
			if (validateInfo4('SUCGRANTEE','3') ) {
				currentStep = 'sucgrantee4';
				hide('SaddButton');
			}
		}
		if (currentStep == 'sucgrantee2') {
			if (validateInfo4('SUCGRANTEE','2') ) {
				currentStep = 'sucgrantee3';
			}
		}
		if (currentStep == 'sucgrantee1') {
			if (validateInfo4('SUCGRANTEE','1') ) {
				currentStep = 'sucgrantee2';
			}
		}
		if (currentStep == 'Part3') {
			currentStep = 'sucgrantee1';
			hideIt('sucStart');
		}
		highSUC = currentStep; 
		showIt(currentStep);
	}
// ---------------------------------------------------
	if (step == "Part4") {
		if (currentStep == 'Part3') {
			hideIt("Part3"); showIt("Part4"); currentStep = 'Part4';
		}
		if (currentStep == 'sucgrantee1') {
			if (validateInfo4('SUCGRANTEE','1') ) {
				if (interestCheck('SUCGRANTEE','1')) {
					hideIt("Part3"); showIt("Part4"); currentStep = 'Part4';
					highSUC = 'sucgrantee1';
					currentStep = 'Part4';
				}
			}
		}
		if (currentStep == 'sucgrantee2') {
			if (validateInfo4('SUCGRANTEE','2') ) {
				if (interestCheck('SUCGRANTEE','2')) {
					hideIt("Part3"); showIt("Part4"); currentStep = 'Part4';
					highSUC = 'sucgrantee2';
					currentStep = 'Part4';
				}
			}
		}
		if (currentStep == 'sucgrantee3') {
			if (validateInfo4('SUCGRANTEE','3') ) {
				if (interestCheck('SUCGRANTEE','3')) {
					hideIt("Part3"); showIt("Part4"); currentStep = 'Part4';
					highSUC = 'sucgrantee3';
					currentStep = 'Part4';
				}
			}
		}
		if (currentStep == 'sucgrantee4') {
			if (validateInfo4('SUCGRANTEE','4') ) {
				if (interestCheck('SUCGRANTEE','4')) {
					hideIt("Part3"); showIt("Part4"); currentStep = 'Part4';
					highSUC = 'sucgrantee4';
					currentStep = 'Part4';
				}
			}
		}
	}
// ---------------------------------------------------
	if (step == "_Part4_") {
		hideIt("Part3");
		hideIt("Part5");
		showIt("Part4");
	}
// ---------------------------------------------------
	if (step == "Part5") {
		var county = Validation.validate('COUNTY');
		var legalDesc =  Validation.validate('LEGALDSC');
		var propAddress = Validation.validate('PROPADDRESS');
		var email;
		
		if ('man' == How ) {
			email = true;
		} else {
			email = Validation.validate('EMAIL');
		}

		if ( county && legalDesc && propAddress && email) {
			hideIt("Part4");
			showIt("Part5");
			currentStep = 'Part5';
		}
	}
// ---------------------------------------------------
}

function makePOSTRequest(url, parameters) {
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
			
	http_request.onreadystatechange = ResultOfInterestCheck;

	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}


function ResultOfInterestCheck () {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			result = http_request.responseText;
//alert(result);
			if (result != "GOOD") {
				alert(result);
			} else {
				document._xclick.submit();
			}
		} else {
			alert('There was a problem verifying the Interest amounts.');
		}
	}
}


