function changeVerifyImage(imagename) {
    document.getElementById(imagename).src = ini_rootweb + '/index.php?Page=ImageVerify&'+Math.random();
}
function windowOpen(url, w, h) {
    t = (screen.height - h)/2;
    l = (screen.width - w)/2;
    window.open(url,'pwin','width='+w+',height='+h+',left='+l+',top='+t);
}

function showdiv(divname) {
    if(document.getElementById(divname))
        document.getElementById(divname).style.display = 'block';
}
function hidediv(divname) {
    if(document.getElementById(divname))
        document.getElementById(divname).style.display = 'none';
}
function hidetabs() {
    var i=0;
    for(i=1;;i++) {
        if(document.getElementById('tab'+i)) {
            hidediv('tab'+i);
            if(document.getElementById('tabbutton'+i)) {
                document.getElementById('tabbutton'+i).style.backgroundColor='#f3f3f3';
                document.getElementById('tabbutton'+i).style.color='#cccccc';
            }
        } else {
            break;
        }
    }
}
function showtab(tabno) {
    showdiv('tab'+tabno);
    if(document.getElementById('tabbutton'+tabno)) {
        document.getElementById('tabbutton'+tabno).style.backgroundColor='#ffffff';
        document.getElementById('tabbutton'+tabno).style.color='#333333';
    }
}
var onloadjobs = new Array();
function addonloadjob(job) {
    onloadjobs[onloadjobs.length] = job;
}
function onLoadJobs() {
    for(var i=0;i<onloadjobs.length;i++) {
        eval(onloadjobs[i]);
    }
}
/* ############################ FORMS ########################################### */
function validateform(theform) {
    var numElements = theform.elements.length;
    var checkedcount;
    var element;
    var i,j;
    for (i = 0; i<numElements; i++) {
        checkedcount = -1;
        element = theform.elements[i];
        if(element.name.substr(0,8) == 'NOTNULL_') {
            if(element.tagName.toLowerCase() == 'select' && element.options.selectedIndex == 0) {
                alert('Required Field: ' + element.id);
                element.focus();
                return false;
            } else if(element.tagName.toLowerCase() == 'input' && (element.type.toLowerCase() == 'text' || element.type.toLowerCase() == 'password') && element.value == '') {
                alert('Required Field: ' + element.id);
                element.focus();
                return false;
            }/* else if(element.tagName.toLowerCase() == 'input' && element.type.toLowerCase() == 'radio') {
                for(j = 0; j < element.length; j++) {
                    if(element[j].checked) {
                        checkedcount = j;
                    }
                }
                if(checkedcount == -1) {
                    alert('Required Field: ' + element.id);
                    element.focus();
                    return false;
                }
            }*/
        } else if(element.name.substr(0,4) == 'INT_' && element.tagName.toLowerCase() == 'input' && element.type.toLowerCase() == 'text' && element.value.length > 0 && isNaN(element.value)) {
            alert('Field should be integer: ' + element.id);
            element.focus();
            return false;
        }
    }
    return true;
}
/* ################################# POPUP NOTES ################################# */
if (document.layers) { /* Netscape */
    document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = captureMousePosition;
} else if (document.all) { /* Internet Explorer */
    document.onmousemove = captureMousePosition;
} else if (document.getElementById) { /* Netcsape 6 */
    document.onmousemove = captureMousePosition;
}
/* Global variables */
xMousePos = 0;
yMousePos = 0;
xMousePosMax = 0;
yMousePosMax = 0;
var popupnotes = new Array();
function captureMousePosition(e) {
    if (document.layers) {
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    } else if (document.all) {
        xMousePos = window.event.x+document.body.scrollLeft;
        yMousePos = window.event.y+document.body.scrollTop;
        xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
        yMousePosMax = document.body.clientHeight+document.body.scrollTop;
    } else if (document.getElementById) {
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    }
}

function showpopup(ContentID, trigger) {
    $('NOTES').clonePosition(trigger, {setWidth: false, setHeight: false, offsetTop: 10, offsetLeft: 10});
    $('NOTES').update('<table width=300><tr><td class=popuptext>'+popupnotes[ContentID]+'</td></tr></table>');
    $('NOTES').show();
    
}
function hidepopup(popupname) {
    $('NOTES').hide();
}
/* ############################################################################ */
function moduleRefresh(id, params) {
    $('div_module_' + id).update('<center><br /><br /><br /><img src="' + ini_imageweb + '/ajaxloader.gif" /><br /><br /><br /></center>');
    var url = ini_rootweb + '/index.php?Page=ModuleOnly&ModuleID=' + id + '&' + params;
    new Ajax.Request(url, {
        onSuccess: function(res) {
            $('div_module_' + id).update(res.responseText);
        }
    });
}

