var min=12;
var max=60;

function login(srcElement) {
	var values = {
		url :"/ajax/login/",
		handleAs :"xml",
		load : function(data, ioArgs) {
			console.log("POST SUCCESS:" + data);
			var SessionId = data.getElementsByTagName("SessionId")[0].firstChild.nodeValue;
			var Message = data.getElementsByTagName("Message")[0].firstChild.nodeValue;
			dojo.byId('Login_Message').innerHTML = Message;
			if (SessionId.length != 1)
				location.href = '/admin/index?sesid=' + SessionId;
		},
		error : function(data, ioArgs) {
			console.log("POST ERROR:" + data);
		},
		timeout :10000,
		content : {
			'txtUsername' :dojo.byId('txtUsername').value,
			'txtPassword' :dojo.byId('txtPassword').value
		}
	};
	dojo.xhrPost(values);
}

function getVerses(No) {
	try {
		dojo.byId("pagenumber").selectedIndex = 0;
	} catch (ex) {

	}
	var values = {
		url :"/ajax/getverse/",
		handleAs :"xml",
		load : function(data, ioArgs) {
			console.log("POST SUCCESS:" + data);
			var verseCount = data.getElementsByTagName("Count")[0].firstChild.nodeValue;
			var verses = dojo.byId("verse");
			while (verses.childNodes.length >= 1) {
				verses.removeChild(verses.firstChild);
			}
			for (i = 1; i <= verseCount; i++) {
				var option = document.createElement("option");
				option.setAttribute("value", i);
				option.innerHTML = i;
				if(No==i)option.selected="1";
				verses.appendChild(option);
			}
			getTasfirs();

		},
		error : function(data, ioArgs) {
			console.log("POST ERROR:" + data);
		},
		timeout :10000,
		content : {
			'sVerseName' :dojo.byId("sura").value
		}
	};
	dojo.xhrPost(values);

}

function getTasfirs() {
	var requestedTasfirs = "";
	for (i = 0; i <= (dojo.byId("tafsircount").value ); i++) {

		if (dojo.byId("taf_" + i).checked == 1)
			requestedTasfirs +=dojo.byId("taf_" + i).value + "|";
			
	}
	var values = {
		url :"/ajax/gettafsirs/",
		handleAs :"text",
		load : function(data, ioArgs) {
			console.log("POST SUCCESS:" + data);
			dojo.byId("bigcolumn").innerHTML = data;
		},
		error : function(data, ioArgs) {
			console.log("POST ERROR:" + data);
		},
		timeout :10000,
		content : {
			'sSuraName' :dojo.byId("sura").value,
			'sVerseNo' :dojo.byId("verse").value,
			'sTasfirs' :requestedTasfirs
		}
	};
	dojo.xhrPost(values);

}

function getKoran(Page) {
		Set_Cookie("CurrentPage",Page, '', '/', '', '' );
	var values = {
		url :"/ajax/getkoran/",
		handleAs :"text",
		load : function(data, ioArgs) {
			console.log("POST SUCCESS:" + data);
			dojo.byId("bigcolumn").innerHTML = data;
		},
		error : function(data, ioArgs) {
			console.log("POST ERROR:" + data);
		},
		timeout :10000,
		content : {
			'sSuraName' :dojo.byId("sura").value,
			'sVerseNo' :dojo.byId("verse").value,
			'nPageNo' :(Page==null?dojo.byId("pagenumber").value:Page),
			'nPageType' :dojo.byId("pagetype").value
		}
	};
	dojo.xhrPost(values);

}

function checkPage(){
	if(Get_Cookie("CurrentPage")) getKoran(Get_Cookie("CurrentPage"));
}

function clearVerses() {
	dojo.byId("sura").selectedIndex = 0;
	var combo = dojo.byId("verse");
	while (combo.childNodes.length >= 1) {
		combo.removeChild(combo.firstChild);
	}
	var option = document.createElement("option");
	option.setAttribute("value", "");
	option.innerHTML = "Seçiniz...";
	combo.appendChild(option);
}

function setFont(size) {
	var div = document.getElementById("book_content");
	div.setAttribute("style", "font-size:" + size + "pt;");
}

function getVersesSpec(id) {
	var values = {
		url :"/ajax/getverse/",
		handleAs :"xml",
		load : function(data, ioArgs) {
			console.log("POST SUCCESS:" + data);
			var verseCount = data.getElementsByTagName("Count")[0].firstChild.nodeValue;
			var verses = dojo.byId("verse_" + id);
			while (verses.childNodes.length >= 1) {
				verses.removeChild(verses.firstChild);
			}
			for (i = 1; i <= verseCount; i++) {
				var option = document.createElement("option");
				option.setAttribute("value", i);
				option.innerHTML = i;
				verses.appendChild(option);
			}

		},
		error : function(data, ioArgs) {
			console.log("POST ERROR:" + data);
		},
		timeout :10000,
		content : {
			'sVerseName' :dojo.byId("sura_" + id).value
		}
	};
	dojo.xhrPost(values);

}

