//image preloading
function prl(ss)
{
	var im = new Image();
	//im.src = site_host + ss;
	im.src = ss;
	return im;
}

function pLoa()
{
	i1 = prl('images/bg/green-grad-1.gif');
	i2 = prl('images/btn/' + fw_language + '.download.gif');
	i3 = prl('images/btn/' + fw_language + '.send.gif');
	i4 = prl('images/btn/' + fw_language + '.download-inactive.gif');
	i5 = prl('images/btn/' + fw_language + '.send-inactive.gif');
	i6 = prl('images/ico/money-jk.gif');
	i7 = prl('images/ico/phone-jk.gif');
	i8 = prl('images/ico/download-jk.gif');
	i9 = prl('images/ico/mail-jk.gif');

	g1 = prl('images/ico/grey_star.gif');
	b1 = prl('images/ico/blue_star.gif');
	p1 = prl('images/btn/plus.gif');
	m1 = prl('images/btn/minus.gif');
	w1 = prl('images/btn/' + fw_language + '.view-doc-over.gif');

	dm = prl('images/btn/' + fw_language + '.view-doc-over.gif');
	di = prl('images/btn/' + fw_language + '.view-doc.gif');
}

//vars
var submited = false;
var starvalue = 0;


function blocks(cont)
{
	var array = cont.split('|');
	if (array.length == 3)
	{
		executeByBlockAction(array[0], array[1], array[2]);
	}
}


function executeByBlockAction(block, action, content)
{
	if (block == 'stars')
	{
		if (action == 'newform')
		{
			if (!dBi('star_blc_frm'))
			{
				dBi('star_blc').innerHTML += content;
			}
		}
		else if (action == 'success')
		{
			dBi('star_blc_frm').innerHTML = '<div class="error" style="margin-bottom:0;">' + content + '</div>';
		}
	}
	else if (block == 'comments')
	{
		if (action == 'newform')
		{
			if (!dBi('comm_blc_frm'))
			{
				dBi('comm_blc').innerHTML += content;
			}
		}
		else if (action == 'error')
		{
			commentError(content);
		}
		else if (action == 'success')
		{
			dBi('comm_blc_frm_id').innerHTML = '<div class="error">' + content + '</div>';
		}
	}
	else if (block == 'report')
	{
		if (action == 'newform')
		{
			if (!dBi('report_blc_frm'))
			{
				dBi('report_blc').innerHTML += content;
			}
		}
		else if (action == 'error')
		{
			dBi('report_blc_frm_text_err').innerHTML = content;
			dBi('report_blc_frm_text_err').style.display = '';
		}
		else if (action == 'success')
		{
			dBi('report_blc_frm_id').innerHTML = '<div class="error">' + content + '</div>';
		}
	}
	else if (block == 's2f')
	{
		if (action == 'newform')
		{
			if (!dBi('s2f_blc_frm'))
			{
				dBi('s2f_blc').innerHTML += content;
			}
		}
		else if (action == 'error')
		{
			s2fError(content);
		}
		else if (action == 'success')
		{
			dBi('s2f_blc_frm_id').innerHTML = '<div class="error">' + content + '</div>';
		}
	}
	else if (block == 'comments_list')
	{
		if (action == 'anotherpage')
		{
			dBi('comments_list_blc').innerHTML = content;
		}
	}
}


function commentError(cont)
{
	var array = cont.split('#');
	arrLen = array.length;
	if (arrLen >= 2)
	{
		for (i=0; i < arrLen; i=i+2)
		{
			if (dBi('comm_blc_frm_' + array[i] + '_err'))
			{
				dBi('comm_blc_frm_' + array[i] + '_err').innerHTML = array[i+1];
				dBi('comm_blc_frm_' + array[i] + '_err').style.display = '';
			}
		}
	}
}


function s2fError(cont)
{
	var array = cont.split('#');
	arrLen = array.length;
	if (arrLen >= 2)
	{
		for (i=0; i < arrLen; i=i+2)
		{
			if (dBi('s2f_blc_frm_' + array[i] + '_err'))
			{
				dBi('s2f_blc_frm_' + array[i] + '_err').innerHTML = array[i+1];
				dBi('s2f_blc_frm_' + array[i] + '_err').style.display = '';
			}
		}
	}
}


