﻿// JavaScript Document
var loadingimg = 'images/loading.gif';
var loadingstrip = 'images/loadingstrip.gif';
var currtab = 1;
var currtype = 'dummy';

//// UNIVERAL CALL LOADING MODULES
var element = 'mainblk';

function openWin( windowURL, windowName, windowFeatures ) { 
		return window.open( windowURL, windowName, windowFeatures ) ; 
	}
	
function CheckAll (theForm)
{
    var bFirst=true;
    var bValue=true;
    for (var i = 0; i < theForm.selaudios.length; i++) {
        if (bFirst) {
            if (theForm.selaudios[i].checked)
                bValue = false;
            else
                bValue = true;
            bFirst = false;
        }
        theForm.selaudios[i].checked = bValue;
    }
}
	
function chksrchval(arg){
		if(arg.q.value == ''){
			alert('أدخل كلمات البحث');
			arg.q.focus();
			return false;
		}
		return true;
	}
	
//IE likes to cache so we will fix it's wagon!
function _noCache(url){
	var qs = new Array();
	var arr = url.split('?');
	var scr = arr[0];
	if(arr[1]){ qs = arr[1].split('&'); }
	//qs[qs.length]='noCache='+new Date().getTime()+'&simplelayout=1';
	qs[qs.length]='simplelayout=1';
return scr+'?'+qs.join('&');
}

function showerror(obj){
	alert('Sorry, there was an error');
	$('mainblk').innerHTML = obj.responseText;
}

function editFailed(t, obj){
	obj.innerHTML	= t.responseText;
}

var publishdata = function(obj){
	$(element).hide();
  $(element).innerHTML = obj.responseText;
	new Effect.BlindDown(element, {duration: 0.5, delay:0.1});
	new Effect.ScrollTo(element,{offset: -100}); 
}

// to add an option to any specific HTML select tag element
var selectAddOption = function (selObj , txt, val)
	{
	var opt = new Option(txt,val);
	selObj.options[selObj.options.length] = opt;
	};
///////// FUNCTIONS USED BY ALL /////////
function sysmsgdisplay(clsname){
	//display the status to the user
	$('sysmsg').addClassName(clsname);
	$('sysmsg').hide();
	$('sysmsg').innerHTML = repMsg;
	Effect.Appear('sysmsg', {delay:1, duration:3});
	Effect.Fade('sysmsg', {delay:5, duration:3}); return false;
}

function setClassName(id, cls, newcls){
	$(id).removeClassName(cls);
	$(id).addClassName(newcls);
}
///////// FUNCTIONS USED BY ALL /////////
var mlRegister = function (frmid)
	{
	$('mlLoadingImage').show();

	var success	= function(t){mlSuccess(t,frmid);}
	var failure	= function(t){alert('Error ...');}
	var pars = Form.serialize(frmid);
	$(frmid).disable();
	
	var url = 'http://gate.gph.gov.sa/index.cfm?do=cms.mlRegister';
	var myAjax = new Ajax.Request(_noCache(url), {parameters:pars, onSuccess:success, onFailure:failure});	
	}

var mlSuccess = function (t,frmid)
	{
		$(frmid).enable();
		$('mlLoadingImage').hide();
		t.responseText.evalScripts();
		$('mlFormMessage').update(msg);
		new Effect.BlindDown('mlFormMessage',{duration:0.2});
		new Effect.Highlight('mlFormMessage',{delay:0.3, duration:2,afterFinish: function(){ new Effect.BlindUp('mlFormMessage',{duration:0.2,delay:8}) } });
		
		if(taskDone)
			{
			$(frmid).reset();
			}
		else
			{
			}
	}
	
var verifyempInfo = function(frm, frmid, url){
	var pars = $(frmid).serialize();
	var success	= function(t){showempInfo(t);}
	var failure	= function(t){editFailed(t);}
	var myAjax = new Ajax.Request(_noCache(url), {method:'post', parameters:pars, onSuccess:success, onFailure:failure});	
}

var showempInfo = function(t)
	{t.responseText.evalScripts();
		if(emptask == true){
			$('empmsg').innerHTML = '';
			$('empinfologin').hide();
			$('empinfo').innerHTML = t.responseText;
			$('empinfo').show() ;
			new Effect.Highlight('empinfo');
		}
		else{
			$('empmsg').innerHTML = taskempmsg;
		}
	}	
var showcontent = function (url) {
	$('loadingimage').show();

	var success	= function(t){taskSuccess(t);}
	var failure	= function(t){alert('Error ...');}
	
	var myAjax = new Ajax.Request(_noCache(url,2), { onSuccess:success, onFailure:failure});	
	
}

var taskSuccess = function(t){
	$('loadingimage').hide();
	$('mainblk').innerHTML = t.responseText;
	new Effect.ScrollTo('mainblk');
	
}

var showtheimage = function (url) {
	$('loadingimage').show();

	var success	= function(t){imgSuccess(t);}
	var failure	= function(t){alert('Error ...');}
	
	var myAjax = new Ajax.Request(_noCache(url), { onSuccess:success, onFailure:failure});	
	
}

var imgSuccess = function(t){
	
	$('loadingimage').hide();
	$('glShowImage').innerHTML = t.responseText;
	t.responseText.evalScripts();
	new Effect.ScrollTo('glShowImage');
	
}
var validateme = function(arg,myactionval){
	for(i=0; i<arg.selaudios.length; i++){
		if(arg.selaudios[i].checked){
			arg.myaction.value = myactionval;
			if(myactionval == 'bookmark'){
				arg.origin.value = 'multiselect';
			}
			arg.submit();
			return true;
		}
	}
	alert('اختر مادة صوتية على الأقل');
	return false;
}

