var iErrorCount;
var sErrorMessage;
var oCorrect = new Image();
var oError = new Image();
var oHelp = new Image();
var oRequired = new Image();
var F = document.forms[0];
var bClosingWindow = true;
var bAppSaved = false;

oCorrect.src = "_images/nav/btn_correct.gif";
oError.src = "_images/nav/btn_error.gif";
oHelp.src = "_images/nav/btn_info.gif";
oRequired.src = "_images/nav/btn_info_req.gif";

sErrorMessage = ") Merchant Application Errors Found --\n\n";
sErrorMessage += "Data is either missing or incorrect.\n\n";
sErrorMessage += "Please find the fields marked with a red 'X' and make\n";
sErrorMessage += "the necessary corrections.  Clicking on the red 'X' will\n";
sErrorMessage += "provide you with information on each error.";
//==============================================================================
function CheckForSaveBeforeExit() {
	if (bClosingWindow == true && bAppSaved == false) {
		var sQuestion
		sQuestion = "You are about to exit the application process and your ";
		sQuestion += "application has not been saved.\nBy doing this you will ";
		sQuestion += "lose any unsaved data.\n\nDo you wish to continue?";
		
		if (confirm(sQuestion)) {
			location.href = "save.asp";
		}
	}
}
//==============================================================================
function SetFees(sProduct, iGateway, iGatewayPI, iSetup) {
	var oF = document.forms[0];
	
	oF.product.value = sProduct;
	if (document.forms[0].x_path.value == "moto") {
		oF.gateway.value = iGateway;
		oF.gateway_pi.value = iGatewayPI;
		oF.setup.value = iSetup;
	}
	
} 
//==============================================================================
function LaunchHelp(sField, sId) {
	var sUrl = "help.asp?field=" + sField + "&error=" + sId;
	var oWin = window.open(sUrl, "help", "width=415,height=275,scrollbars");
	oWin.focus();
}
//==============================================================================
function LaunchApp(i) {
	var oApp = window.open("main.asp?page=" + i, "App", "width=640,height=450,resizable,scrollbars");
	oApp.focus();
}
//==============================================================================
function NewWindow(sLocation, sName, iWidth, iHeight) {
	if (iWidth == null || iWidth == "") {iWidth = '540'}
	if (iHeight == null || iHeight == "") {iHeight = '400'}
	if (sName == null) {sName = 'popup'}
	var oWin = window.open(sLocation, sName, "top=32,left=32,scrollbars,resizable,width=" + iWidth + ",height=" + iHeight);
	oWin.focus();
}
//==============================================================================
function CheckRequirements() {
	var F = document.forms[0];
	if (F.chk1.checked == 1 && F.chk2.checked == 1) {
		LaunchApp(0);
	} else {
		alert("You must meet all the requirements in order to continue.\n\nIf you do meet the requirements, please indicate so by\nchecking the proper boxes below.");
	}
}
//==============================================================================
function AppBack(iPrevPage) {
	//var iPrevPage = parseInt(document.forms[0].page.value) - 1;
	//var iPrevPage = parseInt(document.forms[0].x_page.value);
	if (iPrevPage >= 0) {
	//	if (document.forms[0].page.value == "3" && document.forms[0].path.value != "retail") {
	//		iPrevPage = 1;
	//	} else {
		document.forms[0].action = 'main.asp?page=' + iPrevPage; 
		document.forms[0].submit();
	//	}
	} else {
		alert("You are at the begining of the application.\n\nYou can not go back any further.");
	}
}
//==============================================================================
function AppNext() {
	//perform errorchecking
	ErrorCheckForm(); 

	//tell the user if there were any errors -- if not submit form
	if (iErrorCount > 0) {
		if (iErrorCount != 99) { alert("-- (" + iErrorCount + sErrorMessage) }
	} else {
		document.forms[0].submit();
	}
}
//==============================================================================
function VerifyEmail() {
	var sValue = document.forms[0].email.value
	var iPos1 = sValue.indexOf("@")
	var iPos2 = sValue.indexOf(".", iPos1)
	
	if (iPos1 == -1 ||  iPos2 == -1 || iPos2 < iPos1) {
		alert("ERROR:\n\nPlease enter a valid email address!");
		return false;
	}
}
//==============================================================================
function SetAddress(i) {
	var F = document.forms[0];
	if (i == 1) {
		F.biz_mail_address.value = F.biz_loc_address.value;
		F.biz_mail_city.value = F.biz_loc_city.value;
		F.biz_mail_state.options[F.biz_loc_state.selectedIndex].selected = true;
		F.biz_mail_zip.value = F.biz_loc_zip.value;
	} else {
		F.biz_mail_address.value = "";
		F.biz_mail_city.value = "";
		F.biz_mail_state.options[0].selected = true;
		F.biz_mail_zip.value = "";
	}
}
//==============================================================================
function SetError(i, sErrMsg) {
	var oImages = document.images;
	var oLinks = document.links;
	var sTemp;
	var sHref;
	var iPos;
	
	
	//set error indicators
	oImages[i + 6].src = oError.src;  //i + 5 to offset the two header grfx
	
	sHref = unescape(oLinks[i + 2].href);
	//alert(sHref);
	iPos = sHref.indexOf("'", 24);
	//alert(sHref.slice(0, 24));
	//alert(iPos);
	
	if (iPos >= 0) { sTemp = sHref.slice(24, iPos) }
	//alert(sTemp);
	
	oLinks[i + 2].href = "javascript: LaunchHelp('" + sTemp + "', '" + sErrMsg + "');";
	
	iErrorCount++;
}
//==============================================================================
function SetCorrect(i) {
	var oImages = document.images;
	var oLinks = document.links;
	var sTemp;
	var sHref;
	var iPos;
	
	//set correct indicators
	oImages[i + 6].src = oCorrect.src;  //i + 5 to offset the two header grfx
	
	sHref = unescape(oLinks[i + 2].href);
	//alert(sHref);
	iPos = sHref.indexOf("'", 24);
	//alert(sHref.slice(0, 24));
	//alert(iPos);
	
	if (iPos >= 0) { sTemp = sHref.slice(24, iPos) }
	//alert(sTemp);
	
	oLinks[i + 2].href = "javascript: LaunchHelp('" + sTemp + "', '');";
}
//==============================================================================
//TEMPLATE ONLY
function ErrorCheckFormTemplate() {
	var oF = document.forms[0]
	
	iErrorCount = 0; //initailize error count to zero
	
	//START ERRORCHECKING ------------------------------------------------------
	
	//END ERRORCHECKING --------------------------------------------------------
	
	//set all non-errorchecked fields correct
	

}
//==============================================================================
function SetRequired(iSwitch) {
	var oImages = document.images;
	
	for(i=13 ; i <= 15  ; i++) {
		if (iSwitch) {
			oImages[i].src = oRequired.src
		} else {
			oImages[i].src = oHelp.src
		}
	}
}