
live_site = "";
username_flag = false;
useremail_flag = false;
payment_window = null;

TEST_VALUE = 0;
/**
 * This function is called after the whole page has been loaded.
 */
function init(site)
{
	//inits the tool tips
	inittooltips();
	
	//init lunch field
	init_lunch();
	
	live_site = site;
}  //init


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=/";
}  //createCookie

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;
}  //readCookie


/**
 * This function checks whether the lunch radio button 'Yes' is clicked and activates the lunch field
 */
function init_lunch()
{
	var saved = readCookie('lam');
	if (saved && saved!='-1') {
		set_lunch(true);	
	} else {
		set_lunch(false);
	}
	createCookie('lam',-1,-1);

	//inits the pay button
	var obj = document.getElementById('paybutton');
	var don = document.getElementById('donation_amount');
	if (obj && obj.disabled) {
		if ( don && !isNaN(parseInt(don.value)) ) {
			obj.disabled = false;
		} 
	}
}  //init_lunch

/**
 * Activates/deactivates the field for lunch amount
 *
 */
function set_lunch(flag)
{
	var text   = document.getElementById('lunch_text');
	var dollar = document.getElementById('baks_lunch_text');
	var field  = document.getElementById('lunch_amount');
	var dis = "text_disabled";
	var en  = "text_enabled";
	var str = "text_strong";
	var lunch_cost = 15;         //the price for the lunch in USD

	if (field) field.disabled = !flag;
	if (flag) {
		if (text) text.className = en;
		if (dollar) dollar.className = str;
		
		//calculate the total amount for the lunch
		var visiters = (document.getElementById('total_accomodation'))?parseInt(document.getElementById('total_accomodation').value) : 1;
		lunch_cost *= visiters;
		if (field) field.value = lunch_cost;
		
		var obj = document.getElementById('paybutton');
		if (obj) obj.disabled = false;
	} else {
		if (text) text.className = dis;
		if (dollar) dollar.className = dis;
		if (field) field.value = "";

		var obj = document.getElementById('paybutton');
		var don = document.getElementById('donation_amount');
		if (don && !isNaN(parseInt(don.value)) && parseInt(don.value)>0) {
			if (obj) obj.disabled = false;
		} else {
			if (obj) obj.disabled = true;
		}
	}

}  //set_lunch

/**
 * Checks the form values before send the request about payment to paypal from the user profile page.
 */
function updatepayment(frm)
{
	if (!frm) return false;
	if (!frm.total_accomodation || !frm.lunch_amount || !frm.amount) return false;
	var total_amount = eval(frm.total_accomodation.value * 15);
	frm.lunch_amount.value = total_amount;
	frm.amount.value = total_amount;
	return true;
}  //updatepayment


/**
 * Opens the new window with paypal payment
 */
function pay(site)
{
	
	var obj = document.getElementById('userpaid');
	if (obj) obj.value = '1';
	obj.form.submit();
	return;
/*	
	var w = 800;
	var h = 650;
	var f = 'width='+w+',height='+h+',title=yes,resizable=yes,status=yes,scrollbars=yes';
	if (screen)	{
		var ww = screen.width/2-w/2;
		var hh = screen.height/2-h;
		f += ', left='+ww+', top='+hh;
	}
	
	var query = "?amount=";
	
	var amount  = 0;
	var item_id = 0;
	var item_name = '';
	
	
	var obj = document.getElementById('lunch_amount');
	if (obj && obj.value!="" && !isNaN(parseFloat(obj.value)) ) {
		amount += parseFloat(obj.value);
		item_id = 2;
		item_name = 'lunch';
	}
	obj = document.getElementById('donation_amount');
	if (obj && obj.value!="" && !isNaN(parseFloat(obj.value)) ) {
		amount += parseFloat(obj.value);
		if (item_id==2) {
			item_id = 3;
			item_name = 'mix';
		} else {
			item_id = 1;
			item_name = 'donation';
		}
	}
	
	if (isNaN(amount) || amount==0) {
		alert('Если вы хотите сделать пожертвование, введите, пожалуйста, сначала сумму в поле для пожертвования.');
		return;
	}
	
	query += amount + "&item_id="+item_id+"&item_name="+item_name;
	
	query = site + query;
	
	payment_window = window.open( query, '_blank', f );
	//payment_window = window.open( "http://localhost/churchny/ext/php/paypal_nexttime.php", '_blank', f);
	
	setTimeout( 'check_payment_process();', 2000 );
*/
}  //pay


function check_payment_process()
{
	if (payment_window && !payment_window.closed) {
		setTimeout( 'check_payment_process();', 1000 );
	} else {
		alert(TEST_VALUE);
		var obj = document.getElementById('submitbutton');
		if (obj) obj.form.submit();
	}
} //check_payment_process



/**
 * callback function; called when the payment was successfull.
 */
function paid()
{
	var obj = document.getElementById('userpaid');
	if (obj) {
		obj.value = '1';
		obj.form.submit();
	}
}  //paid


function check_email(email,status)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {
		useremail_flag = false;
		if (status) {
			status.innerHTML="<img src=\""+live_site+"/ext/images/invalid.gif\" width=\"16\" height=\"16\">";
		}
	} else {
		useremail_flag = true;
		if (status) {
			status.innerHTML="<img src=\""+live_site+"/ext/images/valid.gif\" width=\"16\" height=\"16\">";
		}
	}
}  //check_email

