/* Ajax post request */
function scriptDoLoadPost(scriptUrl, scriptForm, scriptPos, scriptArgs, noLoading) {
	if(!scriptArgs){ var scriptArgs = ''; }
	scriptArgs = $(scriptForm).serialize() + scriptArgs;
	myAjax = new Ajax.Updater(scriptPos, scriptUrl, {
		method : 'post',
		parameters : scriptArgs,
		evalScripts : true,
		onLoading: function(request){ showLoadingIcon(scriptPos, noLoading); },
		insertion : Insertion.Append
	});
}

/* Ajax get request */
function scriptDoLoad(scriptUrl, scriptPos, scriptArgs, noLoading) {
	myAjax = new Ajax.Updater(scriptPos, scriptUrl, {
		method : 'get',
		parameters : scriptArgs,
		evalScripts : true,
		onLoading : function(request){ showLoadingIcon(scriptPos, noLoading); },
		insertion : Insertion.Append
	});
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/* site map request */
var periodic;
function sitemapDoLoadPost(scriptUrl, scriptForm, scriptPos, scriptArgs, noLoading) {
	
	hideDiv('proceed');
	showSitemapLog(1);
	if(!scriptArgs){ var scriptArgs = ''; }
	scriptArgs = $(scriptForm).serialize() + scriptArgs;
	myAjax = new Ajax.Updater(scriptPos, scriptUrl, {
		method : 'post',
		parameters : scriptArgs,
		evalScripts : true,
		onLoading: function(request){ showLoadingIcon(scriptPos, noLoading); },
		onComplete : function(request){ periodic.stop();},
	});	
}

function showSitemapLog(start){
	if(start){
		document.getElementById('url_count').innerHTML = '0';
		document.getElementById('curr_url').innerHTML = '';
	}else{
		document.getElementById('url_count').innerHTML = readCookie('url_count');
		document.getElementById('curr_url').innerHTML = unescape(readCookie('curr_url'));
	}	
	periodic = setTimeout('showSitemapLog()', 2000);	
}

/* Onloading image icon function */
function showLoadingIcon(scriptPos,noLoading){
	loading = 0;
	switch (scriptPos){		
		
		case "content":
			content = '<div id="loading_content"></div>';
			loading = 1;
			break;
		
		case "subcontent":
			content = '<div id="loading_subcontent"></div>';
			loading = 1;
			break;
		
		case "ContentFrame":
			content = '<div id="loading_content_frame"></div>';
			loading = 1;
			break;
		
		default :
			content = '<div id="loading_rankarea"></div>';
			loading = 1;
			break;
	}
	
	if((loading == 1) && (noLoading != 1)){
		document.getElementById(scriptPos).innerHTML = content;
	}
}

function confirmLoad(scriptUrl, scriptPos, scriptArgs) {

	var agree = confirm("Do you really want to proceed? ");
	if (agree)
		return scriptDoLoad(scriptUrl, scriptPos, scriptArgs);
	else
		return false;
}

function confirmSubmit(scriptUrl, scriptForm, scriptPos) {

	var agree = confirm("Do you really want to proceed? ");
	if (agree)
		return scriptDoLoadPost(scriptUrl, scriptForm, scriptPos);
	else
		return false;
}

function doAction(scriptUrl, scriptPos, scriptArgs, actionDiv) {
	actVal = document.getElementById(actionDiv).value;
	scriptArgs += "&sec=" + actVal;
	switch (actVal) {
		case "select":		
			break;
		
		case "checkstatus":
		case "edit":
		case "reports":
			scriptDoLoad(scriptUrl, scriptPos, scriptArgs);
			break;
	
		default:
			confirmLoad(scriptUrl, scriptPos, scriptArgs);
			break;
	}
}

function doLoad(argVal, scriptUrl, scriptPos, scriptArgs) {
	actVal = document.getElementById(argVal).value;
	scriptArgs += "&"+ argVal +"=" + actVal;
	scriptDoLoad(scriptUrl, scriptPos, scriptArgs);
}

/* func to show hide menu */
function showMenu(button, scriptPos){
    var but = document.getElementById(button).src;
    var s = but.match("more");
    var d = "more";

    if(s==d){ 
        but = but.replace(/more/,"hide");
        document.getElementById(scriptPos).style.display = '';
        document.getElementById(button).src = but;
    }else{
        but = but.replace(/hide/,"more");
        document.getElementById(scriptPos).style.display = 'none';
        document.getElementById(button).src = but;
    }
}

function updateArea(scriptPos, content) {
	document.getElementById(scriptPos).innerHTML += content;
}

function chkObject(theVal) {
    if (document.getElementById(theVal) != null) {
        return true;
    } else {
        return false;
    }
}

function checkSubmitInfo(scriptUrl, scriptForm, scriptPos, catCol) {

	if(chkObject('captcha')){
		value = document.getElementById('captcha').value;
		if (value==null||value==""){
			alert('Please enter the code shown');
			return false;
		}
	}
	
	var obj = document.getElementsByName(catCol).item(0);
	value = obj.value;
	if (value==null||value==""||value==0){
		alert('Please select a category');
		return false;
	}
	
	scriptDoLoadPost(scriptUrl, scriptForm, scriptPos);
}

function loadJsCssFile(filename, filetype){
	if (filetype=="js"){ //if filename is a external JavaScript file
		var fileref=document.createElement('script')
		fileref.setAttribute("type","text/javascript")
		fileref.setAttribute("src", filename)
	}else if (filetype=="css"){ //if filename is an external CSS file
		var fileref=document.createElement("link")
		fileref.setAttribute("rel", "stylesheet")
		fileref.setAttribute("type", "text/css")
		fileref.setAttribute("href", filename)
	}
	if (typeof fileref!="undefined")
		document.getElementsByTagName("head")[0].appendChild(fileref)
}

function hideDiv(scriptPos){
	document.getElementById(scriptPos).style.display = 'none';
}
