

function validateDate(strDate) {	
  //This class is used to do the basic data validations and other date functions.
  //The constructor accepts a string parameter in the dd/mm/yyyy format.
  
  //Each object of this class will contail the following properties
  this.intDate = 0;   //This property holds the Date part
  this.intMonth = 0;    //This property holds the Month part
  this.intYear = 0;   //This property holds the Year part
  this.isDate = true;   //This property determines whether it is a valid date or not
  this.yyyymmdd = "";   //This property holds the given Date in yyyymmdd format

  var arrMonths = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
  var arrDtElements = trim(strDate).split("/");

  if (arrDtElements.length != 3 || !isInteger(padLZero(trim(arrDtElements[0]))) || 
    !isInteger(padLZero(trim(arrDtElements[1]))) || !isInteger(padLZero(trim(arrDtElements[2])))){  
  	this.isDate=false;
  }  
  else {
    this.intMonth = parseInt(padLZero(trim(arrDtElements[0])));
    this.intDate = parseInt(padLZero(trim(arrDtElements[1])));
    this.intYear = parseInt(padLZero(trim(arrDtElements[2])));
    if (this.intYear % 4 || (!(this.intYear % 100) && (this.intYear % 400))) arrMonths[1] = 28;
    if (this.intYear < 1900 || this.intYear > 9999 || this.intMonth < 1 || this.intMonth > 12 || this.intDate < 1 || this.intDate > arrMonths[this.intMonth - 1]){
    	this.isDate=false;
    }

  }
  if(!this.isDate){
  	alert("The date you entered was not formatted correctly. Please enter your date in the format mm/dd/yyyy.");
  	return true;	
  }
  return false;	  
}
function compareDates(fromDate,toDate){
	if(fromDate != null && toDate != null && fromDate != "" && toDate != "") {
		var isLess=true;
		var fromDateElements=trim(fromDate).split("/");
		var toDateElements=trim(toDate).split("/");
		if(parseInt(padLZero(trim(fromDateElements[2])))>parseInt(padLZero(trim(toDateElements[2]))))
			isLess=false;
		else if(parseInt(padLZero(trim(fromDateElements[2])))==parseInt(padLZero(trim(toDateElements[2])))){
			if(parseInt(padLZero(trim(fromDateElements[0])))>parseInt(padLZero(trim(toDateElements[0]))))
				isLess=false;
			else if(parseInt(padLZero(trim(fromDateElements[0])))==parseInt(padLZero(trim(toDateElements[0])))){
				if(parseInt(padLZero(trim(fromDateElements[1])))>parseInt(padLZero(trim(toDateElements[1]))))
					isLess=false;
			}	
		}
		if(isLess)
			return false;
		return true;
	}
		
}
function padLZero(strValue) {
  //This function return a string after removing the leading zeros from the string parameter
  
  var reLeadingZero = /^0*/;
  return strValue.replace(reLeadingZero, "")
}
function isInteger(strValue) {

  var reInteger = /^\d*$/;
  if(strValue=="")
  	return false;
  return reInteger.test(strValue);
}
function isRadioChecked(radioName,count){
	if(count==1){
		if(radioName.checked)
			return false;
	}
	else{
		for(var i=0;i<radioName.length;i++)
			if(radioName[i].checked)
				return false;
	}			
	return true;
}
function emptyFieldValidation(txtInput)
{
	
	if(!txtInput.length) {		
		return true;
	}else
		return false;
}

