////////////////////////////////////////////////////////////////////////////////////////////////
// CMS Delete Javascript
////////////////////////////////////////////////////////////////////////////////////////////////

function DeleteConfirm(PageID,Type,TypeID){

	if (confirm("Are you sure you want to archive this page?")) {
		if (Type == "Page") {
			location = '/cms/submit.asp?PageID='+PageID+'&Type=Delete'
		}
		if (Type == "News") {
			location = '/cms/submit.asp?PageID='+PageID+'&Type=DeleteNews&submittype=SubmitNews&NewsID='+TypeID
		}
		if (Type == "Conf") {
			location = '/cms/submit.asp?PageID='+PageID+'&Type=DeletConf&ConferenceID='+TypeID
		}
		if (Type == "Bookcat") {
			location = '/cms/submit.asp?PageID='+PageID+'&Type=DeleteBookCategory&BookCategoryID='+TypeID
		}
		if (Type == "BookType") {
			location = '/cms/submit.asp?PageID='+PageID+'&Type=DeleteBookType&BookTypeID='+TypeID
		}
		if (Type == "Book") {
			location = '/cms/submit.asp?PageID='+PageID+'&Type=DeleteBook&BookDetailsID='+TypeID
		}
		if (Type == "NewsArtDel") {
			location = '/cms/submit.asp?PageID='+PageID+'&Type=DeleteNewsArticle&NewslettersID='+TypeID
		}
		if (Type == "NewsIssDel") {
			location = '/cms/submit.asp?PageID='+PageID+'&Type=DeleteNewsIssue&NewsletterIssueID='+TypeID
		}
	} else {
		return false
	}
}

