var site_url = 'http://' + location.host + '/';
var ajax_url = site_url + 'asynch/';
//var class_url = site_url + 'classes/';
var css_url = site_url + 'css/';
var image_url = site_url + 'images/';
var site_name = "Mannar Thirumalai Naicker College";
var imageSlideTimer = null, scrollTimer;
var imageTimerSpeed = 30;
var imageSlideSpeed = 25;
var imageWidth = 175;	// 175 - single image with margin
var stopPoint = 0;
var galleryContainer, images, galleryContainerWidth, galleryCurrentPosition, nextArrow, previousArrow, profileViewer;
var ie = (document.all)? true:false;
var infoId, progressId;
var monthsAsc = new Array();
	monthsAsc["Jan"] = 1;
	monthsAsc["Feb"] = 2;
	monthsAsc["Mar"] = 3;
	monthsAsc["Apr"] = 4;
	monthsAsc["May"] = 5;
	monthsAsc["Jun"] = 6;
	monthsAsc["Jul"] = 7;
	monthsAsc["Aug"] = 8;
	monthsAsc["Sep"] = 9;
	monthsAsc["Oct"] = 10;
	monthsAsc["Nov"] = 11;
	monthsAsc["Dec"] = 12;	
var months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');

function getXMLHttp() {
	if(window.XMLHttpRequest) {
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
		showAlert('Please Upgrade Your Browser', 'close', '');
		return false;
	}
}

function setGalleryThumbWidth () {
	galleryContainer = document.getElementById('idGalleryImages');
	images = galleryContainer.getElementsByTagName('img');
	galleryContainer.style.width = images.length * 175 + 'px';	// 175 - single image with margin(width 125px, margin 25px)
}

function ajaxRequest(params, url, method, dataId, async, progressId, actionAfterRespond, isClearForm, f) {
	var xmlHttp = getXMLHttp();
	method = method.toUpperCase();
	
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4) {
			if(xmlHttp.status == 200) {
				var response = xmlHttp.responseText;
				if(progressId !='') {				
					document.getElementById(progressId).innerHTML = '&nbsp;';
				}
				
				if(document.getElementById(dataId)) {
					document.getElementById(dataId).innerHTML = response;
				}
				
				if(isClearForm) {
					clearForm(f);
				}
				
				if(actionAfterRespond != '') {
					eval(actionAfterRespond);
				}
			}
		}
		else {
			if(progressId !='') {				
				document.getElementById(progressId).innerHTML = '<img src="'+ image_url +'loading.gif" />';
			}
		}
	}
	
	if(method == "GET") {
		xmlHttp.open(method, url + '?' + params, async);
		xmlHttp.send(null);
	}
	else {
		xmlHttp.open(method, url, async);
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlHttp.send(params);
	}
}

function moveImage(action) {
	if(imageSlideTimer != null) {
		return;
	}
	
	galleryContainer = document.getElementById('idGalleryImages');
	galleryContainerWidth = getValueFromPixel(galleryContainer.style.width);
	galleryCurrentPosition  = getValueFromPixel(galleryContainer.style.marginLeft);
	
	nextArrow = document.getElementById('idGalleryRightArrow');
	previousArrow = document.getElementById('idGalleryLeftArrow');
	
	if(action == 'next') {
		// -1 because the container is moving left side that is in minus value	
		if(galleryCurrentPosition == (-1 * (galleryContainerWidth / 2)) && galleryCurrentPosition != 0) {
			return;
		}
		stopPoint = galleryCurrentPosition - 175;
		previousArrow.style.visibility = 'visible';
		imageSlideTimer = setInterval('nextImage()', imageTimerSpeed);
	}
	else if(action = 'previous') {
		if(galleryCurrentPosition == 0) {
			return;
		}
		stopPoint = galleryCurrentPosition + 175;
		nextArrow.style.visibility = 'visible';
		imageSlideTimer = setInterval('previousImage()', imageTimerSpeed);
	}
}