function rtst(nr, total)
{
	if (!submited)
	{
		for (i=total; i>=1; i--)
		{
			if (nr < i) dBi('rtst'+i).src = g1.src;
			else dBi('rtst'+i).src = b1.src;
		}
	}
}

function cleanst(total)
{
	if (!submited)
	{
		starvalue = 0;
		for (i=total; i>=1; i--)
		{
			dBi('rtst'+i).src = g1.src;
		}
	}
}

function cleanstars(total)
{
	submited = false;
	cleanst(total);
}

function rtstsub(nr, total)
{
	submited = false;
	rtst(nr, total);
	submited = true;
	starvalue = nr;
	submitEssayRating();
}

function submitEssayComments()
{
	var name = dBi('escommentName');
	var description = dBi('esdescriptionName');
	var email = dBi('escommentEmail');
	sendRequest(site_host+action_url, 'submit=comm&id='+essay_id+'&fullname='+name.value+'&comment='+description.value+'&email='+email.value, 'POST');
	return false;
}

function submitEssayRating()
{
	sendRequest(site_host+action_url, 'submit=stars&id='+essay_id+'&rate='+starvalue, 'POST');
	return false;
}

function submitEssayReport()
{
	var text = dBi('reportText').value;
	var email = dBi('reportEmail').value;
	sendRequest(site_host+action_url, 'submit=report&id='+essay_id+'&text='+text+'&email='+email, 'POST');
	return false;
}

function submitEssayS2F()
{
	var email 	= dBi('friendEmail').value;
	var sender 	= dBi('senderName').value;
	var text 	= dBi('s2fText').value;

	sendRequest(site_host+action_url, 'submit=s2f&id='+essay_id+'&friend_email='+email+'&sender='+sender+'&message='+text, 'POST');
	return false;
}

function getCommentsList(pg)
{
	sendRequest(site_host+action_url, 'giveme=comments_list&e_id='+essay_id+'&cpage='+pg, 'POST');
}

function openCloseComments(what, actName)
{
	var li = dBi(actName);
	var liimg = dBi(actName + '_img');


	if (li.className.search(/active/) == -1)
	{
		li.className = 'action_block_active';

		liimg.src = m1.src;

		if (!dBi(actName + '_frm'))
		{
			sendRequest(site_host+action_url, 'giveme=' + actName, 'POST');
		}
		else
		{
			dBi(actName + '_frm').style.display = '';
		}
	}
	else
	{
		if (dBi(actName + '_frm'))
		{
			dBi(actName + '_frm').style.display = 'none';
		}
		li.className = '';
		liimg.src = p1.src;
	}
}

function checkIfNotEmpty(id)
{
	if (trim(dBi(id).value) != '')
	{
		return true;
	}
	else
	{	
		return false;
	}
}

function checkCode()
{
	if (checkIfNotEmpty('code'))
	{
		if(trim(dBi('code').value).length > 5){
			return true;
		}else{
			//alert (code_error_message);
			dBi('code').focus();
			return false;
		}
	}
	else
	{	
		//alert (code_error_message);
		dBi('code').focus();
		return false;
	}
}

function checkMail()
{
	var type = dBi('sp2').type;
	if (type == 'hidden')
	{
		if (checkIfNotEmpty('m_1')) return checkMailOneLt('m_1');
		else return false;
	}
	else
	{
		if (checkIfNotEmpty('sp2') && checkIfNotEmpty('m_1')) return checkMailOneLt('m_1');
		else return false;
	}
}

function checkMailWithoutCredits()
{		
	if(dBi('tofs_mail').checked == true)
	{
		//var type = dBi('code').type;

		if (checkIfNotEmpty('email'))
		{ 
			if(checkMailOneLt('email'))
			{
				var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				if (!filter.test(dBi('email').value)) 
				{
					//alert(email_error_message);
					dBi('email').focus();
					return false;
				}
				else return true;
			}
			else return false;
		}
		else
		{	//alert(email_error_message);
			dBi('email').focus();
			return false;
		}
	}
	else
	{
		return true;
	}
}