////////////////////////////////////
//
// Functions using AJAX
//
////////////////////////////////////

dxmlHttp = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
  dxmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
  try {
    dxmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e2) {
    dxmlHttp = false;
  }
}
@end @*/

if (!dxmlHttp && typeof XMLHttpRequest != 'undefined') {
  dxmlHttp = new XMLHttpRequest();
}

/**
 * Checks whether the username exists or not
 *
 * @param string username - the username
 */
function ajax_check_username(username)
{
	var img_obj = document.getElementById('username_pic');
	if (img_obj) {
		var s = "<img src=\""+live_site+"/ext/images/fetching.gif\" width=\"16\" height=\"16\">";
		img_obj.innerHTML = s;
	}
	
	var url = live_site+"/ext/php/ajax_functions.php?function=check_username&value="+username;
	
	dxmlHttp.open("GET", url, true);

	// Установить функцию для сервера, которая выполнится после его ответа
  	dxmlHttp.onreadystatechange = ajaxUpdateUsername;

  	// SПередать запрос
  	dxmlHttp.send(null);
  	
}  //ajax_check_username


function ajaxUpdateUsername()
{
	if (dxmlHttp.readyState == 4) {
    	var response = parseInt(dxmlHttp.responseText);
    	var img_obj = document.getElementById('username_pic');
    	if (isNaN(response)) {
    		if (img_obj) img_obj.innerHTML="";
    		username_flag = false;
    	} else if (response==0) {
	    	if (img_obj) img_obj.innerHTML="<img src=\""+live_site+"/ext/images/valid.gif\" width=\"16\" height=\"16\">";
	    	username_flag = true
    	} else {
	    	if (img_obj) img_obj.innerHTML="<img src=\""+live_site+"/ext/images/invalid.gif\" width=\"16\" height=\"16\">";
	    	username_flag = false;
    	}
  	}  
}  //ajaxUpdateUsername


/**
 * Checks whether the email exists or not
 *
 * @param string username - the username
 */
function ajax_check_email(email)
{
	var img_obj = document.getElementById('email_pic');
	if (img_obj) {
		var s = "<img src=\""+live_site+"/ext/images/fetching.gif\" width=\"16\" height=\"16\">";
		img_obj.innerHTML = s;
	}
	
	var url = live_site+"/ext/php/ajax_functions.php?function=check_email&value="+email;
	
	dxmlHttp.open("GET", url, true);

	// Установить функцию для сервера, которая выполнится после его ответа
  	dxmlHttp.onreadystatechange = ajaxUpdateEmail;

  	// SПередать запрос
  	dxmlHttp.send(null);
  	
}  //ajax_check_email


function ajaxUpdateEmail()
{
	if (dxmlHttp.readyState == 4) {
    	var response = parseInt(dxmlHttp.responseText);
    	var img_obj = document.getElementById('email_pic');
    	if (isNaN(response)) {
    		if (img_obj) img_obj.innerHTML="";
    		useremail_flag = false;
    	} else if (response==0) {
	    	if (img_obj) img_obj.innerHTML="<img src=\""+live_site+"/ext/images/valid.gif\" width=\"16\" height=\"16\">";
	    	useremail_flag = true
    	} else {
	    	if (img_obj) img_obj.innerHTML="<img src=\""+live_site+"/ext/images/invalid.gif\" width=\"16\" height=\"16\">";
	    	useremail_flag = false;
    	}
  	}  
}  //ajaxUpdateEmail


function ajaxCheckUserInfoForUpdation()
{
	var img_obj = document.getElementById('status_pic');
	if (img_obj) {
		var s = "<img src=\""+live_site+"/ext/images/fetching.gif\" width=\"16\" height=\"16\">";
		img_obj.innerHTML = s;
	}
	
	var url = live_site+"/ext/php/ajax_functions.php?function=update_user_info";
	
	var user_id;
	if (document.getElementById('user_id')) user_id = document.getElementById('user_id').value; else return;
	var visitors = 1;
	if (document.getElementById('total_accomodation')) visitors = document.getElementById('total_accomodation').value;	
	var lunch;
	if (document.getElementById('lunch_radio') && document.getElementById('lunch_radio').checked) lunch=1; else lunch=0;
	var lunch_amount=0;
	if (document.getElementById('lunch_amount')) lunch_amount=document.getElementById('lunch_amount').value;	var phone = "";
	if (document.getElementById('phone')) phone=document.getElementById('phone').value;	
	var address = "";
	if (document.getElementById('address')) address=document.getElementById('address').value;	
	var order_id=-1;
	if (document.getElementById('custom')) order_id=document.getElementById('custom').value;
	
	url += "&user_id="+user_id+"&visitors="+visitors+"&lunch="+lunch+"&lunch_amount="+lunch_amount+"&order_id="+order_id+"&phone="+escape(phone)+"&address="+escape(address);

	dxmlHttp.open("GET", url, true);

	// Установить функцию для сервера, которая выполнится после его ответа
  	dxmlHttp.onreadystatechange = ajaxDoNothing;

  	// SПередать запрос
  	dxmlHttp.send(null);
}  //ajaxUpdateUserInfo


function ajaxDoNothing()
{
	//whatever...
}