document.onreadystatechange = DoReload;

function DoReload()
{
	if(document.readyState == "complete")
	{
		var vBioReader = document.getElementById("No_Bio_Readers");
		var vCardHolders = document.getElementById("No_CardHolders");
		var vCardType = document.getElementById("Card_Type");
		var vOpt1 = document.getElementById("opt1");
		var vOpt2 = document.getElementById("opt2");
		var vOpt3 = document.getElementById("opt3");

		vCardType.selectedIndex = 0;
		vBioReader.value = "1";
		vOpt1.checked = false;
		vOpt2.checked = false;
		vOpt3.checked = false;

		ResetForm();
	}
}

function ResetForm()
{
	var vForm = document.getElementById("form1");
	vForm.reset();
	OnChangeCardType();

	var vOKImgSrc = "/system/modules/uk.co.tssi.website/resources/images/Information.gif";
	var vBioReaderStat = document.getElementById("helpbio");
	var vCardHolderStat = document.getElementById("helpholder");
	var vCardTypeStat = document.getElementById("helpcard");
	var vConnectTypeStat = document.getElementById("helpconnect");

	vBioReaderStat.src = vCardHolderStat.src = vCardTypeStat.src = vConnectTypeStat.src = vOKImgSrc;
}

function OnChangeCardType()
{
	var vTemplate_Loc = document.getElementById("Template_Loc");
	var vReader_Req = document.getElementById("Reader_Req");
	var vCardType = document.getElementById("Card_Type");
	ClearCombo(vTemplate_Loc);
	ClearCombo(vReader_Req);

	switch(vCardType.options.item(vCardType.selectedIndex).value)
	{
	case "blank":
		AddCombo(vTemplate_Loc, ".. select ID card first ..", "blank", true);
		AddCombo(vReader_Req, ".. select ID card first ..", "blank", true);
		break;
	case "pin":
		AddCombo(vTemplate_Loc, "In the Verid itself", "verid", true);
		AddCombo(vReader_Req, ".. please select one ..", "blank", true);
		AddCombo(vReader_Req, "No - I've got a PIN reader", "none", false);
		AddCombo(vReader_Req, "Yes - integrated into the Verid", "pin", false);
		break;
	case "swipe":
		AddCombo(vTemplate_Loc, ".. please select one ..", "blank", true);
		AddCombo(vTemplate_Loc, "On the magnetic stripe", "mag", false);
		AddCombo(vTemplate_Loc, "In the Verid itself", "verid", false);
		AddCombo(vReader_Req, "No - I've got a swipe reader", "mag", true);
		break;
	case "prox":
		AddCombo(vTemplate_Loc, "In the Verid itself", "verid", true);
		AddCombo(vReader_Req, ".. please select one ..", "blank", true);
		AddCombo(vReader_Req, "No - I've got a prox reader", "none", false);
		AddCombo(vReader_Req, "Yes - integrated into Verid", "prox", false);
		break;
	case "iclass":
		AddCombo(vTemplate_Loc, ".. please select one ..", "blank", true);
		AddCombo(vTemplate_Loc, "In the HiD iclass card", "iclass", false);
		AddCombo(vTemplate_Loc, "In the Verid itself", "verid", false);
		AddCombo(vReader_Req, ".. select ID card first ..", "blank", true);
		break;
        case "mifare":
		AddCombo(vTemplate_Loc, ".. please select one ..", "blank", true);
		AddCombo(vTemplate_Loc, "In the Mifare card", "mifare", false);
		AddCombo(vTemplate_Loc, "In the Verid itself", "verid", false);
                AddCombo(vReader_Req, ".. select ID card ..", "blank", true);
		break;
	}
}

function OnChangeTemplateLoc()
{
	var vTemplate_Loc = document.getElementById("Template_Loc");
	var vReader_Req = document.getElementById("Reader_Req");
	var vCardType = document.getElementById("Card_Type");
	ClearCombo(vReader_Req);

        switch(vCardType.options.item(vCardType.selectedIndex).value)
	{
	case "blank":
		AddCombo(vReader_Req, ".. select ID card first ..", "blank", true);
		break;
	case "pin":
		AddCombo(vReader_Req, ".. please select one ..", "blank", true);
		AddCombo(vReader_Req, "No - I've got a PIN reader", "none", false);
		AddCombo(vReader_Req, "Yes - integrated into the Verid", "pin", false);
		break;
	case "swipe":
		AddCombo(vReader_Req, "No - I've got a swipe reader", "none", true);
		break;
	case "prox":
		AddCombo(vReader_Req, ".. please select one ..", "blank", true);
		AddCombo(vReader_Req, "No - I've got a prox reader", "none", false);
		AddCombo(vReader_Req, "Yes - integrated into Verid", "prox", false);
		break;
	case "iclass":
		AddCombo(vReader_Req, ".. please select one ..", "blank", true);
		AddCombo(vReader_Req, "No - I've got an iClass reader", "none", false);
		AddCombo(vReader_Req, "Yes - integrated into Verid", "iclass", false);
		break;
        case "mifare":
	        AddCombo(vReader_Req, ".. please select one ..", "blank", true);
		AddCombo(vReader_Req, "No - I've got a Mifare reader", "none", false);
		AddCombo(vReader_Req, "Yes - integrated into Verid", "mifare", false);
		break;
	}

	switch(vTemplate_Loc.options.item(vTemplate_Loc.selectedIndex).value)
	{
	case "iclass":
                ClearCombo(vReader_Req);
		AddCombo(vReader_Req, "Yes - integrated into Verid", "iclass", false);
		break;
        case "mifare":
                ClearCombo(vReader_Req);
		AddCombo(vReader_Req, "Yes - integrated into Verid", "mifare", false);
		break;
	}
}