function  checkFields()
{
	if(!checkCode() || !checkMailWithoutCredits())
		return false;
	else
		return true;
}

function  checkTwoFields()
{
	if(!checkCode() && !checkMailWithoutCredits())
		return 3;
	else if (!checkCode())
		return 2;
	else if (!checkMailWithoutCredits())
		return 1;
	else return 0;
}

function checkMailOneLt(id)
{
	var one = trim(dBi(id).value);
	if (!one.match(new RegExp(/@one.lt$/i)))
	{
		return true;
	}
	else
	{
		return confirm(one_lt_confirm_message);
	}
}

function setBlockToShow(what)
{ 	
	var dwnType = (getCheckedById('tofs_mail')) ? 'byMail' : 'dwDir';
	var payment = (getCheckedById('tofp_co')) ? 'ByCo' : 'ByCr';
	
	what.showBlockAfterSubmit.value = dwnType + '|' + payment; 
}


function setBlockToShowWithoutPayments(what)
{ 	
	var dwnType = (getCheckedById('tofs_mail')) ? 'byMail' : 'dwDir';
	var payment = 'ByCo';
	
	what.showBlockAfterSubmit.value = dwnType + '|' + payment; 
}

function getCheckedById(id)
{
	return (dBi(id).checked == true);
}

function loadDocViewer(Ob){

	var objBody = document.getElementsByTagName("body").item(0);
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');

	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '99';
 	objOverlay.style.width = '100%';
	objBody.insertBefore(objOverlay, objBody.firstChild);

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';

	y=getPosY(Ob);
	if( dBi("docviewer") )
	{
			dBi('docviewer').style.display='';
			dBi('docviewer').style.top=arrayPageScroll[1]+160+"px";

			if (parseInt(navigator.appVersion)>3) {
			if (navigator.appName=="Netscape") {
			winW = window.innerWidth;
			winH = window.innerHeight;
			}
			if (navigator.appName.indexOf("Microsoft")!=-1) {
			winW = document.body.offsetWidth;
			winH = document.body.offsetHeight;
			}
			}

			dBi('docviewer').style.left=winW/2-(630/2)+"px";
	}
	BG = '1';
}

function getPageScroll(){
	var yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll)
	return arrayPageScroll;
}

function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

/* close docviewer window */
function DocViewerClose(){
var BG;
dBi('overlay').style.display='none';
dBi('docviewer').style.display='none';
BG = '0';
}

function resize()
{
	//alert(BG)
if(BG=='1')
{
var arrayPageSize = getPageSize();
var arrayPageScroll = getPageScroll();
dBi('overlay').style.height = (arrayPageSize[1] + 'px');
dBi('overlay').style.width = (screen.width-20 + 'px');
}
}

function getPosY(theObj){
  y = 0;
  while(theObj){
    y += theObj.offsetTop;
    theObj = theObj.offsetParent;
  }
 return y;
}

function hideLoading(){

if( dBi("loading") )dBi('loading').style.display='none';

}


function writeDocViewer(file){
	document.write('<object id="docviewerflash" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="630" height="500" id="docviewer" align="middle">');
	document.write('<param name="allowScriptAccess" value="sameDomain" />');
	document.write('<param name="movie" value="container.swf?xml_file='+file+'" /><param name="quality" value="high" />');
	document.write('<param name="scale" value="noscale" /><param name="wmode" value="transparent" /><param name="bgcolor" value="#ffffff" />');
	document.write('<embed src="container.swf?xml_file='+file+'" quality="high" scale="noscale" wmode="transparent" bgcolor="#ffffff" width="630" height="500" name="docviewer" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');
}

//change visibility
function changeVisibility(ids,visType)
{
	for (i=0; i<ids.length; i++)
	{
		if(dBi(ids[i]))
		{
			if (visType == 'hide')
				displayState(ids[i], 'none');
			else if(visType == 'show')
				displayState(ids[i], '');
		}
	}
	return;
}

