//Storage for selected catagory expansion
var selectedContentArray = new Array();

/*
Function to load research menu
*/
function loadContentMenu(id){
	//alert("loadContentMenu");
	var page = "content/staticcontentlinks.asp";
	if (id){
		editSelectedContentList(id);
	}
	//Set expandable
	var data = "expandid=" + selectedContentArray.toString();
	genericSubmit(page,data,null,'menucontent',completedSingleLoad,regularloadingstring,false);
}

/*
Function for expansion and shrinking of menu (via selection array)
*/

function editSelectedContentList(idval){
	
	var isSelected = false;
	for (var i = 0; i < selectedContentArray.length;i++){	
		if (selectedContentArray[i] == idval) {
			selectedContentArray[i] = null;
			var arstring = selectedContentArray.toString();
			var regex = /\,\,/g;
			arstring = arstring.replace(regex,",")
			if (arstring.charAt(0) == ",") {arstring = arstring.substr(1,arstring.length - 1);}
			if (arstring.charAt(arstring.length - 1) == ",") {arstring = arstring.substr(0,arstring.length - 1);}
			if (arstring != "") {
				selectedContentArray = arstring.split(",");
			}
			else {
				//alert("hier");
				//clearResearchSelection();
			}
			isSelected = true;
			//clearContentSelection()
			break;
		}
	}
	if (!isSelected) {
		selectedContentArray.push(idval);
	}
}

/*
Clears the values in the selection array
*/
function clearContentSelection() {
	if (selectedContentArray) {
		selectedContentArray = new Array();
	}
}

/*Loads selected content*/
function getContent(page) {
	if (!(page)) {
		page = "content/pages/comingsoon.asp"	
	}
	genericSubmit(page,'',null,'topcontent',completedSingleLoad,regularloadingstring,false);
}


/************************************ Application Form Functions *******************************/

function UpdateApplication(formName, submitToPage, MainID, proc2Save)
{
		var ApplicationSuccess = function(oXML) 
		{
			var returnVal = oXML.responseText
			regularCursor();
			if (returnVal.substr(0,5) == "ERROR") {
				alert(returnVal);
			}
			else {
				//alert(returnVal);
				alert("Application submitted");
				var page = "/content/pages/welcome.asp"
				genericSubmit(page,"",null,"topcontent",completedSingleLoad,regularloadingstring,false)
			}
		}
		
		var AfterApplicationAdd = function(oXML) 
		{
			var returnVal = oXML.responseText
			regularCursor();
			if (returnVal.substr(0,5) == "ERROR") {
				//alert(returnVal);
				alert("The application was not submitted successfully");
			}
			else {

				//alert(returnVal);
				var page = "bin/mailer.asp" //get form action
				var dataToSend = "action=applicationmail&" + fetchFormAndListboxData(formName) + "&BranchName=" + document.forms(formName).selBranch.options[document.forms(formName).selBranch.selectedIndex].text

				hiddenSubmit(page,dataToSend, ApplicationSuccess, 0, null, false)
			}
		}
		
		if (ValidateApplicationForm(formName))
		{
			//Call hiddenSubmit
			//alert("true");
			
			var page = submitToPage //get form action
			var dataToSend = "action=UpdateApplication&MainID=" + MainID + "&" + fetchFormAndListboxData(formName)

			hiddenSubmit(page,dataToSend, AfterApplicationAdd, 0, null, false)
		}
		else
		{
			//Alert the fault
			//alert("false");
			return;
		}
}

function ValidateApplicationForm(formName)
{
	var validValue = false;
	if (document.forms(formName).all("selBranch").value != 0) // a value is selected - go on
	{
		if (document.forms(formName).all("txtAppDate").value != "") // a valid Date exists
		{
			if (IsValidIDNumber(document.forms(formName).all("txtIDNumber").value))
			{
				if (document.forms(formName).all("txtName").value != "") // a valid FirstName exists
				{
					if (document.forms(formName).all("txtSurname").value != "") // a valid Surname exists
					{
						if (document.forms(formName).all("txtCompanyName").value != "") // a valid Company Name exists
						{
							//if (document.forms(formName).all("txtCompanyRegNo").value != "") // a valid CompanyRegNo exists
							//{
								//if (document.forms(formName).all("txtVatRegNo").value != "") // a valid txtVatRegNo exists
								//{
									if (document.forms(formName).all("selRightList").options.length > 0 || document.forms(formName).all("txtBusinessOther").value != "") // a valid business link exists
									{
										if (document.forms(formName).all("txtBusinessAdd1").value != "" || document.forms(formName).all("txtBusinessAdd2").value != "") // a valid business address exists
										{
											
												if (IsNumeric(document.forms(formName).all("txtBusinessPostal").value)) // a valid business postal code exists
												{
													if (document.forms(formName).all("txtPostalAdd1").value != "" || document.forms(formName).all("txtPostalAdd2").value != "") // a valid postal address exists
													{
														
															if (IsNumeric(document.forms(formName).all("txtPostalCode").value)) // a valid postal code exists
															{
																if (document.forms(formName).all("txtTelBusiness").value != "") // a valid business telnr exists
																{
																

																	if (document.forms(formName).all("chkExisting").checked == true) // an existing branch exists
																	{
																		if (document.forms(formName).all("selExistingBranch").value != 0) // a valid existing branch exists
																		{
																			validValue = true;
																		}
																		else
																		{
																			validValue = false;
																			alert("If you have an existing card, you have to enter the branch it's linked to");
																		}
																	}
																	else
																	{
																		validValue = true;
																	}
																	
																	//if (document.forms(formName).all("txtTelHome").value != "") // a valid home telnr exists
																	//{
																		//if (document.forms(formName).all("txtTelFax").value != "") // a valid Fax telnr exists
																		//{
																			//if (document.forms(formName).all("txtTelCell").value != "") // a valid Cell telnr exists
																			//{
																				//if (isValidEmail(document.forms(formName).all("txtEmail").value)) // a valid Email exists
																				//{
																					
																				//}	
																				
																				//else
																				//{
																				//	validValue = false;
																				//}
																			//}
																			//else
																			//{
																			//	validValue = false;
																			//}
																		//}
																		//else
																		//{
																		//	validValue = false;
																		//}
																	//}
																	//else
																	//{
																	//	validValue = false;
																	//}
																}
																else
																{
																	alert("Please enter a valid business telephone number");
																	validValue = false;
																}
															}
															else
															{
																alert("Please enter a valid postal address code");
																validValue = false;
															}
														
													}
													else
													{
														alert("Please enter a valid postal address");
														validValue = false;
													}
												}
												else
												{
													alert("Please enter a valid business postal code");
													validValue = false;
												}
											
										}
										else
										{
											alert("Please enter a valid business address");
											validValue = false;
										}
									}
									else
									{
										alert("Please select a valid business type");
										validValue = false;
									}
								//}
								//else
								//{
								//	validValue = false;
								//}
							//}
							//else
							//{
							//	validValue = false;
							//}
						}
						else
						{
							alert("Please enter a valid company name");
							validValue = false;
						}
					}
					else
					{
						alert("Please enter a valid surname");
						validValue = false;
					}
				}
				else
				{
					alert("Please enter a valid firstname");
					validValue = false;
				}
			}
			else
			{
				alert("Please enter a valid ID number consisting of 13 numbers");
				validValue = false;
			}
		}
		else
		{
			alert("Please enter a valid application date");
			validValue = false;
		}
	}
	else
	{
		alert("Please enter a valid branch for whom the application is for");
		validValue = false;
	}
	return validValue;
}

/************************************ Application Form Functions *******************************/
