/**
 * dialogBoy section
 */

var dBoySendFriend = 0, dBoyReport = 0, dBoyProgress = 0;

var dBoyDefaultSFEmail = '', dBoyDefaultSFMessage = '', dBoyDefaultReportMistake = '';

function submitDialogBoy() {
	if (dBoyProgress == 2) {
		submitDialogBoyReport();
	} else if (dBoyProgress == 4) {
		submitDialogBoySendFriend();
	}
}



function closeDialogBoy() {
	hideObj('dialogboy');
	dBoyProgress = 0;
	hideObj('dialogshadow');
}



function showShadow() {
	YAHOO.util.Dom.setStyle('dialogshadow', 'height', YAHOO.util.Dom.getViewportHeight() + 'px');
	if (YAHOO.util.Dom.hasClass('dialogshadow', 'div_hidden')) YAHOO.util.Dom.replaceClass('dialogshadow', 'div_hidden', 'div_visible');
}



function showDialogBoyReport(reportText) {

	if (dBoyProgress == 0) {
		dBoyReport = Math.ceil(Math.random()*9999999) + 1;
		setdBoyHeader('Report mistake');
		setdBoyContent('Please wait while loading...');
		oDisable('dBoySubmit');

		var callback = {
			success: function(o) {
				oid('dBoyContent').className = "dBoy_Standard";
				if (o.responseText != '') { 
					setdBoyContent(o.responseText);
					dBoyDefaultReportMistake = document.dialogBoyReportMistake.mistakeDescription.value;
					if (!isEmpty(reportText)) document.dialogBoyReportMistake.mistakeDescription.value = reportText;
				} else {
					this.failure(o);
				}
									
			},  
			failure: function(o) {
				oid('dBoyContent').className = "dBoy_Error";
				oid('dBoyContent').innerHTML = "Internal communication error";			
			},  
			argument: [],
			cache: false 
		} 
		
		dBoyProgress = 1;
		var transaction = YAHOO.util.Connect.asyncRequest('POST', 'ajax_wrapper.php?action=getFormReport', callback, 'RepNum=' + dBoyReport);
		
		var nx = Math.ceil((YAHOO.util.Dom.getViewportWidth() - 400)/2);
		var ny = 100;
		showShadow();
		YAHOO.util.Dom.setXY('dialogboy', [nx, ny]);
		showDiv('dialogboy');
		YAHOO.util.Dom.setXY('dialogboy', [nx, ny]);
		dBoyProgress = 2;
	}
	
	return false;
}



function submitDialogBoyReport() {
	document.dialogBoyReportMistake.numB.value = parseInt(document.dialogBoyReportMistake.numA.value) + dBoyReport;
	dBoyReport = Math.ceil(Math.random()*9999999) + 1;
	document.dialogBoyReportMistake.RepNum.value = dBoyReport;
	document.dialogBoyReportMistake.mistakeURL.value = document.URL;
	YAHOO.util.Connect.setForm(oid('dialogBoyReportMistake'));
	
	var callback = {
		upload: function(o) {
			alert('upload: ' + o.responseText);
		},
		success: function(o) {
			if (o.responseText == 'YES') {
				alert('Your mistake report has been sent.\n\nThank You very much.');
				hideObj('dialogshadow');
				hideObj('dialogboy');
				dBoyProgress = 0;

			} else {
				var contentBackup = document.dialogBoyReportMistake.mistakeDescription.value;
				alert('Submit failed.\n\nIf You are reporting mistakes on multiple pages simultaneously,\nplease, report them separately.');
				dBoyProgress = 0;
				showDialogBoyReport(contentBackup);
				dBoyProgress = 2;
				validateDialogBoyReport();
			}
			
		},
		failure: function(o) {
			alert('failure: ' + o.responseText);
			
		},
		timeout: 3600,
		argument: ['numA', 'numB', 'mistakeURL', 'mistakeDescription'],
		cache: false
		
	}
	
	YAHOO.util.Connect.asyncRequest('POST', 'ajax_wrapper.php?action=submitFormReport', callback); 
}