function displayState(id, state)
{
	if(dBi(id)){
		dBi(id).style.display = state;
	}
	else
	{
		alert("id:"+ id +" not found!");
		return;
	}
}


function procToolboxSel(changePaymentBg, disableOnCredit, withCredits)
{
	
	var runTimeoutFunc;
	var procIdVisibility = new Array();

	if(typeof(changePaymentBg) == 'undefined'){
		var changePaymentBg = true;
	}
	if(typeof(disableOnCredit) == 'undefined'){
		var disableOnCredit = false;
	}
	if(typeof(withCredits) == 'undefined'){
		var withCredits = true;
	}
	
	if(changePaymentBg)
	{ 
		if (dBi('typeOfPaymentDiv'))
		{
			var pmnt = dBi('typeOfPaymentDiv').style;

			if (getCheckedById("tofp_cr")) //credit
			{	
				pmnt.background = 'url(/images/ico/money-jk.gif) 125px 3px no-repeat';
				if(typeof(pay_credit_show) != 'undefined'){ changeVisibility(pay_credit_show, 'show'); }
				if(typeof(pay_credit_hide) != 'undefined'){ changeVisibility(pay_credit_hide, 'hide'); }
				if(disableOnCredit)
				{
					dBi('email').disabled=true;
					dBi('submit_img').disabled=true;
					dBi('submit_img').style.display='none'; 
					dBi('submit_img_inactive').style.display='';

				}
			}
			else if(getCheckedById("tofp_co")) //code
			{
				pmnt.background = 'url(/images/ico/phone-jk.gif) 132px 3px no-repeat';
				if(typeof(pay_code_show) != 'undefined'){ changeVisibility(pay_code_show, 'show'); }
				if(typeof(pay_code_hide) != 'undefined'){ changeVisibility(pay_code_hide, 'hide'); }
				if(disableOnCredit)
				{
					dBi('email').disabled=false;
					dBi('submit_img').disabled=false;
					dBi('submit_img').style.display='';
					dBi('submit_img_inactive').style.display='none';
				}
			}
		}
		else
		{
			runTimeoutFunc = true;
		}
	}


	if(dBi('typeOfDownloadDiv'))
	{	
		var dwnl = dBi('typeOfDownloadDiv').style;
		if(getCheckedById("tofs_dir"))
		{	
			dwnl.background = 'url(/images/ico/download-jk.gif) 125px 3px no-repeat';
			if(typeof(down_direct_show) != 'undefined'){ changeVisibility(down_direct_show, 'show'); }
			if(typeof(down_direct_hide) != 'undefined'){ changeVisibility(down_direct_hide, 'hide'); }
		}
		else if(getCheckedById("tofs_mail"))
		{   
			dwnl.background = 'url(/images/ico/mail-jk.gif) 125px 3px no-repeat';
			if(typeof(down_email_show) != 'undefined'){ changeVisibility(down_email_show, 'show'); }
			if(typeof(down_email_hide) != 'undefined'){ changeVisibility(down_email_hide, 'hide'); }
		}
		
		//if (withCredits == true) changeBackground();
	}
	else
	{
		runTimeoutFunc = true;
	}


	if(runTimeoutFunc)
	{
		setTimeout("changeBackground("+changePaymentBg+", "+changeDownloadBg+")", 100)
	}
	
	// Conditions array processing
	if (changePaymentBg)
	{
		if (dBi('typeOfPaymentDiv'))
			{
				
	
				if (getCheckedById("tofp_cr")) //credit
				{	
		
						if(dBi('typeOfDownloadDiv'))
						{	
							if(getCheckedById("tofs_dir"))
							{	
								
								if(typeof(dwDirByCr_show) != 'undefined'){ changeVisibility(dwDirByCr_show, 'show'); }
								if(typeof(dwDirByCr_hide) != 'undefined'){ changeVisibility(dwDirByCr_hide, 'hide'); }
							}
							else if(getCheckedById("tofs_mail"))
							{   
								
								if(typeof(byMailByCr_show) != 'undefined'){ changeVisibility(byMailByCr_show, 'show'); }
								if(typeof(byMailByCr_hide) != 'undefined'){ changeVisibility(byMailByCr_hide, 'hide'); }
							}
							
				
						}
				}
				else if(getCheckedById("tofp_co")) //code
				{
					
							if(dBi('typeOfDownloadDiv'))
							{	
								if(getCheckedById("tofs_dir"))
								{	
									
									if(typeof(dwDirByCo_show) != 'undefined'){ changeVisibility(dwDirByCo_show, 'show'); }
									if(typeof(dwDirByCo_hide) != 'undefined'){ changeVisibility(dwDirByCo_hide, 'hide'); }
								}
								else if(getCheckedById("tofs_mail"))
								{   
									
									if(typeof(byMailByCo_show) != 'undefined'){ changeVisibility(byMailByCo_show, 'show'); }
									if(typeof(byMailByCo_hide) != 'undefined'){ changeVisibility(byMailByCo_hide, 'hide'); }
								}
								
								//if (withCredits == true) changeBackground();
							}
				}
			}
	}



	
	return;
}