function DeleteAlert(PageID) {
	if (confirm("If you archive this page all child pages will be archived.")) {
		location = '/cms/submit.asp?PageID='+PageID+'&Type=Delete2'
		return false
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////
// CMS Javascript
////////////////////////////////////////////////////////////////////////////////////////////////

function checktitle(theForm, subType) {
	if(document.CMSForm.Title) {
		if(document.CMSForm.Title.value == ""){
			alert("Please enter a title")
		} else {
			SavePage(theForm,subType)
		}
	} else {
		SavePage(theForm,subType)
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////
// Forms Javascript
////////////////////////////////////////////////////////////////////////////////////////////////

function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function validRequired(formField,fieldLabel)
{
	var result = true;
	
	if (formField.value == "")
	{
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}
	
	return result;
}

function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	
	return result;
}

function validEmail(formField,fieldLabel,required)
{
	var result = true;
	
	if (required && !validRequired(formField,fieldLabel))
		result = false;

	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		formField.focus();
		result = false;
	}
   
  return result;

}

function validNum(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		if (!allDigits(formField.value))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}


function validInt(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var num = parseInt(formField.value,10);
 		if (isNaN(num))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}


function validDate(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var elems = formField.value.split("/");
 		
 		result = (elems.length == 3); // should be three components
 		
 		if (result)
 		{
 			var month = parseInt(elems[0],10);
  			var day = parseInt(elems[1],10);
 			var year = parseInt(elems[2],10);
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && (day > 0) && (day < 32) &&
					 allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
 		}
 		
  		if (!result)
 		{
 			alert('Please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.');
			formField.focus();		
		}
	} 
	
	return result;
}

////////////////////////////////////////////////////////////////////////////////////////////////
//Check date is valid and in correct dd/mm/yyyy format
////////////////////////////////////////////////////////////////////////////////////////////////

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

////////////////////////////////////////////////////////////////////////////////////////////////
//Validate user dates for updating and adding users
////////////////////////////////////////////////////////////////////////////////////////////////

function ValidateAddUserForm(theForm){
	if (!validRequired(theForm.Contact,"Contact"))
		return false;
		
	if (!validRequired(theForm.Firstname,"Firstname"))
		return false;
		
	if (!validRequired(theForm.Surname,"Surname"))
		return false;
		
	if (!validRequired(theForm.Username,"Username"))
		return false;

	if (!validRequired(theForm.Password,"Password"))
		return false;
					
	if(theForm.Joined.value != "") {
		if (isDate(theForm.Joined.value)==false){
			theForm.Joined.focus()
			return false
		}
	}
	if(theForm.AccreditationDate.value != "") {
		if (isDate(theForm.AccreditationDate.value)==false){
			theForm.AccreditationDate.focus()
			return false
		}
	}
	if(theForm.DateOfApplication.value != "") {
		if (isDate(theForm.DateOfApplication.value)==false){
			theForm.DateOfApplication.focus()
			return false
		}
	}
	if(theForm.DateOfApproval.value != "") {
		if (isDate(theForm.DateOfApproval.value)==false){
			theForm.DateOfApproval.focus()
			return false
		}
	}
	if(theForm.DateDiplomaIssued.value != "") {
		if (isDate(theForm.DateDiplomaIssued.value)==false){
			theForm.DateDiplomaIssued.focus()
			return false
		}
	}
	if(theForm.MostRecentAudit.value != "") {
		if (isDate(theForm.MostRecentAudit.value)==false){
			theForm.MostRecentAudit.focus()
			return false
		}
	}
	if(theForm.DateRegForTraining.value != "") {
		if (isDate(theForm.DateRegForTraining.value)==false){
			theForm.DateRegForTraining.focus()
			return false
		}
	}
	if(theForm.DateOfCourse.value != "") {
		if (isDate(theForm.DateOfCourse.value)==false){
			theForm.DateOfCourse.focus()
			return false
		}
	}
	if(theForm.DateCompletedTraining.value != "") {
		if (isDate(theForm.DateCompletedTraining.value)==false){
			theForm.DateCompletedTraining.focus()
			return false
		}
	}
	return true
}

function ValidateAddUserForm2(theForm) {
	if(theForm.DatePayment.value != "") {
		if (isDate(theForm.DatePayment.value)==false){
			theForm.DatePayment.focus()
			return false
		}
	}
	return true
}

////////////////////////////////////////////////////////////////////////////////////////////////
//Validate user dates for advanced search
////////////////////////////////////////////////////////////////////////////////////////////////

function ValidateAdvancedForm(theForm) {
	if(theForm.JoinedFrom.value != "") {
		if (isDate(theForm.JoinedFrom.value)==false){
			theForm.JoinedFrom.focus()
			return false
		}
	}
	if(theForm.JoinedTo.value != "") {
		if (isDate(theForm.JoinedTo.value)==false){
			theForm.JoinedTo.focus()
			return false
		}
	}
	if(theForm.AccreditationDateFrom.value != "") {
		if (isDate(theForm.AccreditationDateFrom.value)==false){
			theForm.AccreditationDateFrom.focus()
			return false
		}
	}
	if(theForm.AccreditationDateTo.value != "") {
		if (isDate(theForm.AccreditationDateTo.value)==false){
			theForm.AccreditationDateTo.focus()
			return false
		}
	}
	if(theForm.DateOfApplicationFrom.value != "") {
		if (isDate(theForm.DateOfApplicationFrom.value)==false){
			theForm.DateOfApplicationFrom.focus()
			return false
		}
	}
	if(theForm.DateOfApplicationTo.value != "") {
		if (isDate(theForm.DateOfApplicationTo.value)==false){
			theForm.DateOfApplicationTo.focus()
			return false
		}
	}
	if(theForm.DateDiplomaIssuedFrom.value != "") {
		if (isDate(theForm.DateDiplomaIssuedFrom.value)==false){
			theForm.DateDiplomaIssuedFrom.focus()
			return false
		}
	}
	if(theForm.DateDiplomaIssuedTo.value != "") {
		if (isDate(theForm.DateDiplomaIssuedTo.value)==false){
			theForm.DateDiplomaIssuedTo.focus()
			return false
		}
	}
	if(theForm.DateRegForTrainingFrom.value != "") {
		if (isDate(theForm.DateRegForTrainingFrom.value)==false){
			theForm.DateRegForTrainingFrom.focus()
			return false
		}
	}
	if(theForm.DateRegForTrainingTo.value != "") {
		if (isDate(theForm.DateRegForTrainingTo.value)==false){
			theForm.DateRegForTrainingTo.focus()
			return false
		}
	}
	if(theForm.DateCompletedTrainingFrom.value != "") {
		if (isDate(theForm.DateCompletedTrainingFrom.value)==false){
			theForm.DateCompletedTrainingFrom.focus()
			return false
		}
	}
	if(theForm.DateCompletedTrainingTo.value != "") {
		if (isDate(theForm.DateCompletedTrainingTo.value)==false){
			theForm.DateCompletedTrainingTo.focus()
			return false
		}
	}
	if(theForm.DatePaymentFrom.value != "") {
		if (isDate(theForm.DatePaymentFrom.value)==false){
			theForm.DatePaymentFrom.focus()
			return false
		}
	}
	if(theForm.DatePaymentTo.value != "") {
		if (isDate(theForm.DatePaymentTo.value)==false){
			theForm.DatePaymentTo.focus()
			return false
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////
//Validate user dates for exporting users
////////////////////////////////////////////////////////////////////////////////////////////////

function ValidateExportForm(theForm) {
	if(theForm.AccreditationDateFrom.value != "") {
		if (isDate(theForm.AccreditationDateFrom.value)==false){
			theForm.AccreditationDateFrom.focus()
			return false
		}
	}
	if(theForm.AccreditationDateTo.value != "") {
		if (isDate(theForm.AccreditationDateTo.value)==false){
			theForm.AccreditationDateTo.focus()
			return false
		}
	}
	if(theForm.DateOfApplicationFrom.value != "") {
		if (isDate(theForm.DateOfApplicationFrom.value)==false){
			theForm.DateOfApplicationFrom.focus()
			return false
		}
	}
	if(theForm.DateOfApplicationTo.value != "") {
		if (isDate(theForm.DateOfApplicationTo.value)==false){
			theForm.DateOfApplicationTo.focus()
			return false
		}
	}
	if(theForm.DateOfApprovalFrom.value != "") {
		if (isDate(theForm.DateOfApprovalFrom.value)==false){
			theForm.DateOfApprovalFrom.focus()
			return false
		}
	}
	if(theForm.DateOfApprovalTo.value != "") {
		if (isDate(theForm.DateOfApprovalTo.value)==false){
			theForm.DateOfApprovalTo.focus()
			return false
		}
	}
	if(theForm.DateDiplomaIssuedFrom.value != "") {
		if (isDate(theForm.DateDiplomaIssuedFrom.value)==false){
			theForm.DateDiplomaIssuedFrom.focus()
			return false
		}
	}
	if(theForm.DateDiplomaIssuedTo.value != "") {
		if (isDate(theForm.DateDiplomaIssuedTo.value)==false){
			theForm.DateDiplomaIssuedTo.focus()
			return false
		}
	}
	if(theForm.MostRecentAuditFrom.value != "") {
		if (isDate(theForm.MostRecentAuditFrom.value)==false){
			theForm.MostRecentAuditFrom.focus()
			return false
		}
	}
	if(theForm.MostRecentAuditTo.value != "") {
		if (isDate(theForm.MostRecentAuditTo.value)==false){
			theForm.MostRecentAuditTo.focus()
			return false
		}
	}
	if(theForm.DateRegForTrainingFrom.value != "") {
		if (isDate(theForm.DateRegForTrainingFrom.value)==false){
			theForm.DateRegForTrainingFrom.focus()
			return false
		}
	}
	if(theForm.DateRegForTrainingTo.value != "") {
		if (isDate(theForm.DateRegForTrainingTo.value)==false){
			theForm.DateRegForTrainingTo.focus()
			return false
		}
	}
	if(theForm.DateOfCourseFrom.value != "") {
		if (isDate(theForm.DateOfCourseFrom.value)==false){
			theForm.DateOfCourseFrom.focus()
			return false
		}
	}
	if(theForm.DateOfCourseTo.value != "") {
		if (isDate(theForm.DateOfCourseTo.value)==false){
			theForm.DateOfCourseTo.focus()
			return false
		}
	}
	if(theForm.DateCompletedTrainingFrom.value != "") {
		if (isDate(theForm.DateCompletedTrainingFrom.value)==false){
			theForm.DateCompletedTrainingFrom.focus()
			return false
		}
	}
	if(theForm.DateCompletedTrainingTo.value != "") {
		if (isDate(theForm.DateCompletedTrainingTo.value)==false){
			theForm.DateCompletedTrainingTo.focus()
			return false
		}
	}
	return true
}