var toggledisplay = function(audtype){
	if(currtype==audtype)return;
	new Effect.BlindUp(currtype,{duration:1.5, delay:0.3});
	currtype = audtype;
	new Effect.BlindDown(currtype,{duration:1.5, delay:0.3});
}

var audioaction = function(mfrmid){
	var pars = Form.serialize(mfrmid);
	
	var success	= function(t){multiselectresult(t);}
	var failure	= function(t){editFailed(t);}
	
	var url = 'http://gate.gph.gov.sa/index.cfm?do=cms.AudioAction';
	var myAjax = new Ajax.Request(_noCache(url), {method:'get', parameters:pars, onSuccess:success, onFailure:failure});	
}

var multiselectresult = function(t){
	t.responseText.evalScripts(); 
	if(actiontype == 'noselectionmade'){
		alert('فضلا أختر مادة صوتية');
	}
	else if(actiontype == 'listen'){
		playaudio(audiolist);
	}
	else if(actiontype == 'send2frnd'){
		sendtofriend(audiolist,'audio');
	}
	else if(actiontype == 'bookmark'){
		bookmarkall(audiolist);
	}	
	else if(actiontype == 'singleitembookmark' || actiontype == 'submitbookmarkall'){
		addtothistag(audioid,tagid,mode,tagname,type);
	}		
}


var bookmarkall = function(audid){
	Position.clone( 'bmpos', 'bookmarkall',{setLeft:true, setTop:true, setWidth:false, setHeight:false});
	$('bookmarkall').show();
	$('bookmarkall').innerHTML = '<img src='+loadingimg+' border="0" />';
	
	var success	= function(t){loadusrtags_forbookmarkall(t,audid);}
	var failure	= function(t){editFailed(t);}
	var url = 'index.cfm?do=cms.getusrtags&id='+audid+'&type=audio&origin=multiselect';
	var myAjax = new Ajax.Request(_noCache(url), {method:'post', onSuccess:success, onFailure:failure});	
}

var loadusrtags_forbookmarkall = function(t,audid){
	t.responseText.evalScripts();
	if(task == true){
		$('bookmarkall').innerHTML = t.responseText;
		Position.clone( 'bmpos', 'bookmarkall',{setLeft:true, setTop:true, setWidth:false, setHeight:false});
		
		new Effect.Appear('bookmarkall', {duration:0.5, delay:0.3});
	}
	else{
		$('bookmarkall').hide();
		logintoproceed(audid,'bookmarkall');
	}
}

var playaudio = function(aid){
	window.open('http://gate.gph.gov.sa/index.cfm?do=cms.AudioListen&simplelayout=1&audioid='+aid, 'haramainaudio','width=602px,height=374px,resizeable=no,status=no,left=200,top=200,screenX=200,screenY=200');
}

var playvideo = function(aid){
	window.open('http://gate.gph.gov.sa/index.cfm?do=cms.playflv&simplelayout=1&audioid='+aid, 'haramainvideo','width=600px,height=300px,resizeable=no,status=no,left=300,top=300,screenX=300,screenY=300');	
}
var sendaudioerror = function(audid){
	Modalbox.show('http://gate.gph.gov.sa/index.cfm?do=cms.SendAudioError&id='+audid+'&simplelayout=1', {title:'إرسال ملاحظة', width: 602, overlayClose: false });
}

var submitaudioerror = function(frmid){
	$('processbuttons').hide();
	$('processindicator').innerHTML	= '<img src='+loadingimg+' border="0" />';

	var success	= function(t){sendMailSuccess(t);}
	var failure	= function(t){editFailed(t);}

	var pars = $(frmid).serialize();
	var url = 'http://gate.gph.gov.sa/index.cfm?do=cms.ActSendAudioError';
	var myAjax = new Ajax.Request(_noCache(url), {method:'get', parameters:pars, onSuccess:success, onFailure:failure});	
}

var sendtofriend = function(aid,type){
	type=(typeof type == 'undefined')?'audio':type;
	Modalbox.show('index.cfm?do=cms.SendtoFriend&id='+aid+'&type='+type+'&simplelayout=1', {title:'أرسل لصديق', width: 602, overlayClose: false });
}

var send2friend = function(frmid){
	$('processbuttons').hide();
	$('processindicator').innerHTML	= '<img src='+loadingimg+' border="0" />';

	var success	= function(t){sendMailSuccess(t);}
	var failure	= function(t){editFailed(t);}

	var pars = $(frmid).serialize();
	var url = 'http://gate.gph.gov.sa/index.cfm?do=cms.ActSendtoFriend';
	var myAjax = new Ajax.Request(_noCache(url), {method:'get', parameters:pars, onSuccess:success, onFailure:failure});	
}

var sendMailSuccess = function(t){
	t.responseText.evalScripts();
	if(success == true){ 
		$('sndfrm').innerHTML = mlmsg;
		new Effect.Highlight('sndfrm', {delay: 0.2, duration: 0.4, afterFinish:function(){setTimeout('Modalbox.hide()',1000);}});	
	}
	else{
		$('snd2frnderrmsg').innerHTML = mlmsg;
		new Effect.BlindDown('snd2frnderrmsg', {delay: 0, duration: 0.2});
		$('processindicator').innerHTML	= '';
		$('processbuttons').show();
	}
}

var showCommentBlk = function(aid,uid){
	if(uid !=0){
		new Effect.Appear('addcommentblk',{duration:0.2,delay:0.1, afterFinish: function(){ new Effect.ScrollTo('addcommentblk',{offset: -100}) } });
	}
	else{
		logintoproceed(aid,'comments'); // 0 is fake audioid
	}
}

