// Function to Check all the Checkbox's present before the records listed
function checkFunc(CheckBoxControl)
{	  
	var a = 0;
	for (i=0; i<document.listView.elements.length; i++) {
		if (document.listView.elements[i].type == "checkbox" && document.listView.elements[i].id.indexOf('chkOffer') < 0) {
			document.listView.elements[i].checked=CheckBoxControl.checked;
			a = a + 1;
		}
	}
	if (CheckBoxControl.checked == true) {
		a = a - 1;
	} else {
		a = 0;
	}
	
	document.getElementById("selectCount").innerHTML = a;		
	if(document.getElementById("selectCount1")) {
		document.getElementById("selectCount1").innerHTML = a;
	}
}	

// Function to generate the selected Checkbox's Count	
function select_count()
{
	var a = 0;
	for (i=0; i<document.listView.elements.length; i++) {
		if (document.listView.elements[i].type == "checkbox") {
			if (document.listView.elements[i].checked == true && document.listView.elements[i].id.indexOf('chkOffer') < 0)
				a = a + 1;
		}
	}
	if (document.listView.checkAll.checked == true)	{
		a = a-1;
	}
	document.getElementById("selectCount").innerHTML = a;
	if(document.getElementById("selectCount1")) {
		document.getElementById("selectCount1").innerHTML = a;
	}
	document.listView.checkAll.checked = false;			
}

// Function to Clear all the Checkbox selected
function clearfunc()
{
	for (i=0; i<document.listView.elements.length; i++) {
		if (document.listView.elements[i].type == "checkbox") {
			document.listView.elements[i].checked = false;
		}
	}
	document.getElementById("selectCount").innerHTML = 0;
	document.getElementById("selectCount1").innerHTML = 0;
}

// Function to confirm action
function confirmAction(msg1, msg2, href) {
	var answer = confirm(msg1)
	if (answer) window.location = href;
	else alert(msg2);
}

// Function to confirm before delete
function isActionValid(action)
{
	var msg1 = "Are you sure to "+action+" ? ";
	
	var isValid = window.confirm(msg1);
		
	return isValid;
}

// Function to validate the form before delete operation executes
function formValidator(frmName, actn)
{	
	var a=0;	
	for(i=0; i<document.listView.elements.length; i++)
	{
		if(document.listView.elements[i].type=="checkbox"  && document.listView.elements[i].name != "checkAll")
		 {
		 	if(document.listView.elements[i].id.indexOf('chkOffer') >= 0)
		 		continue;
			if (document.listView.elements[i].checked == true)
			a = a + 1;
		 }
	}	

	if (a < 1) {
		var msg = 'record';
		if(actn == 'Offer' || actn == 'Exempt')
			msg = 'student';
		alert("Select at least one " + msg +" to "+actn);
		return false;
	}
	else if (actn == "delete")
	{		  
			var z = window.confirm("Are you sure to "+actn+" "+a+" record(s) ?");
    
			return z;
	}
	return true;
}

function validateCheckbox(frmName,action)
{
	var a=0;		
	if(frmName!='') {
		for(i=0; i<frmName.elements.length; i++) {
			if(frmName.elements[i].type=="checkbox"  && frmName.elements[i].name != "checkAll"){			    
				if (frmName.elements[i].checked == true)
				a = a + 1;
			}			
		}		
		if(a < 1)  {
			alert("Select at least one record to "+action);
			return false;
		}
		else {
			var z = window.confirm("Are you sure to "+action+" "+a+" record(s) ?");
			return z;
		}
		return true;
	}
}

function validateRadioButton(frmName,action)
{
	var a=0;		
	if(frmName!='') {
		for(i=0; i<frmName.elements.length; i++) {
			if(frmName.elements[i].type=="radio" ){			    
				if (frmName.elements[i].checked == true)
				a = a + 1;
			}			
		}		
		if(a < 1)  {
			alert("Select at least one "+action);
			return false;
		}
		return true;
	}
}

//Function to reset values and clear error messages
function resetValues(formName)
{  	
        var x;
        formName.reset();
        //Reset the values of editor
        var editor = document.getElementsByTagName('iframe');
        for (count = 0 ; count < editor.length; count++){
	        if(editor[count]){
	            if(editor[count].id.indexOf('wysiwyg')>=0){
		            elementId = editor[count].id.substr(7);
		        	txtArea = document.getElementById(elementId);
		            doc = editor[count].contentWindow.document;     
		            doc.open();
		  			doc.write(txtArea.value);
		  			doc.close();
	            }
	        }
        }
        var errSpan = document.getElementsByTagName("span");   
        for(x=0;x<errSpan.length;x++){
            if(! errSpan[x].id) continue;
            if(errSpan[x].id.indexOf("Err")>0){
               if(errSpan[x].innerHTML)
                errSpan[x].innerHTML= "";
            }
        }
       
}   