function nextImage() {
	galleryCurrentPosition = getValueFromPixel(galleryContainer.style.marginLeft);
	
	if(stopPoint == galleryCurrentPosition) {
		clearInterval(imageSlideTimer);
		imageSlideTimer = null;
	}
	else {
		galleryContainer.style.marginLeft = galleryCurrentPosition - imageSlideSpeed + 'px';
		
		if(getValueFromPixel(galleryContainer.style.marginLeft) == (-1 * (galleryContainerWidth / 2))) {
			nextArrow.style.visibility = 'hidden';			//Hide the previous button if there is no image to slide Next
		}
	}
}

function previousImage() {
	galleryCurrentPosition  = getValueFromPixel(galleryContainer.style.marginLeft);
	
	if(stopPoint == galleryCurrentPosition) {
		clearInterval(imageSlideTimer);
		imageSlideTimer = null;
	}
	else {
		galleryContainer.style.marginLeft = galleryCurrentPosition + imageSlideSpeed + 'px';
		if(getValueFromPixel(galleryContainer.style.marginLeft) == 0) {
			previousArrow.style.visibility = 'hidden';		//Hide the previous button if there is no image to slide previous
		}
	}
}

function getValueFromPixel(pixelValue) {
	return parseInt(pixelValue.substr(0, pixelValue.length - 2));		//substr(indexFrom, Number of Characters)
}

function scrollPage(side, limit) {
	if(side == 'up') {
		scrollTimer = setInterval('scrollUp(' + limit + ')', 10);
	}
	else if(side == 'down') {
		scrollTimer = setInterval('scrollDown(' + limit + ')', 10);
	}
}

function scrollUp(limit) {
	
}

function scrollDown(limit) {
	
}

function changeBlock(id) { 
	document.getElementById(document.getElementById('hid_current_block').value).className = 'clsDisplayNone';
	document.getElementById(document.getElementById('hid_current_block').value + 'Link').className = '';
	
	document.getElementById(id + 'Link').className = 'clsCurrentSideLink';
	document.getElementById(id).className = 'clsDisplayBlock';
	document.getElementById('hid_current_block').value = id;
}

function changeBlockInTitle(id) {
	document.getElementById(document.getElementById('hid_current_block').value).className = 'clsDisplayNone';
	document.getElementById(document.getElementById('hid_current_block').value + 'Link').style.backgroundColor = '#FFFFFF';
	document.getElementById(document.getElementById('hid_current_block').value + 'Link').style.color = '#113340';
	
	document.getElementById(id + 'Link').style.backgroundColor = '#113340';
	document.getElementById(id + 'Link').style.color = '#FFFFFF';
	document.getElementById(id).className = 'clsDisplayBlock';
	document.getElementById('hid_current_block').value = id;
}

function showProfile (folder, department, course, profile, name) {
	var path = image_url + folder + '/' + department + '/' + course + '/' + profile + '.jpg';
	var image = document.getElementById('idProfilerImg');
	image.src = path;
	document.getElementById('idProfile').innerHTML = name;
	
	profileViewer = document.getElementById('idProfileViewer');
	profileViewer.className = 'clsBringFront';
}

function closeProfiler() {
	if(document.getElementById('idProfilerImg')) {
		var image = document.getElementById('idProfilerImg');
		image.src = '';
	}
	document.getElementById('idProfileViewer').className = 'clsSentBack';
}

function redirect(url) {
	window.location = url;
}

function hideInfo(id) {
	document.getElementById(id).className = 'clsDisplayNone';
	document.getElementById(id).innerHTML = '';
}

function showInfo(id, msg) {
	document.getElementById(id).innerHTML = msg;
	document.getElementById(id).className = 'clsInformation';
}

function clearForm(f) {
	for(i = 0; i < f.elements.length; i++) {
		if(f.elements[i].type == 'text' || f.elements[i].type == 'textarea' || f.elements[i].type == 'password' || f.elements[i].type == 'file') {
			f.elements[i].value = '';
		}
		else if(f.elements[i].type == 'radio' || f.elements[i].type == 'checkbox') {
			f.elements[i].checked = false;
		}
	}
}