var addcomment = function(frmid){
	$('errmsg').hide();	// if shown
	$('processbuttons').hide();
	$('processindicator').innerHTML	= '<img src='+loadingimg+' border="0" />';

	var success	= function(t){commentSuccess(t);}
	var failure	= function(t){editFailed(t);}

	var pars = $(frmid).serialize();
	var url = 'index.cfm?do=cms.actComment';
	var myAjax = new Ajax.Request(_noCache(url), {method:'post', parameters:pars, onSuccess:success, onFailure:failure});	
}

var commentSuccess = function(t){
	t.responseText.evalScripts();
	if(success == true){ 
		
		Form.reset('commfrm');
		$('processbuttons').show();
		$('processindicator').update('');
		$('msg').innerHTML = msg;
		
		new Effect.Appear('msg', {delay: 0.1, duration: 0.1});	
		new Effect.Highlight('msg', 
												 	{
														delay: 0.5, 
														duration: 2, 
														afterFinish:function(){new Effect.Fade('msg',{delay: 5, duration:0.2}) } 
													});	
	}
	else{
		$('commerrmsg').innerHTML = msg;
		new Effect.BlindDown('errmsg', {delay: 0.1, duration: 0.1});
		$('processindicator').innerHTML	= '';	
		$('processbuttons').show();
	}
	
}

var actdownloadaudio = function(aid,type){
	type=(typeof type == 'undefined')?'rm':type;
	$('download_'+aid).hide();
 	window.location.href = 'index.cfm?do=cms.ActAudioDownload&AudioID='+aid+'&type='+type;
}

var lastopendwnldblk = 0;
var lastopendocdwnldblk = 0;
var downloadaudio = function(id, leftOffset, topOffset){
	Position.clone( 'dld_'+id, 'download_'+id,{setLeft:true, setTop:true, setWidth:false, setHeight:false, offsetLeft:leftOffset, offsetTop:topOffset});
	
	if(lastopendwnldblk != 0 && $('download_'+lastopendwnldblk)){
		$('download_'+lastopendwnldblk).hide();
		$('docdownload_'+lastopendocdwnldblk).hide();
	}
	lastopendwnldblk = id;
	$('download_'+id).show();
}


var downloaddoc = function(id, leftOffset, topOffset){
	Position.clone( 'docdld_'+id, 'docdownload_'+id,{setLeft:true, setTop:true, setWidth:false, setHeight:false, offsetLeft:leftOffset, offsetTop:topOffset});
	
	if(lastopendocdwnldblk != 0 && $('docdownload_'+lastopendocdwnldblk)){
		$('docdownload_'+lastopendocdwnldblk).hide();
		$('download_'+lastopendwnldblk).hide();
	}
	lastopendocdwnldblk = id;
	$('docdownload_'+id).show();
}

var toggleSlide = function(id){ 
	if(!$(id).visible())									
		new Effect.SlideDown(id, {duration: 0.3});
	else
		new Effect.SlideUp(id, {duration: 0.2});
}

var BlindDown = function(frmid, id1,id2) {
	$(frmid).reset();
	new Effect.BlindUp(id2, {duration: 0.5, afterFinish:function(){ new Effect.BlindDown(id1, {duration: 0.5, delay:0.1}) } });
}

var BlindUp = function(frmid, id1,id2) {
	$(frmid).reset();
	new Effect.BlindUp(id1, {duration: 0.5, afterFinish:function(){ new Effect.BlindDown(id2, {duration: 0.5, delay:0.1}) } });
}

var loginuser = function(lfrmid){
	$('processbuttons').hide();
	$('processindicator').innerHTML	= '<img src='+loadingimg+' border="0" />';

	var success	= function(t){loginStatus(t);}
	var failure	= function(t){editFailed(t);}

	var pars = $(lfrmid).serialize(); 
	var url = 'index.cfm?do=cms.loginuser';
	var myAjax = new Ajax.Request(_noCache(url), {method:'post', parameters:pars, onSuccess:success, onFailure:failure});	
}

var loginStatus = function(t){
	t.responseText.evalScripts();
  if(task == true){
		$('usrlogin').innerHTML = t.responseText;
	}
	else{
		$('msg').innerHTML = loginwelmsg;
		$('processindicator').innerHTML	= '';
		$('processbuttons').show();
	}
}

var registeruser = function(rfrmid,msgplh){
	$('registerprocessbuttons').hide();
	$('registerprocessindicator').innerHTML	= '<img src='+loadingimg+' border="0" />';

	var success	= function(t){registerStatus(t,msgplh);}
	var failure	= function(t){editFailed(t);}

	var pars = $(rfrmid).serialize();
	var url = 'index.cfm?do=cms.registeruser';
	var myAjax = new Ajax.Request(_noCache(url), {method:'post', parameters:pars, onSuccess:success, onFailure:failure});		
}

var registerStatus = function(t,msgplh){
	t.responseText.evalScripts();
  if(task == true){
		$(msgplh).innerHTML = t.responseText ;
 	}
	else{
		$('regmsg').innerHTML = regstatmsg;
		$('registerprocessindicator').innerHTML	= '';
		$('registerprocessbuttons').show();
	}
}

var forgotpassword = function(){
	Modalbox.show('index.cfm?do=cms.forgotpassword&simplelayout=1', {title:'نسيت كلمة السر', width: 602, height:224, overlayClose: false });
}