function splCharFieldValidation(txtInput)
{
	var emptyFldValidation=/["'~]/;	
	if(emptyFldValidation.test(txtInput)) {		
		return true;
	}else
		return false;
}
function validateUrl(urlInput)
{
	 var urlValidation=/[A-Z'~`:;"\<\>\?,.\s(){}\[\]\+\\|!@#\$\^%?\*\&\/\=]/;
	 //alert("1234 " + urlValidation.test(urlInput));
	 if(urlValidation.test(urlInput))
	  return true;
	 else
	  return false;
}
function fieldLengthValidation(inputLength,prescribedLength)
{
	if(inputLength>prescribedLength)
	 return true;
	else
	return false;
}
function trim(txtInput)
{
	if(txtInput != null && txtInput != "") {	
		txtInput=txtInput.replace(/^[\s]+/,'');
		txtInput=txtInput.replace(/[\s]+$/,'');
	}
	return txtInput;
	
}
function search(beginIndex,endIndex,searchElement,elementArray)
{
	for(j=beginIndex-1;j>=endIndex;j--)
	{
 		if(searchElement==elementArray[j].value)
 		{
 			return true;			
 		}
	} 
	return false;		
}
function validateEmail(email){
//Changed the email pattern to match exactly two characters after the first '.'
	emailPattern = /^[\w\d\-\.]+\@[\w\d\-]+\.[a-zA-Z\d\-.]{2,}$/;
//	emailPattern= /^[\w\.-]+@[\w\.-]+\.[a-zA-Z]+$/;
	if(!emailPattern.test(email))
		return false;
	else
		return true;
}
function sectionValidations(form,document,sections,operationType)
{	
 var browserTypeIE = false;
 var count= document.getElementsByName('sectionName').length;   	
 var nameLenth,nameChartest,urlLenth,urlChartest;
 var tmp=0;
 
 /*checks whether the browser is IE or not*/
 if ((index = navigator.userAgent.indexOf("MSIE")) >= 0)
 {
 	browserTypeIE = true;
 }
 
 if(sections=='')
 	tmp=1;
  
 if(operationType=="EditCourse"||operationType=="CopyCourse"||operationType=="AddSection"){
   	tmp=0;   	 
  }
  

 for(i=tmp;i<count;i++)
 {	
  
  if(operationType=="AddSection" || count == 1)
  {
   document.getElementById('sectionNameId').value=trim(document.getElementById('sectionNameId').value);
   document.getElementById('sectionUrlId').value=trim(document.getElementById('sectionUrlId').value);
   nameLenth = document.getElementById('sectionNameId').value.length;
   nameChartest = document.getElementById('sectionNameId').value;
   urlLenth = document.getElementById('sectionUrlId').value.length;
   urlChartest = document.getElementById('sectionUrlId').value;
  }
  else
  {
   if(operationType=='CopySection'){
      	count=1;
      	if(document.getElementById("linkToInstructor").style.display==""){
      		alert(validateEmail(document.copySectionForm.secondaryInstructor[0].value));
      		if(validateEmail(document.copySectionForm.secondaryInstructor[0].value)){
   			alert("Please enter a valid email address");
   			return false;
   		}
   		
   	}else
   	if(document.getElementById("unlinkToInstructor").style.display==""){
   	alert(validateEmail(document.copySectionForm.secondaryInstructor[0].value));
   		if(validateEmail(document.copySectionForm.secondaryInstructor[0].value)){
   			alert("Please enter a valid email address");
   			return false;
   		}
   		break;
   	}else
   	if(document.getElementById("copyToOwnCourse").style.display==""){
   			i=1;tmp=1;
   	}
   }
   form.sectionName[i].value=trim(form.sectionName[i].value);
   form.sectionUrl[i].value=trim(form.sectionUrl[i].value);
   nameLenth = form.sectionName[i].value.length;
   nameChartest = form.sectionName[i].value;
   urlLenth = form.sectionUrl[i].value.length;
   urlChartest = form.sectionUrl[i].value;
  }
  
  
  if(emptyFieldValidation(nameChartest))
  {   
   alert("Section Name cannot be left blank ");      			 
   return false; 
  }
   var splCharacter = '(\" \' ~).';
  if(splCharFieldValidation(nameChartest)){
  	alert("Section name cannot contain special characters " + splCharacter);
  	return false; 
  }
   
  if(fieldLengthValidation(nameLenth,100))
  {
    alert('Section Name cannot be more than 100 characters long.');       				
    return false; 
  }
  
  
  if(i!=tmp)
  {
   if(search(i,tmp,nameChartest,form.sectionName))
   {
    alert('Please make sure each section name is unique.');       				
    return false;	
   } 
  }    	
  
  if((!urlLenth))
  { 
   alert("Section Web Address cannot be left blank or contain special characters.");      			
   return false; 
  }
  
  
  if(!fieldLengthValidation(urlLenth,100))
  {
   if(validateUrl(urlChartest))
   {
    //var splChar = '(\/ \' \" ~)';
    alert("The section Web Address should contain only alphanumeric (lowercase), hyphen (-) or underscore (_) .");
    return false;
   }
  }
  else
  {
   alert('Section Web Address cannot be more than 100 characters long.'); 
   return false; 
  }
  if(search(i,tmp,urlChartest,form.sectionUrl))
  {
   alert('Please make sure each section Web Address is unique.');
   return false;	
  } 	
 }
 return true;
}

function timeValidation(inthour1,intmin1,intam1,inthour2,intmin2,intam2){
	if(intam2=="PM" ){
				if(inthour2!=12)
				{
					var time2 = (inthour2 +12)* 60 +  parseInt(intmin2) ;
							
				}else
					{
					var time2 = (0+12)*60+  parseInt(intmin2);
					}
	
			}else  
				{ 
					if(inthour2!=12)
					{
						var time2 = inthour2 * 60 +  parseInt(intmin2) ;
								
					}else{
						var time2 = 0*60+  parseInt(intmin2);
					}
				}
					
	if(intam1=="PM" ){
				if(inthour1!=12)
				{
					var time1 = (inthour1 + 12) * 60 + parseInt(intmin1); 
							
				}else{
					var time1 = (0+12)*60+ parseInt(intmin1);
					}	
			}else {	
	 			if(inthour1!=12)
	 			{
					var time1 = inthour1 * 60 + parseInt(intmin1) ;
							
				}else{
					var time1 = 0*60+ parseInt(intmin1);
					}
			}		
	if(time2>time1) 
	{
		return true;
	}else{
		return false;
	}
}

function specialCharacterValidation(txtInput)
{
	var emptyFldValidation=/['~`(){}\[\]\+\-\\|!@#_\$\^%?\*]/;
	if(emptyFldValidation.test(txtInput))
	return true;
	else
	return false;
}

function htmltagvalidation(message){
var browserName=navigator.appName; 
	if (browserName=="Microsoft Internet Explorer")
	{
		if(message!=" " || message!=null)
		{
			
			var i = message.indexOf("<p>");
			var j = message.indexOf("</p>");
			if(i >=0 || j>=0) {
						alert("<p> tag is not supported by "+browserName);
						return false;
			}
			var i = message.indexOf("<h1>");
			var j = message.indexOf("</h1>");
			if(i >=0 || j>=0) {
						alert("<h1> tag is not supported by "+browserName);
						return false;
			}
			var i = message.indexOf("<h2>");
			var j = message.indexOf("</h2>");
			if(i >=0 || j>=0) {
						alert("<h2> tag is not supported by "+browserName);
						return false;
			}
			var i = message.indexOf("<h3>");
			var j = message.indexOf("</h3>");
			if(i >=0 || j>=0) {
						alert("<h3> tag is not supported by "+browserName);
						return false;
			}
			var i = message.indexOf("<h4>");
			var j = message.indexOf("</h4>");
			if(i >=0 || j>=0) {
						alert("<h4> tag is not supported by "+browserName);
						return false;
			}
		}
		
	}
	return true;
	
}
 
function escapeBackslashChars(inputString)
{
  return  inputString.replace(/(\w*)([\\'])(\w*)/g,"\$1\\\$2\$3");
}

function getCookie(cookieName) {
	var myCookie = document.cookie;
	var prefix = cookieName + "=";
	var begin = myCookie.indexOf("; " + prefix);
	if (begin == -1) {
		begin = myCookie.indexOf(prefix);
		if (begin != 0) return null;
	} else {
		begin += 2;
	}
	
	var end = myCookie.indexOf(";",begin);
	if (end == -1) end = myCookie.length;
	
	var returnString = unescape(myCookie.substring(begin + prefix.length, end));
	if (returnString) {
		return returnString;
	} else {
		return false;
	}
}

function setCookie(cookieName, cookieValue) {
	var nextyear = new Date();
	nextyear.setFullYear(nextyear.getFullYear()+1);
	document.cookie = cookieName + "=" + escape(cookieValue) + "; path=/; expires=" + nextyear.toGMTString();
}

function deleteCookie(cookieName) {
	var lastyear = new Date();
	lastyear.setFullYear(lastyear.getFullYear()-1);
	
	// First try deleting without specifying a domain:
	document.cookie = cookieName + "=''; value=''; path=/; expires=" + lastyear.toGMTString();

	if (getCookie(cookieName)) {
		// that didn't work, try removing the subdomain:
		var theHost = location.host;
		var hostBits = theHost.split("\.");
		var shortHost =  "." + hostBits[hostBits.length-2] + "." + hostBits[hostBits.length-1];
		if (shortHost.indexOf(":") > -1) { 
			shortHost = shortHost.substring(0,shortHost.indexOf(":"));
		}
		document.cookie = cookieName + "=''; value=''; domain=" + shortHost + "; path=/; expires=" + lastyear.toGMTString();
	}
}

function validateDateForRegistration(date, oldDate, todaysDate, errorMsg, id, courseStartDate, courseEndDate) {
	var splittedDate = date.split("/");
	if(splittedDate.length == 3 && date.length <=10 && date.indexOf(" ") ==-1) {
		if(splittedDate[0] > 0 && splittedDate[0] <= 12 && splittedDate[2].length == 4) {
			if(splittedDate[1] >0 && splittedDate[1] <= 31) {
				if(!isValidDateField(splittedDate[1],splittedDate[0],splittedDate[2])) {
					alert("The date you entered was not formatted correctly. Please enter your date in the format mm/dd/yyyy.");
					document.getElementById(id).focus();
					return false;
				}
			} else {
				alert("The date you entered was not formatted correctly. Please enter your date in the format mm/dd/yyyy.");
				document.getElementById(id).focus();
				return false;
			}
		} else {
			alert("The date you entered was not formatted correctly. Please enter your date in the format mm/dd/yyyy.");
			document.getElementById(id).focus();
			return false;
		}
		if(splittedDate[0].length == 1) {
			splittedDate[0] = "0" + splittedDate[0];
		}
		if(splittedDate[1].length == 1) {
			splittedDate[1] = "0" + splittedDate[1];
		}
		date = splittedDate[2] + splittedDate[0] + splittedDate[1];
		if(oldDate != "" && oldDate != "mm/dd/yyyy" && oldDate == date) {
			return true;
		}/* else if(date < todaysDate) {
			alert(errorMsg + " should be greater than or equal to todays date");
		} */else if(date < courseStartDate) {
			alert(errorMsg + " should be greater than Course Start Date");
		} else if(date > courseEndDate) {
			alert(errorMsg + " should be less than Course End Date");
		} else {
			return true;
		}
		document.getElementById(id).focus();
		return false;
	}
	alert("The date you entered was not formatted correctly. Please enter your date in the format mm/dd/yyyy.");
	document.getElementById(id).focus();
	return false;
}

function isValidDateField(date, month, year) {
	var monthArr = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	if(year % 4 == 0) {
		monthArr[1]=29;
	}
	if(date > monthArr[month-1]) {
		return false;
	}
	return true;
}

function validateEmailWithLDAPCheck(username, isLDAPCheck){
 	emailPattern= /^[\w\d\-\.]+\@[\w\d\-]+\.[a-zA-Z\d\-.]{2,}$/;
 	if(!emailPattern.test(username)) {
 		if(isLDAPCheck == "true" && username.indexOf('@')==-1 && username.indexOf("_") >=0) {
 			allowedCharacters = /^[a-zA-Z0-9-._]+\_[a-zA-Z0-9-._]+$/;
			if(allowedCharacters.test(username)) {
 				return true;
 			}
 		}
 		return false;
 	}
 	else
 		return true;
}
function displayToolTip(linkref,bodyId) {
	var offset = $(linkref).offset();
	tooltiptext=$(linkref).attr("title");					
	$(".course_tooltip > .course_header > b").html(tooltiptext);
	$(".course_tooltip > .course_body").html($("#"+bodyId).html());
	$(".course_mainctr").css({"top":(offset.top)-5,"left":(offset.left) + 75});
	$(".course_mainctr").show();
}
function printRegInfo(elementId) {
	str=$("#"+elementId).html();
	var WinPrint =window.open('','','left=0,top=0,width=1,height=1,t oolbar=0,scrollbars=0,status=0');
	WinPrint.document.write(str);
	WinPrint.document.close();
	WinPrint.focus();
	WinPrint.print();
	WinPrint.close();
}
function closeTooltip() {
	$("#course_mainctr").hide();
}
function resizeFaceboxOverlay() {
  	var faceboxPos = parseInt($("#facebox").css("top").replace("px",""));
  	var faceboxHeight = $("#facebox").height();
  	var documentHeight = document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight;
  	var newDocumentHeight = documentHeight +((faceboxPos+faceboxHeight)-documentHeight)+40;
  	var modalHeight = newDocumentHeight > documentHeight ? newDocumentHeight : documentHeight + 'px';
  	$("#facebox_overlay").css("height",modalHeight);
}