var autoSaveTimer = null;

function commonProfileOnloadPage(profileBase) {
	if (top.document.forms[0].LeaveForm.value == "1") {
		top.document.forms[0].LeaveForm.value = "0";
		if (top.document.forms[0].PersonID.value > 0) {
			top.mainiframe.document.location = leaveTo(profileBase);
		} else {
			// If no SU selected don't load menu
			top.mainiframe.document.location = leaveTo();
		}
	}
	//top.document.all.ServiceUserFunctionsMenu.resetStyles();
	top.scrollTo(0, 0);
	// Autosave feature now removed
	//startAutoSaver();
	hideValidatorInnerText()
}

function commonAdminOnloadPage(profileBase) {
	if (top.document.forms[0].LeaveForm.value == "1") {
		top.document.forms[0].LeaveForm.value = "0";
		top.mainiframe.document.location = leaveTo(profileBase);
	}
	top.scrollTo(0, 0);
	//startAutoSaver();
	hideValidatorInnerText()
}

function startAutoSaver() {
	// Autosave feature now removed
	return;
	//autoSaveTimer = window.setInterval(autoSaver, 10 * 60 * 1000);
}

function autoSaver() {
	// Autosave feature now removed
	return;
	//document.forms[0].SaveButton.click();
}

function hideValidatorInnerText() {
	var tgs = document.all.tags("SPAN");
	for (i = 0; i < tgs.length; i++) {
		if (tgs[i].getAttribute("errormessage")) {
			if (!tgs[i].getAttribute("validationexpression")) {
				tgs[i].innerText = '';
				//alert(tgs[i].outerHTML);
			} else {
				//alert(tgs[i].outerHTML);
			}
		}
	}
}

function getRegId(el) {
	return (el.value > 0) ? el.value : 0;
}

function getProfileId(el, profileBase) {
	return (el.value > 0) ? el.value : 0;
}

function leaveForm() {
	if (top.document.forms[0].PersonID.value > 0) {
		top.mainiframe.document.location = leaveTo(profileBase);
	} else {
		// If no SU selected don't load menu
		top.mainiframe.document.location = leaveTo();
	}
}

function saveForm() {
	if (Page_ClientValidateExtended() == true) {
	}
}

function saveAndLeaveForm() {
	if (Page_ClientValidateExtended() == true) {
		top.document.forms[0].LeaveForm.value = "1";
	}
}

function leaveTo(profileBase) {
	var arrProfileBases = new Array('Reg', 'Ref', 'ContactMethods', 'Ident', 'GeogAssoc', 'Pet', 'EUNat', 'Ethnic', 'Faith', 'Lang', 'Family', 'Health', 'HealthService', 'CrimJust', 'EducTrain', 'EmpInc', 'BlogPwd');
	var arrSupNdBases = new Array('SupNeed', 'SupInfml');
	var arrAdminBases = new Array('Agency');
	var arrAccommBases = new Array('Accomm', 'AccommSuit');
	var arrPlanBases = new Array('SupportPlan');
	var arrThemeBases = new Array('CurrentWorries');
	var arrStrategyThemeBases = new Array('SupportPlanAccommodation', 'SupportPlanFamily', 'SupportPlanHealth', 'SupportPlanLearning', 'SupportPlanLegal', 'SupportPlanLife', 'SupportPlanSocial');
	if (!isBlank(profileBase)) {
		if (isInArray(profileBase, arrProfileBases)) {
			return('../Profile/Profile_Menu.aspx?regID=' + getPersonID());
		} else if (isInArray(profileBase, arrSupNdBases)) {
			return('../Profile/Profile_Support_Menu.aspx?regID=' + getPersonID());
		} else if (isInArray(profileBase, arrAdminBases)) {
			return('../Agency/Agency_Administration.aspx');
		} else if (isInArray(profileBase, arrAccommBases)) {
			return('../Profile/Profile_Menu.aspx?regID=' + getPersonID());
		} else if (isInArray(profileBase, arrPlanBases)) {
			return('../Profile/Plans_Menu.aspx?regID=' + getPersonID());
		} else if (isInArray(profileBase, arrThemeBases)) {
			return('../Profile/Themes_Menu.aspx?regID=' + getPersonID());
		} else if (isInArray(profileBase, arrStrategyThemeBases)) {
			return('../Profile/Profile_SupportPlan.aspx?regID=' + getPersonID());
		} else {
			return('../Empty.aspx');
		}
	} else {
		return('../Empty.aspx');
	}
}

function goToSupportPlanTheme(urlWithQueryString) {
	if (isChanged(document.forms[0])) {
		Page_ClientValidate();
		if (Page_IsValid) {
			document.forms[0].DestinationURL.value = urlWithQueryString;
			document.forms[0].submit();
		} else {
			document.forms[0].NextButton.click();
		}
	} else {
		window.location = urlWithQueryString;
	}
}

function cancelRegistration() {
	top.mainiframe.document.location='../Empty.aspx';
}

// From Nick
function HasFormChanged(source, args)
{
 var oForm = document.forms[0];
 var bChanged = false; 
 var iNumElems = oForm.elements.length;
 
 for (var i=0;i<iNumElems;i++)
 {
  var oElem = oForm.elements[i];
   
  if ("text" == oElem.type || "TEXTAREA" == oElem.tagName) {
   if (oElem.value != oElem.defaultValue) bChanged = true;
  }else if ("checkbox" == oElem.type || "radio" == oElem.type){
   if (oElem.checked != oElem.defaultChecked) bChanged = true;
  }else if ("SELECT" == oElem.tagName){
    var oOptions = oElem.options;
    var iNumOpts = oOptions.length;
    for (var j=0;j<iNumOpts;j++)
    {
     var oOpt = oOptions[j];
     if (oOpt.selected != oOpt.defaultSelected) bChanged = true;
    }
}
}
  
args.IsValid = bChanged;
}