var mailpwd = function(pwdfrmid){
	$('processbuttons').hide();
	$('processindicator').innerHTML	= '<img src='+loadingimg+' border="0" />';

	var success	= function(t){mailpwdStatus(t);}
	var failure	= function(t){editFailed(t);}

	var pars = $(pwdfrmid).serialize();
	var url = 'index.cfm?do=cms.actForgotpassword';
	var myAjax = new Ajax.Request(_noCache(url), {method:'post', parameters:pars, onSuccess:success, onFailure:failure});	
}

var mailpwdStatus = function(t){
	t.responseText.evalScripts();	
	$('pwdfrm').innerHTML = msg;
}

var addtofavourite = function(id,type){
	type=(typeof type == 'undefined')?'audio':type;
	
	Position.clone( 'posid_'+id, 'bookmark_'+id,{setLeft:true, setTop:true, setWidth:false, setHeight:false});
	$('bookmark_'+id).show();
	$('bookmark_'+id).innerHTML = '<img src='+loadingimg+' border="0" />';
	
	var success	= function(t){loadusrtags(t,id,type);}
	var failure	= function(t){editFailed(t);}
	var url = 'index.cfm?do=cms.getusrtags&id='+id+'&type='+type;
	var myAjax = new Ajax.Request(_noCache(url), {method:'post', onSuccess:success, onFailure:failure});	
}

var addtothistag = function(id,tagid,mode,tagname,type){ 
	type=(typeof type == 'undefined')?'audio':type;

	$('clickedtagindicator_'+tagid).innerHTML = '<img src='+loadingimg+' border="0" />';

	var success	= function(t){addedtotag(t,id,mode);}
	var failure	= function(t){editFailed(t);}
	
	if(tagid != 0){
		var url = 'index.cfm?do=cms.addtotag&id='+id+'&tagid='+tagid+'&origin='+mode+'&tagtype='+type;
		var myAjax = new Ajax.Request(_noCache(url), {onSuccess:success, onFailure:failure});	
	}
	else{
		$('clickedtagbutton_'+tagid).hide();
		var url = 'index.cfm?do=cms.addtotag';
		var myAjax = new Ajax.Request(_noCache(url), {method:'post', parameters:'id='+id+'&tagid='+tagid+'&origin='+mode+'&newtag='+tagname+'&tagtype='+type, onSuccess:success, onFailure:failure});		
	}
}

var addedtotag = function(t,id,mode){	
	t.responseText.evalScripts();
	if(task == true){
		if(mode=='singleselect')
			new Effect.Fade('bookmark_'+id, {duration:0.5, delay:0.3});	
		else if(mode == 'multiselect')
			new Effect.Fade('bookmarkall', {duration:0.5, delay:0.3});	
	}
	else {
		$('tagerrmsg').innerHTML = msg;
		$('clickedtagbutton_0').show();
		$('clickedtagindicator_0').innerHTML = '';		
	}
}

var loadusrtags = function(t,id,type){
	t.responseText.evalScripts();
	if(task == true){
		$('bookmark_'+id).innerHTML = t.responseText;
		Position.clone( 'posid_'+id, 'bookmark_'+id,{setLeft:true, setTop:true, setWidth:false, setHeight:false});
		new Effect.Appear('bookmark_'+id, {duration:0.5, delay:0.3});
	}
	else{
		$('bookmark_'+id).hide();
		logintoproceed(id,'bookmark');
	}
}

var logintoproceed = function(aid, fortype){
	Modalbox.show('index.cfm?do=cms.logintoproceed&simplelayout=1&id='+aid+'&type='+fortype, {title:'سجل الدخول', width: 402, height:230, overlayClose: false });
}

var actlogintoproceed = function(frmid,id,type){
	$('processbuttons').hide();
	$('processindicator').innerHTML	= '<img src='+loadingimg+' border="0" />';

	var success	= function(t){loginproceedStatus(t,id, type);}
	var failure	= function(t){editFailed(t);}

	var pars = $(frmid).serialize();
	var url = 'index.cfm?do=cms.loginuser';
	var myAjax = new Ajax.Request(_noCache(url), {method:'post', parameters:pars, onSuccess:success, onFailure:failure});	
}

var loginproceedStatus = function(t,id,type){
	t.responseText.evalScripts();
  if(task == true){
		//hide ModalBox
		Modalbox.hide();
		//update the login box
		isUserLoggedIn();
		
		if(type == 'bookmark'){
			//show the "usertag" div element
			addtofavourite(id);
		}
		else if(type == 'comments'){
			//show the comments block
			new Effect.Appear('addcommentblk',{duration:0.2,delay:0.1, afterFinish: function(){ new Effect.ScrollTo('addcommentblk',{offset: -100}) } });
		}
		else if(type == 'bookmarkall'){
			//show the comments block
			bookmarkall(audid);
		}		
	}
	else{
		$('logintoproceedmsg').innerHTML = loginwelmsg;
		$('processindicator').innerHTML	= '';
		$('processbuttons').show();
	}
}

var isUserLoggedIn = function(){
	var success	= function(t){userloggedin(t);}
	var failure	= function(t){editFailed(t);}

	var url = 'index.cfm?do=cms.isuserloggedin';
	var myAjax = new Ajax.Request(_noCache(url), {method:'post', onSuccess:success, onFailure:failure});	
}

var userloggedin = function(t){
	$('usrlogin').innerHTML = t.responseText;
	new Effect.Highlight('usrlogin', {duration:2.1});	
}

var logout = function(frompage){
	$('logoutindicator').innerHTML = '<img src='+loadingimg+' border="0" />';
	var success	= function(t){logoutsuccess(t,frompage);}
	var failure	= function(t){editFailed(t);}
	
	var url = 'index.cfm?do=cms.logout';
	var myAjax = new Ajax.Request(_noCache(url), {method:'post', onSuccess:success, onFailure:failure});
}