function validate()
{
	var vMsg = "The following errors prevent you from submitting this form.  Please correct them and retry.\r\n\r\n";
	var vErrors = "";

	var vBioReader = document.getElementById("No_Bio_Readers");
	var vCardHolders = document.getElementById("No_CardHolders");
	var vCardType = document.getElementById("Card_Type");
	var vConnectType = document.getElementById("Connect_By");

	var vOKImgSrc = "/system/modules/uk.co.tssi.website/resources/images/Information.gif";
	var vBioReaderStat = document.getElementById("helpbio");
	var vCardHolderStat = document.getElementById("helpholder");
	var vCardTypeStat = document.getElementById("helpcard");
	var vConnectTypeStat = document.getElementById("helpconnect");

	vBioReaderStat.src = vCardHolderStat.src = vCardTypeStat.src = vConnectTypeStat.src = vOKImgSrc;

	vErrors += validateMandatory(vBioReader, "Biometric readers", vBioReaderStat);
	vErrors += validateInt(vBioReader, "Biometric readers", vBioReaderStat);
	vErrors += validateMaxMinLimit(vBioReader, "Biometric readers", 1, 1000, vBioReaderStat);

	vErrors += validateMandatory(vCardHolders, "Number of users", vCardHolderStat);
	vErrors += validateInt(vCardHolders, "Number of users", vCardHolderStat);
	vErrors += validateMaxMinLimit(vCardHolders, "Number of users", 1, 5000, vCardHolderStat);

	vErrors += validateNotBlank(vCardType, "whether you'll use ID cards", vCardTypeStat);

	vErrors += validateNotBlank(vConnectType, "a method of connecting to Verid", vConnectTypeStat);


	if(vErrors.length > 0)
	{
		alert(vMsg + vErrors);
		return false;
	}

	//check for warnings
	vMsg = "The following warning(s) have been identified.  Are you sure?\r\n\r\n";

	if(vErrors.length > 0)
	{
		return window.confirm(vMsg + vErrors);
	}

	return true;
}

function validateMandatory(vEdit, vName, vIcon)
{
	var vMsg = "";

	if(vEdit.value == "")
        {
                vMsg = vName + " is a mandatory field.\r\n";
		if(vIcon != null)
		{
			vIcon.src = "/system/modules/uk.co.tssi.website/resources/images/alert1.gif";
		}
	}

	return vMsg;
}

function validateInt(vEdit, vName, vIcon)
{
	var vMsg = "";

	if(isNaN(parseInt(vEdit.value)))
	{
                vMsg = "Please enter a whole number for the " + vName + ".\r\n";
		if(vIcon != null)
		{
			vIcon.src = "/system/modules/uk.co.tssi.website/resources/images/alert1.gif";
		}
	}
	else if(vEdit.value != parseInt(vEdit.value))
	{
                vMsg = "Please enter a whole number for the " + vName + ".\r\n";
		if(vIcon != null)
		{
			vIcon.src = "/system/modules/uk.co.tssi.website/resources/images/alert1.gif";
		}
	}

	return vMsg;
}

function validateMinLimit(vEdit, vName, vMin, vIcon)
{
	var vMsg = "";

	vMin = parseInt(vMin);

	if(vEdit.value < vMin)
	{
		vMsg = vName + " should be greater or equal to " + vMin + ".\r\n";
		if(vIcon != null)
		{
			vIcon.src = "/system/modules/uk.co.tssi.website/resources/images/alert1.gif";
		}
	}

	return vMsg;
}

function validateMaxMinLimit(vEdit, vName, vMin, vMax, vIcon)
{
	var vMsg = "";

	vMin = parseInt(vMin);
	vMax = parseInt(vMax);

	if(vEdit.value < vMin || vEdit.value > vMax)
	{
		vMsg = vName + " should be between " + vMin + " and " + vMax + ".\r\n";
		if(vIcon != null)
		{
			vIcon.src = "/system/modules/uk.co.tssi.website/resources/images/alert1.gif";
		}
	}

	return vMsg;
}

function validateNotBlank(vCombo, vName, vIcon)
{
	var vMsg = "";
	
	if(vCombo.value == "blank")
	{
		vMsg = "Please select " + vName + ".\r\n";
		if(vIcon != null)
		{
			vIcon.src = "/system/modules/uk.co.tssi.website/resources/images/alert1.gif";
		}
	}

	return vMsg;
}

function ClearCombo(vCombo)
{
	if(vCombo.options.remove != null)
	{
		while(vCombo.options.length > 0)
		{
			vCombo.options.remove(0);
		}
	}
	else
	{
		vCombo.innerHTML = "";
	}
}

function AddCombo(vCombo, vText, vValue, vSelected)
{
	if(vCombo.options.remove != null)
	{
		var vOption = document.createElement("OPTION");
		vCombo.options.add(vOption);
		vOption.innerText = vText;
		vOption.value = vValue;
		vOption.selected = vSelected;
	}
	else
	{
		var vHTML = "<OPTION value='";
		vHTML += vValue;
		vHTML += "' ";
		if(vSelected)
		{
			vHTML += "selected ";
		}
		vHTML += ">";
		vHTML += vText;
		vHTML += "</OPTION>";
		vCombo.innerHTML += vHTML;
	}
}