function validateDialogBoyReport() {
	if ((dBoyDefaultReportMistake == document.dialogBoyReportMistake.mistakeDescription.value
		|| (document.dialogBoyReportMistake.mistakeDescription.value == ''))) {
		oDisable('dBoySubmit');
	} else {
		oEnable('dBoySubmit');
	}
}



function showDialogBoySendFriend(email, message) {

	if (dBoyProgress == 0) {
		dBoySendFriend = Math.ceil(Math.random()*9999999) + 1;
		setdBoyHeader('Send to friend');
		setdBoyContent('Please wait while loading...');
		oDisable('dBoySubmit');

		var callback = {
			success: function(o) {
				oid('dBoyContent').className = "dBoy_Standard";
				if (o.responseText != '') {
					setdBoyContent(o.responseText);
					dBoyDefaultSFEmail = document.dialogBoySendFriend.sendfriendEMAIL.value;
					if (!isEmpty(email)) document.dialogBoySendFriend.sendfriendEMAIL.value = email;
					dBoyDefaultSFMessage = document.dialogBoySendFriend.sendfriendMessage.value;
					if (!isEmpty(message)) document.dialogBoySendFriend.sendfriendMessage.value = message;
				} else {
					this.failure(o);
				}
									
			},  
			failure: function(o) {
				oid('dBoyContent').className = "dBoy_Error";
				oid('dBoyContent').innerHTML = "Internal communication error";			
			},  
			argument: [],
			cache: false 
		} 
		
		dBoyProgress = 3;
		var transaction = YAHOO.util.Connect.asyncRequest('POST', 'ajax_wrapper.php?action=getFormSendFriend', callback, 'SFNum=' + dBoySendFriend);
		
		var nx = Math.ceil((YAHOO.util.Dom.getViewportWidth() - 400)/2);
		var ny = 100;
		showShadow();
		YAHOO.util.Dom.setXY('dialogboy', [nx, ny]);
		showDiv('dialogboy');
		YAHOO.util.Dom.setXY('dialogboy', [nx, ny]);
		dBoyProgress = 4;
	}
	
	return false;
}



function submitDialogBoySendFriend() {
	document.dialogBoySendFriend.numB.value = parseInt(document.dialogBoySendFriend.numA.value) + dBoySendFriend;
	dBoySendFriend = Math.ceil(Math.random()*9999999) + 1;
	document.dialogBoySendFriend.SFNum.value = dBoyReport;
	document.dialogBoySendFriend.sendfriendURL.value = document.URL;
	YAHOO.util.Connect.setForm(oid('dialogBoySendFriend'));
	
	var callback = {
		upload: function(o) {
			//alert('upload: ' + o.responseText);
		},
		success: function(o) {
			if (o.responseText == 'YES') {
				alert('Your message to friend has been sent to e-mail address You have entered.\n\nThank You very much.');
				hideObj('dialogshadow');
				hideObj('dialogboy');
				dBoyProgress = 0;

			} else {
				var sfEmail = document.dialogBoySendFriend.sendfriendEMAIL.value;
				var sfMessage = document.dialogBoySendFriend.sendfriendMessage.value;
				
				alert('Submit failed.\n\nIf You are recommending multiple pages simultaneously,\nplease, recommend them separately.');
				dBoyProgress = 0;
				showDialogBoySendFriend(sfEmail, sfMessage);
				dBoyProgress = 4;
				validateDialogBoySendFriend();
			}
			
		},
		failure: function(o) {
			alert('Failure: SFSF communication problem. Please, check Your internet connectivity and try again later.');
			
		},
		timeout: 3600,
		argument: ['numA', 'numB', 'sendfriendURL', 'sendfriendEMAIL', 'sendfriendMessage'],
		cache: false
		
	}
	
	YAHOO.util.Connect.asyncRequest('POST', 'ajax_wrapper.php?action=submitFormSendFriend', callback); 	
}



function validateDialogBoySendFriend() {
	if ((dBoyDefaultSFEmail == document.dialogBoySendFriend.sendfriendEMAIL.value)
		|| !validateEmail(document.dialogBoySendFriend.sendfriendEMAIL.value)
		|| (dBoyDefaultSFMessage == document.dialogBoySendFriend.sendfriendMessage.value)) {
		oDisable('dBoySubmit');
	} else {
		oEnable('dBoySubmit');
	}	
}