var logoutsuccess = function(t, frompage){
	t.responseText.evalScripts();
	if(frompage == 'home'){
		$('usrlogin').innerHTML = t.responseText;
		new Effect.Highlight('logindetails', {duration:2.1});
	}
	else
	 window.location.href = 'index.cfm';
}

var manageTag = function(tagid){
	$('mt_'+tagid).innerHTML	= '<img src='+loadingimg+' border="0" />';
	
	var success	= function(t){showtagdetails(t,tagid);}
	var failure	= function(t){editFailed(t);}	
	var url = 'index.cfm?do=cms.managetag&tagid='+tagid;
	var myAjax = new Ajax.Request(_noCache(url), {method:'post', onSuccess:success, onFailure:failure});	
}

var showtagdetails = function(t,tagid){
	$('usrmainblk').innerHTML = t.responseText;
	t.responseText.evalScripts();
}

var setClassName = function(id, cls, newcls){
	$(id).removeClassName(cls);
	$(id).addClassName(newcls);
	//$(id+'_edit').toggle();
}

var removetag = function(tagid){
	var success	= function(t){clearTag(t, tagid);}
	var failure	= function(t){editFailed(t);}
 	var url = 'index.cfm?do=cms.removetag';
	var pars = 'tagid='+tagid;
	var myAjax = new Ajax.Request(_noCache(url), {method:'post', parameters:pars, onSuccess:success, onFailure:failure});

}
var clearTag = function(t, tagid){
	t.responseText.evalScripts();
	if(task==true){
		new Effect.Highlight('tag_'+tagid, {delay: 1, duration: 3, startColor:'#fff', endColor:'#f00'});
		Effect.Fade('tag_'+tagid, {delay:.1, duration:3, afterFinish: function(){
			$('tag_'+tagid).remove();
		}});
	}
}

var removetagitem = function(tagid,id,type){
	var success	= function(t){clearTagItem(t,tagid,id);}
	var failure	= function(t){editFailed(t);}
 	var url = 'index.cfm?do=cms.removetagitem&tagid='+tagid+'&id='+id+'&type='+type;
	var myAjax = new Ajax.Request(_noCache(url), {method:'post', onSuccess:success, onFailure:failure});

}
var clearTagItem = function(t,tagid,id){
	t.responseText.evalScripts();
	if(task==true){
		new Effect.Highlight('tagitem_'+tagid+'_'+id, {delay: 1, duration: 3, startColor:'#fff', endColor:'#f00'});
		Effect.Fade('tagitem_'+tagid+'_'+id, {delay:.1, duration:3, afterFinish: function(){
			$('tagitem_'+tagid+'_'+id).remove();
			$('tagitem_'+tagid+'_'+id+'_clear').remove();
		}});
	}
	//hide the tag if there is no item
	if(rcount == 0){
		Effect.Fade('tag_'+tagid, {delay:.1, duration:3, afterFinish: function(){
			$('tag_'+tagid).remove();
		}});		
	}
}

var gotouserpage = function(usrid){
	window.location.href = 'index.cfm?do=cms.memberpage&userid='+usrid;
}
var grpnum = 1;
var getmorecomments = function(id, usrid){
	$('morecomm').innerHTML = '<img src='+loadingimg+' border="0" />';
	grpnum = parseInt(grpnum) + 1;
	var success	= function(t){populatemorecomments(t,id);}
	var failure	= function(t){editFailed(t);}
 	var url = 'index.cfm?do=cms.usercomments&userid='+usrid+'&id='+id+'&groupnumber='+grpnum;
	var myAjax = new Ajax.Request(_noCache(url), {method:'post', onSuccess:success, onFailure:failure});	
}

var populatemorecomments = function(t,id){
	t.responseText.evalScripts();
	new Insertion.Before('myDiv', t.responseText);
	$('morecomm').innerHTML = '';
	new Effect.ScrollTo('myDiv',{offset:-450});
	// hide the more link if 'morerecsexist' is false
	if(morerecsexist == false){
		$('usrcomments').remove();
	}
}

////////////////////////////////////////// AUDIO EXPLORER /////////////////////////////////////////////////
var audioexplorer = function(){
	var url = 'index.cfm?do=cms.audioexplorer';
	var myAjax = new Ajax.Request(_noCache(url), {method:'GET', evalScripts:true, onSuccess:audioexplorerdiv, onFailure:showerror});
}

var audioexplorerdiv = function(t){
	$('mainblk').hide();
	$('mainblk').innerHTML = t.responseText;
	new Effect.BlindDown('mainblk', {duration: 0.5, delay:0.1, afterFinish:function(){ new Effect.ScrollTo('mainblk',{afterFinish:function(){  new Effect.Highlight('mainblk'); } }); } });
	t.responseText.evalScripts();
}

var browsebysurah = function(){
	$('loadingimg').show();	
	var success	= function(t){populateaudiocontainer(t,'quran','surah');}
	var url = 'index.cfm?do=cms.SurahList&audiotype=quran&browseby=surah';
	var myAjax = new Ajax.Request(_noCache(url), {method:'GET', evalScripts:true, onSuccess:success, onFailure:showerror});
}

var browsebysubject = function(){
	$('loadingimg').show();	
	var success	= function(t){populateaudiocontainer(t,'lectures','subject');}
	var url = 'index.cfm?do=cms.SubjectsList&audiotype=lectures&browseby=subject&origin=ajaxpage';
	var myAjax = new Ajax.Request(_noCache(url), {method:'GET', evalScripts:true, onSuccess:success, onFailure:showerror});
}