//Function to convert Date to Ymd Format from dmy format
function convertDateToYMDFromDMY( inputDate )
{
    var dateArr = inputDate.split(/-|\/|\./);
    var day = dateArr[0];
    var month = dateArr[1];
    var year = dateArr[2];
    var ymdFormat = year + "/" + month + "/" + day;
    return ymdFormat;
}

//function to confirm message before print receipt / print invoice.
function confirmPrint(urlVal)
{
	if (confirm("There will be data loss if you navigate away while in the middle of application process"))
	   location.href = urlVal;	
}

/*
Function to check entered key is tab or enter to allow theses characters in file upload fields.
It allows user to use Enter, Tab, Shift+Tab for file upload fields.
*/

function checkTabKeys(e)
{
	var c = (e.charCode) ? e.charCode : ((e.keyCode)? e.keyCode:(e.which)?e.which:0)
	if (c==8 || c==9 || c==13) {
		return true;
	} else {
		return false;
	}
}

function autoSelection(rows, opt) {
	for(var i=0; i<rows; i++) {
		document.getElementById('e'+i).selected = true;
	  if(opt>0) {
		document.getElementById('c'+i).selected = true;
		document.getElementById('g'+i).selected = true;
	  }
	}
}

function autoAddSubtract(rows, sels, opt) {
	var no = document.getElementById('semCount').value;
	if(opt==1) no++;
	else no--;
	if(no < 0) no = sels-1;
	else if(no > sels-1) no = 0;
	for(var i=0; i<rows; i++) document.getElementById('s'+i+'-'+no).selected = true;
	document.getElementById('semCount').value = no;
}

/* Functions used to get module list on change of course */
function getModuleOption(courseId)
{
	resetSelectBox(slctModule);
	if(courseId == '')
		return;
	agent.call('', 'getModuleOption', 'showModuleList', courseId);
}
function showModuleList(mdlList)
{
	resetSelectBox(slctModule);
	for(moduleCnt = 0; moduleCnt < mdlList.length; moduleCnt++ ) {
		slctModule.options[moduleCnt+1] = new Option(mdlList[moduleCnt]['module_code'], mdlList[moduleCnt]['module_id']);
	}
}

/* Functions used to get workshop list on change of course */
function getWorkshopOption(courseId)
{
	resetSelectBox(slctWorkshop);
	if(courseId == '')
		return;
	agent.call('', 'getWorkshopOption', 'showWorkshopList', courseId);
}
function showWorkshopList(mdlList)
{
	resetSelectBox(slctWorkshop);
	for(workshopCnt = 0; workshopCnt < mdlList.length; workshopCnt++ ) {
		slctWorkshop.options[workshopCnt+1] = new Option(mdlList[workshopCnt]['workshop_code'], mdlList[workshopCnt]['workshop_id']);
	}
}

/* Functions used to get student list on change of course */
function getStudentOption(courseId)
{
	resetSelectBox(slctStudent);
	if(courseId == '')
		return;
	agent.call('', 'getStudentOption', 'showStudentList', courseId);
}
function showStudentList(mdlList)
{
	resetSelectBox(slctStudent);
	for(studentCnt = 0; studentCnt < mdlList.length; studentCnt++ ) {
		slctStudent.options[studentCnt+1] = new Option(mdlList[studentCnt]['userFullName'], mdlList[studentCnt]['student_id']);
	}
}

/* Functions used to get intake list on change of course */
function getIntakeOption(courseId)
{
	resetSelectBox(slctIntake);
	if(courseId == '')
		return;
	agent.call('', 'getIntakeOption', 'showIntakeList', courseId);
}
function showIntakeList(mdlList)
{
	resetSelectBox(slctIntake);
	for(intakeCnt = 0; intakeCnt < mdlList.length; intakeCnt++ ) {
		slctIntake.options[intakeCnt+1] = new Option(mdlList[intakeCnt]['intake_name'], mdlList[intakeCnt]['intake_id']);
	}
}

/* Functions used to get intake dates on change of intake */
function getIntakeDate(intakeId)
{   
	agent.call("", "getIntakeDate", "showIntakeDate", intakeId);
}
function showIntakeDate(obj)
{        
	document.getElementById('userStartDate').value = obj[0];
	document.getElementById('userEndDate').value = obj[1];
}

function resetSelectBox(slctObj)
{
	slctObj.options.length = 0;		
	slctObj.options[0] = new Option('<-- Select -->', '');
}

// Calculate and adjust iframe height
// Firefox worked fine. Internet Explorer shows scrollbar because of frameborder
function resizeFrame(f, offset) {
	f.style.height = 0;
	f.style.height = (document.body.scrollHeight * 2 + 100 - offset) + "px";
}