function embedFile(filepath, params)
{
	if (typeof embedfile_params != 'undefined') {
		params.auto = embedfile_params.auto;
	}
    flashembed("container", {
        src: ini_rootweb + '/js/FlowPlayerDark.swf',
        width: 400, 
        height: params.height
    },
    {
        config: {
        loop:false, 
        autoPlay:params.auto,
        controlBarBackgroundColor:'0x2e8860',
        initialScale: 'orig',
        baseURL: ini_rootweb,
        videoFile: filepath
        }
    });
}

function reloadItem(type, page, id, idlist)
{
    $('item_' + type).update('<br /><br /><br /><br /><br /><img src="' + ini_imageweb + '/ajaxloader.gif" /><br /><br />');
    idlist = idlist.split(',');
    var len = idlist.length;
    for (i = 0; i < len; i++) {
        $('link_' + type + '_' + idlist[i]).removeClassName('selected');
    }
    $('link_' + type + '_' + id).addClassName('selected');
    new Ajax.Request(ini_ws + 'm_quranreading?page=' + page + '&action=getItemContent', {
        parameters: {
            type: type,
            id: id
        },
        onSuccess: function(res) {
            res = res.responseText;
            res = res.evalJSON();
            $('item_' + type).update(res.content);
            if (type =='audio') {
                embedFile(res.url, {height: 100, auto: true});
            }
        }
    });
}

itemlist_parameters = new Object;

function itemListPage(page)
{
    itemlist_parameters['itemlist_form_pg'] = page;
    itemListPost();
}

function itemListGroup(group)
{
    itemlist_parameters['itemlist_form_group_id'] = group;
    itemListPost();
}

function itemListRemoveGroup()
{
    itemlist_parameters['itemlist_form_group_id'] = '';
    itemListPost();
}

function itemListPost()
{
    itemlist_parameters['ViewType'] = 'WithoutContainer';

    $('page_content_container').update('<br/><br/><br/><center><img src="' + ini_imageweb + '/ajaxloaderpoint.gif"></center>');
    
    new Ajax.Request(self.location.href, {
        parameters: itemlist_parameters,
        onSuccess: function(res) {
            $('page_content_container').update(res.responseText);
        }
    });
}

function html_entity_decode(str)
{
	var ta=document.createElement("textarea");
	ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
	return ta.value;
}

function getURLVars()
{
	var str = document.location.href;
	str = str.split('#');
	var vars = new Object();
	var variable;
	if (str[1]) {
		matches = str[1].split('&');
		for (var i =0; i < matches.length; i++) {
			variable = matches[i].split('=');
			if (!variable[0] || !variable[1]) {
				continue;
			}
			vars[variable[0]] = variable[1];
		}
	}
	return vars;
}

function getPlainURL()
{
	var str = document.location.href;
	str = str.split('#');
	if (str[1]) {
		return str[0];
	}
	return document.location.href;
}

var last_url = document.location.href;

function checkAnchor()
{
	if (typeof initPage != 'function') {
		return ;
	}
	if (document.location.href != last_url) {
		last_url = document.location.href;
		initPage();
	}
	setTimeout("checkAnchor()", 300);
}
setTimeout("checkAnchor()", 300);

function login()
{
	if (!$F('username') || !$F('password')) {
		return ;
	}
	$('loginbutton').hide();
	$('loginloading').show();
	
	new Ajax.Request(ini_ws, {
		parameters: {
			username: $F('username'),
			password: $F('password'),
			rememberme: $F('rememberme')
		},
		onSuccess: function(res) {
			res = res.responseText.evalJSON();
			if (res.error) {
				alert(res.error);
			}
			if (res.html) {
				$('loginpartial').update(res.html);
			} else {
				$('loginbutton').show();
				$('loginloading').hide();
			}
		}
	});
}

function logout()
{
	new Ajax.Request(ini_ws, {
		parameters: {
			action: 'logout'
		},
		onSuccess: function(res) {
			res = res.responseText;
			if (res) {
				document.location.href = ini_p;
			}
		}
	});
}

var searchBar = function() {
	$('searchform_keyword').value = $F('searchbar_keyword');
	$('searchform').submit();
}