/*
	27/03/08	Mick		New based on ICC
	18/06/07	D Stan		Other Function fixed
	21/07/08	Mick		ListReports() etc
	20/08/08	Mick		Added CheckItemNOTSelected()
	22/01/09	Mick		Added PrintPage()
	30/12/10	Mick		Added new function requested by Heena to check number for all schemes
	30/11/11	A Lowe		Added TrendAnalysis functions
*/


function CheckQuantitative(strControlID, strSample)		{

	var strData = '';
	var strReturn = '';
	var strErrorMessage;
	
	if (strSample.length > 0)	{
		strSample = strSample + ': ';
	}
	
	strErrorMessage = strSample + 'You must enter whole numbers WITHOUT commas or units e.g. 13000.\n\n'; 
	strErrorMessage = strErrorMessage + 'DO NOT enter scientific text (1.3x10e4) or log10 values.\n\n';
	strErrorMessage = strErrorMessage + 'You may enter censored values WITHOUT units (e.g. <10 or >100).\n\n';
  
	/*  Check control exists     */
	if (document.getElementById(strControlID) != null)	{

		/*  Check control is not empty     */
		if (document.getElementById(strControlID).value != '')	{
		
			strData = document.getElementById(strControlID).value;
		
			/*  Check for < & >     */
			if ((strData.slice(0,1) == '<')	|| (strData.slice(0,1) == '>'))		{
				strData = strData.slice(1, strData.length);
			}
			
			/*  Check for Euro version of decimal point - replace , with .      NO - CHANGE OF PLAN!   */
			//strData = strData.replace(',', '.');
						
			/*  Now check that it is a number     */
			if (isNaN(strData))	{ 
				strReturn = strErrorMessage;
			}
			else	{
				/********************************************************
				 It is a number but is it an integer?   
				 If the value of a variable is an integer, then the numeric 
				 value of it's parseFloat() and parseInt() equivalents 
				 will be the same,
				*********************************************************/
				if	(parseFloat(strData) != parseInt(strData))		{
				    strReturn = strErrorMessage
				} 
			}
		}
	} 
	
	return strReturn;
}




function PrintPage()	{

	self.print();
}

function ListReports(lngTrialID)	{

	var strURL = "../ReportList.asp?id=" + lngTrialID + "&ParticipantID=" + document.getElementById("txtParticipantID").value + "&WebLogID=" + document.getElementById("txtWebLogID").value;
	var objWindow = window.open(strURL, "ReportList", "dependent=no,toolbar=no,left=30,top=30,scrollbars=yes,resizable=yes,width=790,height=500");

}


function ShowTrendAnalysis(lngTrialID)	{

	var strURL = "../TrendAnalysis.asp?id=" + lngTrialID + "&WebLogID=" + document.getElementById("txtWebLogID").value;
	var objWindow = window.open(strURL, "TrendAnalysis", "dependent=no,toolbar=no,left=30,top=30,scrollbars=yes,resizable=yes,width=790,height=500");

}

function TrendAnalysisRefresh() {

	document.getElementById("frmTrendAnalysis").submit();
	
}


function CheckItemNOTSelected(strControlName, strName)	{

	var ctrlControl = document.getElementById(strControlName);
	
	var intIndex = ctrlControl.selectedIndex;
		
	if (intIndex != 0)	{
		return strName;
	}
	else	{
		return "";
	}
}


function CheckItemSelected(strControlName, strName)	{

	var ctrlControl = document.getElementById(strControlName);
	var intIndex = ctrlControl.selectedIndex;
	
	if (intIndex == 0)	{
		return strName;
	}
	else	{
		return "";
	}
}



function CheckOthers(strControlName, strMessage)	{

	var ctrlComboControl = document.getElementById(strControlName);
	var ctrlControl = document.getElementById("txt" + strControlName);
	var intIndex = ctrlComboControl.selectedIndex;

	if ((ctrlControl.value.length==0) && (ctrlComboControl.options[intIndex].text.slice(0,5)=="Other"))	{
		alert("Unable to save the results - missing OTHER value for " + strMessage + ".\n\nClick the 'OTHER' button [...] and enter a value.");			
		return false;
	}

	return true;
}
	
//function GetOther(strControlName, strType, strExt, intControlID)	{
function GetOther(strControlName)	{
	
	var ctl = document.getElementById(strControlName);
	var strControlValue = ctl.options[ctl.selectedIndex].text;
	var rtnValue;
					
	if (strControlValue.toLowerCase().indexOf('other') > -1)	{
		
		//var strOtherControlName = "txt" + strType + "Other_" + strExt + intControlID
		var strOtherControlName = "txt" + strControlName;
		
		var ctlOther = document.getElementById(strOtherControlName); 
						
		rtnValue = window.prompt('Enter the OTHER value', ctlOther.value);	
		if (rtnValue != null)	{
			ctlOther.value = rtnValue;
		}
	}
	else	{
		ctl.focus()
		alert("You need to select 'Other' before clicking this button"); 
	}		
		
}