var browsebysubjectgroup = function(){
	$('loadingimg').show();	
	var success	= function(t){populateaudiocontainer(t,'lectures','subjectgroup');}
	var url = 'index.cfm?do=cms.SubjectGroupsList&audiotype=lectures&browseby=subjectgroup&origin=ajaxpage';
	var myAjax = new Ajax.Request(_noCache(url), {method:'GET', evalScripts:true, onSuccess:success, onFailure:showerror});
}

var populateaudiocontainer = function(t,audiotype,audiosubtype){
	//t.responseText.evalScripts();
	updatetabs(audiotype,audiosubtype);
	
	$('loadingimg').hide();
	$('audiocontainer').hide();
	$('audiocontainer').innerHTML = t.responseText;

	new Effect.BlindDown('audiocontainer', {duration: 0.5, delay:0.1, afterFinish:function(){ new Effect.ScrollTo('audio_explorer_tabs'); } });
	t.responseText.evalScripts();
}

var updatetabs = function(audiotype,audiosubtype){
	//update the tabs class
	// remove the prev active tab
	$('audio_explorer_tabs').getElementsByClassName('audtabs_active')[0].className = 'audtabs_default';
	//set the active class
	$(audiotype+'_tab').className = 'audtabs_active';
	
	// remove the prev active sub tab
	$('audio_explorer_tabs').getElementsByClassName('show')[0].className = 'hide';
	//set the active sub tab class
	$(audiosubtype+'_link').className = 'show';
	
	//remove the prev audio info txt
	$('audio_infotxt').getElementsByClassName('infoshow')[0].className = 'infohide';
	//show the appropriate infotxt
	$(audiotype+'_infotxt').className = 'infoshow';
}

var showqareelistforselectedsurah = function(surahid){
	$('load_'+surahid).innerHTML	= '<img src='+loadingimg+' border="0" />';	
	var success	= function(t){populateaudiocontainer(t,'quran','surah');}
	var url = 'index.cfm?do=cms.Surah&audiotype=quran&browseby=surah&surahid='+surahid;
	var myAjax = new Ajax.Request(_noCache(url), {method:'GET', evalScripts:true, onSuccess:success, onFailure:showerror});
}

var lb_showqareelistblk = function(id,type){
	$('load_'+id).innerHTML	= '<img src='+loadingimg+' border="0" />';

if(type == 'quran')
		var success	= function(t){populateaudiocontainer(t,'quran','surah');}
		var url = 'index.cfm?do=cms.Surah&audiotype=quran&browseby=surah&surahid='+id;
	var myAjax = new Ajax.Request(_noCache(url), {method:'GET', evalScripts:true, onSuccess:success, onFailure:showerror});
}

var showScholarslist = function(audiotype,audiosubtype){
	$('loadingimg').show();	
	var success	= function(t){populateaudiocontainer(t,audiotype,audiosubtype);}
	var url = 'index.cfm?do=cms.ScholarsList&audiotype='+audiotype+'&browseby='+audiosubtype;
	var myAjax = new Ajax.Request(_noCache(url), {method:'GET', evalScripts:true, onSuccess:success, onFailure:showerror});	
}

var showscholarallsubjects = function(schid,audiotype,audiosubtype){
	$('load_'+schid).innerHTML	= '<img src='+loadingimg+' border="0" />';	
	var success	= function(t){populateaudiocontainer(t,audiotype,audiosubtype);}
	var url = 'index.cfm?do=cms.scholarallsubjects&schid='+schid+'&type='+audiotype+'&origin=ajaxpage';
	var myAjax = new Ajax.Request(_noCache(url), {method:'GET', evalScripts:true, onSuccess:success, onFailure:showerror});
}

var pageno = 1;
var showscholarsubjectaudios = function(schid,subjid,audiotype,audiosubtype, pageno){
	if($('load_'+subjid))
		$('load_'+subjid).innerHTML	= '<img src='+loadingimg+' border="0" />';	
		
	var success	= function(t){populateaudiocontainer(t,audiotype,audiosubtype);}
	var url = 'index.cfm?do=cms.scholarsubject&schid='+schid+'&subjid='+subjid+'&type='+audiotype+'&origin=ajaxpage&pageno='+pageno;
	var myAjax = new Ajax.Request(_noCache(url), {method:'GET', evalScripts:true, onSuccess:success, onFailure:showerror});
}

var showscholarquranaudios = function(schid,audiotype,audiosubtype){
	$('load_'+schid).innerHTML	= '<img src='+loadingimg+' border="0" />';	
	var success	= function(t){populateaudiocontainer(t,audiotype,audiosubtype);}
	var url = 'index.cfm?do=cms.scholartelawat&schid='+schid+'&type='+audiotype+'&origin=ajaxpage';
	var myAjax = new Ajax.Request(_noCache(url), {method:'GET', evalScripts:true, onSuccess:success, onFailure:showerror});
}

var showscholarkhutbahaudios = function(schid,audiotype,audiosubtype){
	$('load_'+schid).innerHTML	= '<img src='+loadingimg+' border="0" />';	
	var success	= function(t){populateaudiocontainer(t,audiotype,audiosubtype);}
	var url = 'index.cfm?do=cms.scholarkhutbah&schid='+schid+'&type='+audiotype+'&origin=ajaxpage';
	var myAjax = new Ajax.Request(_noCache(url), {method:'GET', evalScripts:true, onSuccess:success, onFailure:showerror});
}

var showKhutbahAudioslist = function(audiotype,audiosubtype){
	$('loadingimg').show();	
	var success	= function(t){populateaudiocontainer(t,audiotype,audiosubtype);}
	var url = 'index.cfm?do=cms.ScholarsAudioList&audiotype='+audiotype+'&browseby='+audiosubtype;
	var myAjax = new Ajax.Request(_noCache(url), {method:'GET', evalScripts:true, onSuccess:success, onFailure:showerror});	
}


