function validateEmail(email) {
    var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(String(email).toLowerCase());
}

function validatePhone(phone) {
    var re = /^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,7}$/im;
    return re.test(phone);
}

function instantPrint(divName) {
	var printContents = document.getElementById(divName).innerHTML;
	var originalContents = document.body.innerHTML;

	document.body.innerHTML = printContents;
	window.print();
	document.body.innerHTML = originalContents;
}

Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};

(function( $ ){
   $.fn.findGetParameter = function(options) {

		var result = null,
            tmp = [];
        var items = location.search.substr(1).split("&");
        for (var index = 0; index < items.length; index++) {
            tmp = items[index].split("=");
            if (tmp[0] === options.param) result = decodeURIComponent(tmp[1]);
        }
        $(this).val(result);
   }; 
})( jQuery );

(function($){
	$.fn.refreshCategoryMapping = function(options) {
		var defaultContentField = $(this);
		var defaultContent = $(defaultContentField).html();
		$.ajax({
		    type: 'POST',
		    url: 'contents/plugins/eedu_portal/ajax.php',
		    data: {action: 'get_category_mapping'},
		    success: function(data) {
		    	data = JSON.parse(data);
		    	if(data.error == 1) {
		    		$(defaultContentField).html(defaultContent);
		    	} else if(data.error == 0) {
		    		var html = '<table class="table table-bordered table-striped table-hover"><tr><th>Categories/Classes</th>';
		    		for(var classKey in data.classes) {
		    			html += '<th><a href="edit-class?id='+data.classes[classKey].id+'" class="popmeup" data-width=500 data-height=700>'+data.classes[classKey].class_name+'</a></th>';
			    	}
			    	html += '</tr>';
			    	for(var catKey in data.categories) {
	    				html += '<tr><td><a href="edit-category?id='+data.categories[catKey].id+'" class="popmeup" data-width=500 data-height=700>'+data.categories[catKey].category_name+'</a></td>';
	    				for(var classKey in data.classes) {
	    					var cat_map = data.category_mapping[data.classes[classKey].id];
	    					cat_id = parseInt(data.categories[catKey].id);
			    			html += '<td><input id="'+data.classes[classKey].class_name+'" type="checkbox" name="categories['+data.classes[classKey].id+']['+data.categories[catKey].id+']"';
			    			if(cat_map != undefined && cat_map.indexOf(cat_id) != -1) {
			    				html += ' checked="1"';
			    			}
			    			html += '></td>';
				    	}
	    				html += '</tr>';
	    			}
			    	html += '</table>';
		    		$(defaultContentField).html(html);
		    	}	
		    },
		    error: function(e) {
		    	$(defaultContentField).html(defaultContent);
		    }
		});
	}
})(jQuery);

(function($){
	$.fn.refreshMinimumSubject = function() {
		var defaultContentField = $(this).find("tbody");
		var defaultContent = $(defaultContentField).html();
		$.ajax({
		    type: 'POST',
		    url: 'contents/plugins/eedu_portal/ajax.php',
		    data: {action: 'get_minimum_subject'},
		}).done(function(data){
			try {
				data = JSON.parse(data);
				var i = 1;
				$(defaultContentField).html("");
				for(var key in data.minimum_subject) {
					var tr = $("<tr></tr>");
			    	$(tr).addElement({
			    		element: "td", content: i
			    	}).addElement({
			    		element: "td", content: data.minimum_subject[key].term_name+" ["+data.minimum_subject[key].session_name+"]"
			    	}).addElement({
			    		element: "td", content: data.minimum_subject[key].class_name
			    	}).addElement({
			    		element: "td", content: data.minimum_subject[key].category_name
			    	}).addElement({
			    		element: "td", content: data.minimum_subject[key].minimum_subject
			    	}).addElement({
			    		element: "td", content: '<a href="javascript:void(0);" class="remove_post_item" data-callback="refreshMinimumSubject" data-callbackid=".minimum_subject_table" data-id="'+data.minimum_subject[key].id+'">Remove</a>'
			    	});
			    	$(defaultContentField).append(tr);
			    	i++;
				}
		    	
			}
			catch(err) {
				console.log(err);
				console.log(data);
				$(defaultContentField).html(defaultContent);
			}
			finally {

			}
		}).fail(function(e){
			$(defaultContentField).html(defaultContent);
		});
	}

	$.fn.getSubjectBranch = function() {
		var e = $(this);
		$.ajax({
			type: "post",
			url: "contents/plugins/eedu_portal/ajax.php",
			data: {action: "get_subject_branch"}
		}).done(function(data){
			try {
				data = JSON.parse(data);
				$(e).html(data);
			}
			catch (err) {
				console.log(err);
				console.log(data);
			}
			finally {}
		})
	}

	$.fn.getCourses = function() {
		var e = $(this);
		$.ajax({
			type: "post",
			url: "contents/plugins/eedu_portal/ajax.php",
			data: {action: "get_courses"}
		}).done(function(data){
			try {
				data = JSON.parse(data);
				$(e).html(data);
			}
			catch (err) {
				console.log(err);
				console.log(data);
			}
			finally {}
		})
	}

	$.fn.getNextStudentId = function() {
		var e = $(this);
		$.ajax({
			type: "post",
			url: "contents/plugins/eedu_portal/ajax.php",
			data: {action: "get_next_student_id"}
		}).done(function(data){
			try {
				data = JSON.parse(data);
				$(e).clearForm();
				$(e).find("#username, #password").val(data);
				$("#latest-students").getLatestUsers("students");
			}
			catch (err) {
				console.log(err);
				console.log(data);
			}
			finally {}
		})
	}

	$.fn.getLatestUsers = function(role) {
		var e = $(this);
		$.ajax({
		    type: 'POST',
		    url: 'contents/plugins/eedu_portal/ajax.php',
		    data: {action: 'get_latest_users', role: role},
		}).done(function(data){
			try {
				data = JSON.parse(data);
				$(e).html(data);
			}
			catch(err) {
				console.log(err);
				console.log(data);
			}
			finally {
				// console.log(data);
			}
		});
	}
})(jQuery);