function ShowControl(ctrlName, bolTrue)	{

	if (bolTrue)	{
		ctrlName.visibility = "visible"
		ctrlName.height = "100%";
		ctrlName.width = "100%"
		ctrlName.position = "relative";
	}
	else	{
		ctrlName.visibility = "hidden"
		ctrlName.height = "0";
		ctrlName.width = "0";
		ctrlName.position = "absolute";	
	}
}


function Back()	{

	document.getElementById("frmDataEntry").action = "../Main.asp";
	document.getElementById("frmDataEntry").submit();
}




function SaveRecord()	{
		
	if (ValidateDataEntry()==true)	{
		var boolSave = confirm('Click OK to confirm that you wish to save your results. \r\n\r\n' + 'You will be able to change them the next time you login.');
				
		if (boolSave)	{
			document.getElementById("txtAction").value = 'save';
			document.getElementById("frmDataEntry").submit();	
		}
	}
}

function CompleteRecord()	{

	document.getElementById("txtAction").value = 'complete';
	
	if (ValidateDataEntry() == true)	{

		var boolComplete = confirm('Press OK to Save and Complete your Results. \r\n\r\n' + 'You will NOT be able to change your Results.');
				
		if (boolComplete)	{
			document.getElementById("frmDataEntry").submit();	
		}
		else	{
			document.getElementById("txtAction").value = '';
		}
	}
	else	{
		document.getElementById("txtAction").value = '';
	}
}


function RedirectPage(strURL)	{

	if (self.parent.frames.length != 0)	{self.parent.location.href=strURL;}
	else	{location.href = strURL;}

}

function DisplayMsg(strMsg)	{
	
	if (strMsg.length>0)	{alert(strMsg);}
	
}


function Register()	{

	var strMessage = "";
	var strErrors = "";
		
	if ((document.getElementById("txtFirstname").value.length == 0) && (document.getElementById("txtSurname").value.length == 0)) {
		strErrors = "You must enter a contact name.\n";
	}

	if (document.getElementById("txtOrganisation").value.length == 0) {
		strErrors = "You must enter the Organisation name.\n";
	}

	if ((document.getElementById("txtStreet").value.length == 0) && (document.getElementById("txtDistrict").value.length == 0) && (document.getElementById("txtCity").value.length == 0)) {
		strErrors = "You must enter an address.\n";
	}

	if (document.getElementById("txtEMail").value.length == 0) {
		strErrors = "You must enter a contact email address.\n";
	}

	if (document.getElementById("txtWebID").value.length == 0) {
		strErrors = "You must enter a WebID.\n";
	}

	if (document.getElementById("txtWebPassword").value.length == 0) {
		strErrors = "You must enter a Web Password.\n";
	}
	
	if (strErrors == "") {
		document.getElementById("txtFunction").value = "register";
		document.getElementById("frmDetails").target = "_self";
		document.getElementById("frmDetails").submit();
	}
	else {
		strMessage = "The details cannot be saved for the following reason(s).\n\n";
		alert(strMessage + strErrors);
	}	
	
}



function RowInFocus(cellName, strPriority) {

	if (document.getElementById)	{
		document.getElementById(cellName).className = 'RowInFocus';
	}
	else	{
		alert("No scripting!");
	}
}

function RowNotInFocus(cellName, strPriority) {

	if (document.getElementById)	{
		document.getElementById(cellName).className = 'RowNotInFocus';
	}
	else	{
		alert("No scripting!");
	}	
}

function isValidDate(dateStr) {

	// Checks for the following valid date formats:
	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
	// Also separates date into month, day, and year variables

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
	var strMessage

	// To require a 4 digit year entry, use this line instead:
	// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

	var matchArray = dateStr.match(datePat); // is the format ok?

	if (matchArray == null) {
		strMessage = "Date is not in a valid format. Use DD/MM/YY  DD/MM/YYYY  DD-MM-YY  DD-MM-YYYY.\n";
		return strMessage;
	}

	// changed matchArray[1] to 3  & 3 to 1 for UK date format 
	month = matchArray[3]; // parse date into variables
	day = matchArray[1];
	year = matchArray[4];

	if (month < 1 || month > 12) { // check month range
		strMessage = "Month must be between 1 and 12.\n";
		return strMessage;
	}
	if (day < 1 || day > 31) {
		strMessage = "Day must be between 1 and 31.\n";
		return strMessage;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		strMessage = "Month "+month+" doesn't have 31 days!\n";
		return strMessage
	}
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
		strMessage = "February " + year + " doesn't have " + day + " days!\n";
		return strMessage;
	   }
	}
	return "";  // date is valid
}

function CheckDate(strControl)	{

	if (document.getElementById(strControl) != null)	{
		if (document.getElementById(strControl).value.length != 0)	{
			var strMessage = isValidDate(document.getElementById(strControl).value);
			if (strMessage.length != 0)	{
				return strMessage;			
			}
			else	{
				return "";
			}
		}
		else	{
			return "";
		}
	}
	else	{
		return "";
	}	
}



