/*
	05/09/07	Added Phospholipid Antibodies (ACA - Phos) scheme to ShowSpecificPage
	08/03/08	Added UCRP and B2M (plus ACR, AFP, CSF2, IGG, MICROB, PSA, TM, C1, E, ESP) to ShowSpecificPage
	03/12/09	Added AAPI
*/

var arrPageGroups

function DisplayProgrammesSortSequence()	{

	if (navigator.appName.indexOf("Microsoft") != -1) {
		var strCmd = 'cmd' + document.frmProgrammes.txtSortField.value;
		document.frmProgrammes[strCmd].value = document.frmProgrammes[strCmd].value + ' ' + document.frmProgrammes.txtSortArrow.value;
	}
}

function getlogin(strMsg) {
	document.frmLogin.txtLabNumber.focus();
	DisplayMsg(strMsg);
}


function PasswordHint()	{

	var strLogin = document.frmLogin.txtLabNumber.value;
	var strPwdHint = document.frmLogin.txtPwdHnt.value;
	
	if (strLogin.length==0)	{
		alert("You need to enter your login ID before a password hint can be provided.")
	}
	else if (strPwdHint.length>0) {
		alert("Your password hint is: \r\n\r\n" + strPwdHint);
	}
	else	{
		document.frmLogin.txtPwdHelp.value = "True"
		document.frmLogin.submit();
	}
}

function Reminder()	{

	var strLab = document.frmLogin.txtLabNumber.value;
	var strEMailAddress = "";
	
	if (strLab.length ==0) {
		alert("You need to enter your Lab Number before you can use this feature");
	}
	else	{
		strEMailAddress = prompt("Enter the EMail Address which you use for your UKNEQAS subscription.", "name@address.com");
		if ((strEMailAddress==null)||(strEMailAddress=="")){
			alert("You need to Enter your Email Address in order to receive a Reminder");
		}
		if (EMailValidation(strEMailAddress)==false){
			document.frmLogin.txtAddress.value="";
			alert("Please check your Email Address as it has failed the validation criteria");
		}
		else	{
			document.frmLogin.txtAddress.value=strEMailAddress;
			document.frmLogin.submit();
		}
	}
}

function EMailValidation(str) {

		var at="@"
		var dot="."
		
		if (str==null)	{str='';}
		
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail Address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail Address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail Addresss")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail Addresss")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail Addresss")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail Addresss")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail Addresss")
		    return false
		 }

 		 return true					
}
	