function getTasfirSpec(id) {
	var requestedTasfirs = document.getElementById("tafsir_" + id).value + "|";
	//alert(requestedTasfirs);
	var values = {
		url :"/ajax/test/",
		handleAs :"xml",
		load : function(data, ioArgs) {
			console.log("POST SUCCESS:" + data);
			alert(data);
			dojo.byId("bigcolumn").appendChild(data);
		},
		error : function(data, ioArgs) {
			console.log("POST ERROR:" + data);
		},
		timeout :10000,
		content : {
			'sSuraName' :dojo.byId("sura_" + id).value,
			'sVerseNo' :dojo.byId("verse_" + id).value,
			'sTasfirs' :requestedTasfirs,
			'sType' :'multi'
		}
	};
	
	dojo.xhrPost(values);

}

function redirecttoUpload(ownerId) {
	location.replace('/religion/upload?page=' + ownerId + '&book='
			+ document.getElementById("txtBook").value +'&header=' + (document.getElementById('addHeader').checked?'1':'0') +'&bookheader=' + (document.getElementById('addBookHeader').checked?'1':'0') );
}

function redirecttoTafsirUpload() {
	location.replace('/tafsir/upload?tafsir='
			+ document.getElementById("txtTafsirFile").value 
			+ '&tafsirDescription='
			+ document.getElementById("txtTafsirDescription").value)
}

function openWindow(Type,Url) {
	if(Type=='2')
	{
		Url='/index/index' + document.getElementById("book_" + Url).value;	
	}
	else
	{
		Url='/index/tafsir?sura=' + document.getElementById("sura_" + Url).value + '&verse=' + document.getElementById("verse_" + Url).value + '&tafsir=' + document.getElementById("tafsir_" + Url).value;		
	}
	day = new Date();
	id = day.getTime();
	var page= window.open(Url,'','toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=1280,height=800,left = 0,top = 0');
}

function callTafsir(sura,verse, tafsir){
	var suras=document.getElementById("sura");
	var verses=document.getElementById("verse");
	for(var i=0;i<suras.childNodes.length;i++) {
		if(suras.childNodes[i].value==sura)
		{
			suras.childNodes[i].selected="1";
			break;
		}
	}
	for (i = 0; i <= (dojo.byId("tafsircount").value ); i++) {
		
		 if (dojo.byId("taf_" + i).value == tafsir)
			 dojo.byId("taf_" + i).checked=true;
		
	}
	
	getVerses(verse);
}




function increaseFontSize() {
   var p = document.book_content_frame.document.getElementsByTagName('p');
   var span=document.book_content_frame.document.getElementsByTagName('span');
   var s;
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         s = 12;
      }
      if(s!=max) {
         s += 2;
      }
      p[i].style.fontSize = s+"px"
   }
   for(i=0;i<span.length;i++) {
      if(span[i].style.fontSize) {
         s = parseInt(span[i].style.fontSize.replace("px",""));
      } else {
         s = 12;
      }
      if(s!=max) {
         s += 2;
      }
      span[i].style.fontSize = s+"px";
   }
}
function decreaseFontSize() {
   var p = document.book_content_frame.document.getElementsByTagName('p');
   var span=document.book_content_frame.document.getElementsByTagName('span');
      var s;
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         s = 12;
      }
      if(s!=min) {
         s -= 2;
      }
      p[i].style.fontSize = s+"px";
   }  
   for(i=0;i<span.length;i++) {
      if(span[i].style.fontSize) {
         s = parseInt(span[i].style.fontSize.replace("px",""));
      } else {
         s = 12;
      }
      if(s!=max) {
         s += 2;
      }
      span[i].style.fontSize = s+"px";
   }
}

function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}


// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}


function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function clearTafsir(tafsirNo){
	dojo.byId('tafsir').value=tafsirNo;
	dojo.byId('versecontent').value="";
	dijit.byId('dialogTafsir').show();
}

function saveTasfirSpec() {
	var values = {
		url :"/ajax/savetafsir/",
		handleAs :"xml",
		load : function(data, ioArgs) {
			console.log("POST SUCCESS:" + data);
			if(data.getElementById("result").firstChild.nodeValue==1){
				dijit.byId('dialogTafsir').hide();
			}
			else
			{
				alert('Ayet kayıt edilemedi.Lütfen Tekrar deneyin');
			}
		},
		error : function(data, ioArgs) {
			console.log("POST ERROR:" + data);
		},
		timeout :10000,
		content : {
			'sura' :dojo.byId("sura_1" ).value,
			'verse' :dojo.byId("verse_1" ).value,
			'tafsir' :dojo.byId("tafsir" ).value,
			'versecontent' :dojo.byId("versecontent" ).value
		}
	};
	dojo.xhrPost(values);

}