/* most probably should be removed */
/*function disableErrorBoxes()
{
	var allPagetags = document.getElementsByTagName("div");
	for (var i=0; i<allPagetags.length; i++)
	{
		if (allPagetags[i].className=='inner-error-jk')
		{
			allPagetags[i].style.display='none';
		}
	}
}*/


function changeBackground()
{
	if (dBi('typeOfPaymentDiv') && dBi('typeOfDownloadDiv'))
	{
		var pmnt = dBi('typeOfPaymentDiv').style;
		var dwnl = dBi('typeOfDownloadDiv').style;
		if (getCheckedById("tofp_cr"))
		{
			pmnt.background = 'url(/images/ico/money-jk.gif) 125px 3px no-repeat';
			pmntType = 'ByCr';
		}
		else if(getCheckedById("tofp_co"))
		{
			pmnt.background = 'url(/images/ico/phone-jk.gif) 132px 3px no-repeat';
			pmntType = 'ByCo';
		}
		if(getCheckedById("tofs_dir"))
		{
			dwnl.background = 'url(/images/ico/download-jk.gif) 125px 3px no-repeat';
			dwnType = 'dwDir';
		}
		else if(getCheckedById("tofs_mail"))
		{
			dwnl.background = 'url(/images/ico/mail-jk.gif) 125px 3px no-repeat';
			dwnType = 'byMail';
		}

		//showPaymentBlock();
	}
	else
	{
		setTimeout("changeBackground()", 100)
	}
}

/*
function showPaymentBlock()
{
	var id = dwnType + pmntType;
	var arr = new Array('dwDirByCo', 'byMailByCo', 'dwDirByCr', 'byMailByCr');
	for (i=0; i<arr.length; i++)
	{
		if (id != arr[i]) displayState(arr[i], 'none');
		else displayState(arr[i], '');
	}
}
*/


function checkIfJSenabled()
{
	dBi('js_enabled').value = '1';
}

function checkFieldsAdv()
{	
	var dwnType = (getCheckedById('tofs_mail')) ? 'byMail' : 'dwDir';
	var paytype = 'ByCo';
	if (dBi('tofp_cr'))
	{
		if (getCheckedById('tofp_cr') == true) paytype = 'ByCr';
			else paytype = 'ByCo';
	}

		
	var id = dwnType + paytype;
	
	
	if (id=='byMailByCr')  
	{
		if(checkMailWithoutCredits())
		{
			return true;
		}
		else 
		{
			alert(email_error_message);
			return false;
		}
	}
	if (id=='byMailByCo') 
	{
		if(checkTwoFields()==3)  { alert(code_error_message+'\n'+email_error_message); return false; }
		else if(checkTwoFields()==2) { alert(code_error_message); return false; }
		else if(checkTwoFields()==1) { alert(email_error_message); return false; }
		else return true;
	}
	
	if (id=='dwDirByCo') 
	{	
		if (checkCode()== false ) 
		{	
			alert (code_error_message);
			return false;
		}
		else 
		{ 
			return true;
		}
	}
		
}