function RedirectPage(strURL)	{

	if (screen.width >= 1280) {
		var intSID = 5;
	}
	else if (screen.width >= 1152) {
		var intSID = 4;
	}
	else if (screen.width >= 1024) {
		var intSID = 3;
	}
	else if (screen.width >= 800) {
		var intSID = 2;
	}
	else if (screen.width >= 640) {
		var intSID = 1;
	}
	if (strURL.substr(strURL.length-3,3)) {
		strURL=strURL + "?SID=" + intSID
	}
	else
	{
		strURL=strURL + "&SID=" + intSID
	}
	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 SendEmail()	{

	document.frmSort.action = "DataUpdate.asp";
	document.frmSort.submit();

}

function SelectAll(intSelection)	{
	if (intSelection==1) {
		document.frmSort.txtChecked.value = "All";
		document.frmSort.submit();
	}
	else if	(intSelection==0)	{
		document.frmSort.txtChecked.value = "None";
		document.frmSort.submit();
	}
}

function Reload(intSelection) {
	if (intSelection==1) {
		document.frmSort.txtSelectList.value = "Members";
		document.frmSort.submit();
	}
	else if	(intSelection==2)	{
		document.frmSort.txtSelectList.value = "EmailList";
		document.frmSort.submit();
	}
}

function RowInFocus(cellName,num) {
	
	
	if (document.images) {
		if (document.getElementById(cellName).className!='RowChosen')
			{
			eval(cellName+".className='RowInFocus'");			
			eval(cellName+".style.cursor='hand'");	
			}
	}
}

function RowNotInFocus(cellName,num) {
	if (document.images) {
		//document.getElementById('r'+ID).className='RowChosen';
		if (document.getElementById(cellName).className!='RowChosen')
			eval(cellName+".className='RowNotInFocus'");
	}
}

function GlossaryRowInFocus(cellName,num) {
	if (document.images) {
		eval(cellName+".className='GlossaryRowInFocus'");
		eval(cellName+".style.cursor='hand'");	
	}
}

function GlossaryRowNotInFocus(cellName,num) {
	if (document.images) {
		eval(cellName+".className='GlossaryRowNotInFocus'");
	}
}

function ShowDiscussionGroup(ID)	{
  location.href = 'DiscussionGroupEdit.asp?id=' + ID;
}


function ShowMember(ID) {
  location.href = 'MemberEdit.asp?id=' + ID;
}


function ShowNews(ID) {
  location.href = 'NewsEdit.asp?id=' + ID;
}


function ShowLinkType(ID) {
  location.href = 'LinkTypeEdit.asp?id=' + ID;
}

function ShowTopic(ID) {
  location.href = 'TopicEdit.asp?id=' + ID;
}

function ShowFAQ(ID) {
  location.href = 'FAQEdit.asp?id=' + ID;
}

function ShowEmail(ID)	{
  location.href = 'ComposeEmail.asp?id=' + ID;
}

function ShowAdvert(ID)	{
  location.href = 'AdvertEdit.asp?id=' + ID;
}

function ShowLink(ID)	{
  location.href = 'LinkEdit.asp?id=' + ID;
}

function ShowParameter(ID)	{
  location.href = 'ParameterEdit.asp?id=' + ID;
}


function ShowSpecificPage(ID,strType)	{

	if	(strType=="SKIN") {location.href = 'BullousDermatosis/TrialEdit.asp?id=' + ID;}
	else if	(strType=="ANCA") {location.href = 'ANCA/TrialEdit.asp?id=' + ID;}
	else if	(strType=="GAS2") {location.href = 'GAS/TrialEdit.asp?id=' + ID;}
	else if	(strType=="CRP") {location.href = 'CRP/TrialEdit.asp?id=' + ID;}
	else if	(strType=="NUC") {location.href = 'Nuclear/TrialEdit.asp?id=' + ID;}
	else if	(strType=="CD") {location.href = 'CoeliacDisease/TrialEdit.asp?id=' + ID;}
	else if	(strType=="MIG") {location.href = 'Monoclonal/TrialEdit.asp?id=' + ID;}
	else if	(strType=="CSF") {location.href = 'CSFO/TrialEdit.asp?id=' + ID;}
	else if	(strType=="FUN") {location.href = 'Fungal/TrialEdit.asp?id=' + ID;}
	else if	(strType=="ACA") {location.href = 'PHOS/TrialEdit.asp?id=' + ID;}
	else if	(strType=="News")	{location.href = 'NewsEdit.asp?id=' + ID;}
	else if	(strType=="MenuItem")	{location.href = 'MenuEdit.asp?id=' + ID;}
	else if	(strType=="Glossary")	{location.href = 'GlossaryEdit.asp?id=' + ID + "&gi=" + document.frmSort.txtASCII.value;}
	else if	(strType=="Parameter")	{location.href = 'ParameterEdit.asp?id=' + ID;}
	else if	(strType=="PageGroup")	{location.href = 'PageGroupEdit.asp?id=' + ID;}
	else if	(strType=="Member")	{location.href = 'MemberEdit.asp?id=' + ID;}
	
	else if	(strType=="UCRP") {location.href = 'UCRP/TrialEdit.asp?id=' + ID;}
	else if	(strType=="B2M") {location.href = 'B2M/TrialEdit.asp?id=' + ID;}
	else if	(strType=="ACR") {location.href = 'ACR/TrialEdit.asp?id=' + ID;}
	else if	(strType=="AFP") {location.href = 'AFP/TrialEdit.asp?id=' + ID;}
	else if	(strType=="CSF2") {location.href = 'CSF2/TrialEdit.asp?id=' + ID;}
	else if	(strType=="IGG") {location.href = 'IGG/TrialEdit.asp?id=' + ID;}
	else if	(strType=="MICROB") {location.href = 'MICROB/TrialEdit.asp?id=' + ID;}
	else if	(strType=="PSA") {location.href = 'PSA/TrialEdit.asp?id=' + ID;}
	else if	(strType=="TM") {location.href = 'TM/TrialEdit.asp?id=' + ID;}
	else if	(strType=="C1") {location.href = 'C1/TrialEdit.asp?id=' + ID;}
	else if	(strType=="E") {location.href = 'E/TrialEdit.asp?id=' + ID;}
	else if	(strType=="ESP") {location.href = 'ESP/TrialEdit.asp?id=' + ID;}
	else if	(strType=="AAPI") {location.href = 'AAPI/TrialEdit.asp?id=' + ID;}
}

function ShowSpecificPageINFO(intProgrammeID,strDistNo,bolExists)	{

	if (bolExists == 1) 
	{
		var varWindow = window.open("Scripts/ViewReportINFO.asp?ProgID=" + intProgrammeID + "&DistNo=" + strDistNo, "INFOReport", "width=800,height=600,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes");	
	}
	else
	{
		alert("There is no Info report to display for this distribution");	
	}
}



function CreatePGArray(strPageGroups)	{

	arrPageGroups = strPageGroups.split(",");

}

function CheckImageSizes()		{

	var index = document.frmDetails.cboContent.selectedIndex;
	var lngContent = document.frmDetails.cboContent.options[index].value;
	var strContentID = "CO" + lngContent
	var lngPageGroup = 1
	var strPageGroup
	
	for(var i = 0; i < (arrPageGroups.length-1); i++)	{
		if (arrPageGroups[i]==strContentID)	{
			i = i+1;
			strPageGroup = arrPageGroups[i]
			lngPageGroup = strPageGroup.slice(2,strPageGroup.length);
			break;
		}
	}

	if (lngPageGroup > 1)	{
		if (document.frmDetails.txtMenuImg.value == "True")	{
			document.imgMenuItem.width=88;
			document.imgMenuItem.height=23;
		}
		if (document.frmDetails.txtMenuImgF.value == "True")	{
		}
			document.imgMenuItemF.width=75;
			document.imgMenuItemF.height=110;
	}
	else	{
		if (document.frmDetails.txtMenuImg.value == "True")	{
			document.imgMenuItem.width=88;
			document.imgMenuItem.height=23;
		}
		if (document.frmDetails.txtMenuImgF.value == "True")	{
		}
			document.imgMenuItemF.width=200;
			document.imgMenuItemF.height=66;
	}

}



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 isValidTime(timestr)
{
var colon_pos = timestr.indexOf(':');
var hours = timestr.substring(0,2);
var minutes = timestr.substring(3,5);
var hoursmins = timestr;
		
		if (colon_pos != 2)
		{
			strMessage = "The time should be entered in the format hh:mm";
			return strMessage;
		}
		else if (hoursmins.length < 5 || hoursmins.length > 5)
		{
			strMessage = "The time should be entered in the format hh:mm";
			return strMessage;
		}
		else if (hours >23)
		{
			strMessage = "The time should be entered in 24 hour clock format";
			return strMessage;
		}
		else if (minutes > 59)
		{
			strMessage = "The time should be entered in 24 hour clock format";
			return strMessage;
		}
return "";  // time is valid
}

function OpenWindow(strControl) {
		
	if (strControl=="txtHeadline") {
		var strHTML=document.frmDetails.txtHeadline.value;	
	}
	else if (strControl=="txtAbstract") {
		var strHTML=document.frmDetails.txtAbstract.value;	
	}
	else if (strControl=="txtDetails") {
		var strHTML=document.frmDetails.txtDetails.value;	
	}
	
	var varWindow = window.open("", "ViewHtml", "width=500,height=500,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes");	
	varWindow.document.write(strHTML);
	varWindow.document.close;
}


function ViewNews()	{

	var varWindow = window.open("../Scripts/News.asp?id=" + document.frmDetails.txtID.value, "ViewHtml", "width=500,height=500,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes");	

}




function DeleteListItem(lngID, strEntity)	{

	var strMsg = "Delete Logic called in Error";

	if (strEntity == 'tblNews') {
		strMsg = "Are you sure that you want to delete this News Item?"
	}
	else if (strEntity == 'tblContent') {
		strMsg = "Are you sure that you want to delete this Content?"
	}
	else if (strEntity == 'RootMenu') {
		strMsg = "Are you sure that you want to delete this Menu Item?"
	}
	else if (strEntity == 'MenuItem') {
		strMsg = "Are you sure that you want to delete this Menu Item?"
	}
	else if (strEntity == 'tblGlossary') {
		strMsg = "Are you sure that you want to delete this Glossary Item?"
	}
	else if (strEntity == 'PageGroup') {
		strMsg = "Are you sure that you want to delete this PageGroup?"
	}
	else if (strEntity == 'Upload') {
		strMsg = "Are you sure that you want to delete this PDF/Flash Item?"
	}
	bolChoice=window.confirm(strMsg) 
	if (bolChoice==true) {
		document.frmSort.txtAction.value='Delete';
		document.frmSort.txtID.value=lngID;
		document.frmSort.submit();
	}
}


function DeleteItem(strEntity)	{

	var strMsg = "";
	var bolErrors = false;
	var bolChoice = false;
		
	if (document.frmDetails.txtID.value == 0)	{
		alert("This record cannot be deleted as it has not yet been saved.");
	}
	else	{
	
		if (strEntity == 'News') {
			strMsg = "Are you sure that you want to delete this News Item?"
		}
		else if (strEntity == 'tblContent') {
			strMsg = "Are you sure that you want to delete this Content?"
		}
		else if (strEntity == 'Glossary') {
			strMsg = "Are you sure that you want to delete this Glossary Item?"
		}
		else if (strEntity == 'tblMenuItem') {
			if (document.frmDetails.txtApplicationItem.value > 0)	{
				bolErrors = true;
				alert("You cannot delete this item.  It is part of the application\r\n\r\nSet Display = No if you wish to hide it."); 	
			}
			else	{
				strMsg = "Are you sure that you want to delete this Menu Item?"
			}
		}
		if (bolErrors==false)	{bolChoice=window.confirm(strMsg) }
		if (bolChoice==true && bolErrors==false) {
			document.frmDetails.txtFunction.value='Delete';
			document.frmDetails.txtTable.value=strEntity;
			document.frmDetails.submit();
		}

	}
}


function NewRecord(strType)	{

	if (strType=="News")	{location.href="../admin/NewsEdit.asp?ID=0";}
	else if (strType=="RootMenu")	{location.href="../admin/MenuEdit.asp?ID=0";}
	else if (strType=="Content")	{location.href="../admin/ContentEdit.asp?ID=0&PG=" + document.frmSort.cboPageGroup.value;}	
	else if (strType=="Glossary")	{location.href="../admin/GlossaryEdit.asp?ID=0&gi=" + document.frmSort.txtASCII.value;}	
	else if (strType=="PageGroup")	{location.href="../admin/PageGroupEdit.asp?ID=0";}	
	else if (strType=="Member")	{location.href="../admin/MemberEdit.asp?ID=0";}	

}


function NewSubMenu(lngMenuID)	{

	location.href="../admin/MenuEdit.asp?ID=0&PID=" + lngMenuID
	
}


function DeleteImage(strImage)
{
	bolChoice=window.confirm("Are you sure that you want to delete " + strImage + " ?")
	if (bolChoice==true) {
		document.frmDetails.txtAction.value="Delete";
		document.frmDetails.txtImgName.value=strImage;
		document.frmDetails.submit()
	}
}


function NewDirectory()	{

	location.href="../admin/ImageSelect.asp?Dir=" + document.frmDetails.cboDirectory.value + "&CFID=" + document.frmDetails.txtCFID.value;
}


function Back()	{

	var strParams = "";
		
	if (document.frmSample.ReferringPage.value == "TrialEdit.asp")	{
		location.href = '../Main.asp';
	}
	else if (document.frmSample.ReferringPage.value == "MethodSelect.asp")	{
		location.href = '../Main.asp';
	}
	
	//location.href = document.frmSample.txtCallingForm.value + strParams;
	
}

function Preview()	{

	var index = document.frmDetails.cboPageGroup.selectedIndex;
	var lngPageGroup = document.frmDetails.cboPageGroup.options[index].value;
	
	var objPreview = window.open("../ContentReader.asp?PU=True&CID=" + document.frmDetails.txtID.value + "&PG=" + lngPageGroup, "Preview", "width=620,height=520,top=50,left=75,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes");	
	if (window.focus) {
		objPreview.opener = self;
		objPreview.focus();
	}

}

function ShowProgress()
{
  strAppVersion = navigator.appVersion;
  if (document.frmDetails.txtFile.value != "")
  {
    if (strAppVersion.indexOf('MSIE') != -1 && strAppVersion.substr(strAppVersion.indexOf('MSIE')+5,1) > 4)
    {
      winstyle = "dialogWidth=375px; dialogHeight:130px; center:yes";
      window.showModelessDialog('<% = barref %>&b=IE',null,winstyle);
    }
    else
    {
      window.open('<% = barref %>&b=NN','','width=370,height=115', true);
    }
  }
  return true;
}

function UploadOK()
{
	top.opener.location.href = "ImageSelect.asp?CFID=" + document.frmDetails.txtCFID.value + "&Dir=" + document.frmDetails.txtDirectory.value;
	window.close();
}

function UploadObjectOK(strType)
{
	top.opener.location.href = "UploadList.asp?UT=" + strType;
	window.close();
}

function ViewImage(strPath)	{

	var objViewImg = window.open("ViewImage.asp?path=" + strPath, "ViewImg", "width=450,height=200,top=50,left=75,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes");	
	if (window.focus) {
		objViewImg.opener = self;
		objViewImg.focus();
	}

}


function RefreshImage(lngID)	{

	if (lngID==1)	{document.imgMenuItem.src=document.frmDetails.txtMenuImgFile.value;}
	else if (lngID==2)	{document.imgMenuItemF.src=document.frmDetails.txtMenuImgFFile.value;}	
}


function BrowseImages(lngCFID)	{

	var objBrowseImages = window.open("../Admin/ImageSelect.asp?CFID=" + lngCFID, "ImageSelect", "top=50,left=50,width=600,height=400,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes");
	if (window.focus) {
		objBrowseImages.opener = self;
		objBrowseImages.focus();
	}
	
}

function setFormValue(vFormName, vControlName, vValue)
// Purpose:		Sets a control's value. Can handle text boxes, drop-downs,
//				multi-selects, grouped checkboxes and radio buttons etc.
// Parameters:	vFormName		name of form
//				vControlName	name of control
//				vValue			value to set it to
// Returns:		None
{	
	var vControl;
	//alert(vControlName + ' '+ vValue);	
	// get reference to control
	try {
		vControl = document.forms(vFormName).elements(vControlName);
	} catch (e) {
		// control must not exist - exit
		return;
	}
	try
	{
		// if its a multi-select
		if (vControl.type == 'select-multiple')
		{
			// split comma separated string into array
			vOptionArray = convertCommaStringToArray(vValue);
			
			// loop through all selected options
			for (var vCounter = 0; vCounter < vOptionArray.length; vCounter++)
			{
				// set the option to selected				
				setMultiSelectOption(vControl, vOptionArray[vCounter]);
			}
		}
		// if its a drop-down
		else if (vControl.type == 'select-one')
		{
			// set the value
			vControl.value = vValue;
		}
		// if it's a check box
		else if (vControl.type == 'checkbox')
		{
			if (vValue == 'on')
				vControl.checked = true;
			else
				vControl.checked = false;
		}
		// check for textbox
		else if (vControl.type == 'textbox')
		{
			// set the value
			vControl.value = vValue;
		}
		// check to see if it's a multi-select checkbox or a group of radio buttons
		else if ((vControl[0].type == 'checkbox') || (vControl[0].type == 'radio'))
		{
			// split comma separated string into array
			vOptionArray = convertCommaStringToArray(vValue);
			
			// loop through all selected options
			for (var vCounter = 0; vCounter < vOptionArray.length; vCounter++)
			{
				// set the option to selected
				setMultiCheckboxOption(vControl, vOptionArray[vCounter]);
			}
		}
	
	} catch (e) {
		// assume a text box - set the value
		try {
			vControl.value = vValue;
		} catch (e) {
			// do nothing, control cannot exist
		}
	}
}


function showCalendar(vTargetPage, vControlName)
// Purpose:		Display's the calendar
{
	var vControl = document.getElementById(vControlName);
	var vDateString = vControl.value;

	var vWidth = 375;
	var vHeight = 225;

	var vLeft = (screen.availWidth - vWidth) / 2;
	var vTop = (screen.availHeight - vHeight) / 2;

	var vOptions = 'center=yes; help=no; resizable=no; status=no; scrollbars=yes; dialogWidth=' + vWidth + 'px; dialogHeight=' + vHeight + 'px; dialogLeft=' + vLeft + 'px; dialogTop=' + vTop + 'px';
	var vResult;

	vResult = window.showModalDialog(vTargetPage, vDateString, vOptions);
			
	if (vResult != 'cancelled')
		vControl.value = vResult;
}


function TestNumeric(str) {
	
	if (isNaN(str))	{return false;}
	else	{return true;}

}