var showKhutbahAudiosbyMonth = function(mn,yr){
	$('loadingimg').show();	
	var success	= function(t){populateaudiocontainer(t,'khutbah','khateebaudio');}
	var url = 'index.cfm?do=cms.ScholarsAudioList&audiotype=khutbah&browseby=khateebaudio&hjmonth='+mn+'&hjyear='+yr;
	var myAjax = new Ajax.Request(_noCache(url), {method:'GET', evalScripts:true, onSuccess:success, onFailure:showerror});	
}

var showaudiodetails = function(audioid,audiotype,audiosubtype){ 
	if($('audiodet_load'+audioid))
		$('audiodet_load'+audioid).innerHTML	= '<img src='+loadingimg+' border="0" />';
	var success	= function(t){populateaudiocontainer(t,audiotype,audiosubtype);}
	var url = 'index.cfm?do=cms.AudioDetails&audioid='+audioid+'&origin=ajaxpage&audiotype='+audiotype;
	var myAjax = new Ajax.Request(_noCache(url), {method:'GET', evalScripts:true, onSuccess:success, onFailure:showerror});
}

var showscholardetails = function(schid,audiotype,audiosubtype){
	if($('load_'+schid))
	  $('load_'+schid).innerHTML	= '<img src='+loadingimg+' border="0" />';	
	var success	= function(t){populateaudiocontainer(t,audiotype,audiosubtype);}
	var url = 'index.cfm?do=cms.scholarpage&schid='+schid+'&type='+audiotype+'&origin=ajaxpage';
	var myAjax = new Ajax.Request(_noCache(url), {method:'GET', evalScripts:true, onSuccess:success, onFailure:showerror});
}

var toggleClName = function(id, cls, newcls){
	$(id).removeClassName(cls);
	$(id).addClassName(newcls);
}

var showAllScholars = function(){
	$('loadingimg').show();	
	var success	= function(t){populateaudiocontainer(t,'all','all');}
	var url = 'index.cfm?do=cms.allscholars&audiotype=all';
	var myAjax = new Ajax.Request(_noCache(url), {method:'GET', evalScripts:true, onSuccess:success, onFailure:showerror});
}

function moveToPrevious(id,currentPage,noofpages)
{	
	new Effect.MoveBy('insidediv',  0, -680);
	showNextPrevLink(currentPage,noofpages);
} 

function moveToNext(id,currentPage,noofpages)
{
	new Effect.MoveBy('insidediv', 0, 680);
	showNextPrevLink(currentPage,noofpages);
}

function showNextPrevLink(currentPage,noofpages){ 
	$('currpagedisplay').innerHTML = ' صفحه '+currentPage+' من '+noofpages;
	if(currentPage == 1 || typeof noofpages == 'undefined'){
		$('previouslink').style.visibility = 'hidden';
	}
	else
		$('previouslink').style.visibility = 'visible';
	if(currentPage  >= noofpages ){
		$('nextlink').style.visibility = 'hidden';
	}
	else
		$('nextlink').style.visibility = 'visible';
}

var showSubjectAudios = function(subjid,goto){
	if($('subjdet_load'+subjid))
		$('subjdet_load'+subjid).innerHTML = '<img src='+loadingimg+' border="0" />';
	var url = 'index.cfm?do=cms.subject&subjectid='+subjid+'&origin='+goto;
	var success	= function(t){populateaudiocontainer(t,'lectures','subject');}
	var myAjax = new Ajax.Request(_noCache(url), {method:'GET', evalScripts:true, onSuccess:success, onFailure:showerror});
}

var showGroupSubjects = function(grpid){
	$('subjdet_load'+grpid).innerHTML = '<img src='+loadingimg+' border="0" />';
	var success	= function(t){populateaudiocontainer(t,'lectures','subjectgroup');}
	var url = 'index.cfm?do=cms.SubjectsList&audiotype=lectures&browseby=subjectgroup&grpid='+grpid;
	var myAjax = new Ajax.Request(_noCache(url), {method:'GET', evalScripts:true, onSuccess:success, onFailure:showerror});
}

var changeusrpicture = function(){
 new Effect.BlindDown('usrpic');
}


/**************************************************************************************/
/****************************** AUDIO AJAX PAGING *************************************/
/**************************************************************************************/
var pagingaudtype = 'lectures';

var getNextSetofRecords = function(schid,subjid,pagingaudtyp, pgno, pagesize){
	$('fetchrec').innerHTML = '‫جلب البيانات‬.......<img src='+loadingimg+' border="0" />';
	var url = 'index.cfm?do=cms.ScholarSubjectAudios&schid='+schid+'&subjid='+subjid+'&type='+pagingaudtype+'&origin=ajaxpage&pgno='+pgno+'&pagesize='+pagesize;
	var myAjax = new Ajax.Request(_noCache(url), {method:'GET', evalScripts:true, onSuccess:populateschsubjaudioblk, onFailure:showerror});
}

var getNextSetofKhutbah = function(audtype,browseby,pgno, pagesize){
	$('fetchrec').innerHTML = '‫جلب البيانات‬.......<img src='+loadingimg+' border="0" />';
	var url = 'index.cfm?do=cms.ScholarsAudioListBlock&audiotype='+audtype+'&browseby='+browseby+'&origin=ajaxpage&pgno='+pgno+'&pagesize='+pagesize;
	var myAjax = new Ajax.Request(_noCache(url), {method:'GET', evalScripts:true, onSuccess:populateschsubjaudioblk, onFailure:showerror});
}