function increaseOpacity(id)
{
	var msgbox = document.getElementById(id);
	msgbox.className = 'clsBringFront';
	var ie = (document.all)? true:false;
	if(ie)
	{
		var temp = msgbox.style.filter.split('=');
		temp = temp[1].split(')');
		var opa = parseInt(temp[0]);
		opa += 5;
		msgbox.style.filter = 'alpha(opacity=' + opa + ')';
		if(opa == 100)
		{
			clearInterval(timer);
			timer = null;
		}	
	}
	else
	{
		var opa = parseFloat(msgbox.style.opacity);
		opa += 0.1;
		msgbox.style.opacity = opa;
		if(opa == 1)
		{
			clearInterval(timer);
			timer = null;
		}
	}
}

function decreaseOpacity(id) {
	var msgbox = document.getElementById(id);
	var msgContent = document.getElementById('idMessageContent');
	var ie = (document.all)? true:false;
	if(ie) {
		var temp = msgbox.style.filter.split('=');
		temp = temp[1].split(')');
		var opa = parseInt(temp[0]);
		opa -= 5;
		msgbox.style.filter = 'alpha(opacity=' + opa + ')';
		if(opa == 0) {
			clearInterval(timer);
			timer = null;
			msgbox.className = 'clsSentBack';
			msgContent.innerHTML = '';
			alertAction();
		}	
	}
	else {
		var opa = parseFloat(msgbox.style.opacity);
		opa -= 0.1;
		msgbox.style.opacity = opa;
		if(opa == 0) {
			clearInterval(timer);
			timer = null;
			msgbox.className = 'clsSentBack';
			msgContent.innerHTML = '';
			alertAction();
		}
	}
}

function decreaseOpacityAndRemove(id) {
	var element = document.getElementById(id);
	var ie = (document.all)? true:false;
	if(ie) {
		var temp = element.style.filter.split('=');
		temp = temp[1].split(')');
		var opa = parseInt(temp[0]);
		opa -= 5;
		element.style.filter = 'alpha(opacity=' + opa + ')';
		if(opa == 0) {
			clearInterval(removeElementTimer);
			removeElementTimer = null;
			element.parentNode.removeChild(element);
		}	
	}
	else {
		var opa = parseFloat(element.style.opacity);
		opa -= 0.1;
		element.style.opacity = opa;
		if(opa == 0) {
			clearInterval(removeElementTimer);
			removeElementTimer = null;
			element.parentNode.removeChild(element);
		}
	}
}

function removeElement(id) {
	if(ie) {
		document.getElementById(id).style.filter = 'alpha(opacity=100)';
	}
	else {
		document.getElementById(id).style.opacity = '1';
	}
	removeElementTimer = setInterval('decreaseOpacityAndRemove(\''+ id +'\')', 15);
}

function checkDate(d, m, y) {
	return m > 0 && m < 13 && y > 0 && y < 32768 && d > 0 && d <= (new Date(y, m, 0)).getDate()
}

function emailValidation(emailid) {
	if(!/^([a-zA-Z0-9._\-]+)@([a-zA-Z0-9]+).([a-zA-Z0-9]{2,4})$/.test(emailid)) {
		return false;
	}
	else {
		return true;
	}
}

function emailValidationWithAlert(emailid) {
	if(!/^([a-zA-Z0-9._\-]+)@([a-zA-Z0-9]+).([a-zA-Z0-9]{2,4})$/.test(emailid)) {
		alert("Please check your Email ID");
		return false;
	}
	else {
		return true;
	}
}

function imgValidate(file, id)
{
	var img = file.value;
	var extension = img.substr(img.lastIndexOf('.') + 1).toLowerCase();
	if(extension != 'jpg' && extension != 'gif' && extension != 'png' && extension != 'bmp' && extension != 'jpeg')
	{
		file.value = '';
		file.src = '';
		file.focus();
		if(id != "") {
			document.getElementById(id).innerHTML = '<span class="clsError">Invalid image file</span>';
		}
		return false;
	}
	return true;
}