var getNextSetofScholarKhutbah = function(schid,audtype,pgno, pagesize){
	$('fetchrec').innerHTML = '‫جلب البيانات‬.......<img src='+loadingimg+' border="0" />';
	var url = 'index.cfm?do=cms.ScholarKhutbahAudiosBlock&schid='+schid+'&type='+audtype+'&origin=ajaxpage&pgno='+pgno+'&pagesize='+pagesize;
	var myAjax = new Ajax.Request(_noCache(url), {method:'GET', evalScripts:true, onSuccess:populateschsubjaudioblk, onFailure:showerror});
}
var populateschsubjaudioblk = function(t){
	$('fetchrec').innerHTML = '';
	t.responseText.evalScripts(); 
	$('lastrecdisplayed').innerHTML = rowdisplayed;
	if(rowdisplayed >= totalrec){
		$('nlb').innerHTML = '';
		$('nlb').hide();
	}
	new Insertion.Before('addrecshere', t.responseText);
}

/********showOrGo**************/
	var sog_last = 0;
	var showOrGo = function(gid,gourl)
		{
		var clicked_li = 'li_menu'+gid;
		var last_clicked_li = 'li_menu'+sog_last;
		//alert(obj.parentNode);
		var subid = 'subm_'+gid;
		if($(subid))
			{
			var last_subid = 'subm_'+sog_last;
			if(sog_last)
				{
				if(gid == sog_last)
					{
					// hide current
					//$(subid).hide();
					new Effect.BlindUp(subid, {duration:0.3});
					// return the default class
					$(clicked_li).className = 'main';
					sog_last = 0;
					}
				else
					{
					// hide last
					new Effect.BlindUp(last_subid, {duration:0.3, afterFinish:function ()
						{
						$(last_clicked_li).className = 'main';
						// show current
						new Effect.BlindDown(subid, {duration:0.3});
						$(clicked_li).className = 'main_active';
						}});
					//$('subm_'+sog_last).hide();
					sog_last = gid;
					}
				}
			else
				{
				//alert('show');
				// show current
				new Effect.BlindDown(subid, {duration:0.3});
				$(clicked_li).className = 'main_active';
				sog_last = gid;
				}
			}
		else{window.location.href=gourl;}
		}


////////////////////////////////////////////////////
// 				Start of Lecture Scheduler Scripts
////////////////////////////////////////////////////

var SCHEDMAIN = 'schedmain';
var LS_LOADER = 'ls_loader';
var LT_BLOCK_TITLE = '';

var lsByScholar = function (cityid,schlid)
	{
	var schlid = (typeof schlid == 'undefined')?'0':schlid;
	
	$(LS_LOADER).show();

	var success	= function(t){lsShowData(t,cityid);}
	var failure	= function(t){SendError(t);}


	var url = 'index.cfm?do=cms.LectSchedByScholar&cityid='+cityid
	if(arguments.length == 2)
		url += '&schlid='+schlid;
	var myAjax = new Ajax.Request(_noCache(url,2), { onSuccess:success, onFailure:failure});	
	};

var lsCity = function ()
	{
	$(LS_LOADER).show();

	var success	= function(t){lsShowData(t,1);}
	var failure	= function(t){SendError(t);}
	
	var url = 'index.cfm?do=cms.LectSchedCity';
//	if(arguments.length)
//		var url = 'index.cfm?do=cms.LectSchedByDay&dayid='+dayid;
	var myAjax = new Ajax.Request(_noCache(url,2), { onSuccess:success, onFailure:failure});	
	};

var lsByBook = function (cityid, bookid)
	{
	$(LS_LOADER).show();

	var success	= function(t){lsShowData(t,cityid);}
	var failure	= function(t){SendError(t);}
	
	var url = 'index.cfm?do=cms.LectSchedByBook&cityid='+cityid;
	if(arguments.length == 2)
		url += '&bookid='+bookid;
	var myAjax = new Ajax.Request(_noCache(url,2), { onSuccess:success, onFailure:failure});	
	};

var lsByDay = function (cityid, dayid)
	{
	$(LS_LOADER).show();

	var success	= function(t){lsShowData(t,cityid);}
	var failure	= function(t){SendError(t);}
	
	var url = 'index.cfm?do=cms.LectSchedByDay&cityid='+cityid;
	if(arguments.length == 2)
		url += '&dayid='+dayid;
	var myAjax = new Ajax.Request(_noCache(url,2), { onSuccess:success, onFailure:failure});	
	};

var lsDetail = function (cityid,schedid)
	{
	$(LS_LOADER).show();

	var success	= function(t){lsShowData(t,cityid);}
	var failure	= function(t){SendError(t);}
	
	var url = 'index.cfm?do=cms.LectSchedDetail&schedID='+schedid + '&cityID='+cityid;
	var myAjax = new Ajax.Request(_noCache(url,2), { onSuccess:success, onFailure:failure});	
	};

var lsShowData = function (t,cityid)
	{
		$('placeIndicator').src = (cityid == 1)?'images/icon-ka3bah.png':'images/icon-madinah.png';

		t.responseText.evalScripts();
		$(LS_LOADER).hide();
		$('schedmain').innerHTML = t.responseText;
		$('ls_links').update(lsLinks);
		$('schedTitleBlock').update(LT_BLOCK_TITLE);
	};

var SendError = function (t)
	{alert('عفوا حدث خطأ أثناء تحميل الصفحة فضلا أعد المحاولة');};

////////////////////////////////////////////////////
// 				End of Lecture Scheduler Scripts
////////////////////////////////////////////////////



