/**
 * Functions for reporting incorrect business profile information
 */

// wrapper object <=> namespace for related funcs
var IncorrectInfo = {};

IncorrectInfo.X_AJAX_INFO = 'users_incorrect_info';

IncorrectInfo.initialize = function() {
	
	IncorrectInfo.box = $('#incorrect_info_box');
	
	if( $('#logged_in').val() != 1 ) {
		$('a#incorrect_info_lnk').click(function(){$('a#login').trigger('click');return false;});
		return;
	}

	IncorrectInfo.boxContent = $('.content', IncorrectInfo.box);
	
	var frmOpen = function(hash) {
		
		var ajax_url = 'http://' + location.hostname + '/' + $('a#incorrect_info_lnk').attr('href'); // 'upgrade-business.html';
		
		$.get(ajax_url, function(response_data) { // , {ts: (new Date()).getTime()}
//			alert(response_data);
			IncorrectInfo.boxContent.html(response_data);
//			(IncorrectInfo.boxContent).get(0).innerHTML = response_data;
			hash.w.show();
//			hash.w.css('opacity', 1).show();
			
			$(IncorrectInfo.box).jqmAddClose($('#reportCloseButton', IncorrectInfo.box));
			$(IncorrectInfo.box).center();
			
//			parseForms();
			$('#submitReport', IncorrectInfo.box).click(
				function() {
//					alert('img submit clicked');
					var reportOptions = {
							url			: location.href,
							data		: { X_AJAX_INFO : IncorrectInfo.X_AJAX_INFO,
											ts			: (new Date()).getTime(),
											'action'    : 'action_submit',
											id_company	: $('#id_company').val()
										  },
							success		: IncorrectInfo.updateCheck,
							dataType	: 'json' // 'text'
					};
					$('#incorrectInfoFrm', IncorrectInfo.box).ajaxSubmit(reportOptions);
					return false;
				}
			);
			//*/
		});
	};
	
	IncorrectInfo.box.jqm({trigger: 'a#incorrect_info_lnk',  overlay: 0, onShow: frmOpen});
	
}

IncorrectInfo.renderObj = function(o, is_recursion) {
	var s = '';
	var c = is_recursion ? "\t" : '';
	var nl = "\n" + c;
	for(var p in o) {
		if(typeof o[p] == 'object') {
			s += c + p + ' = ' + "\n\t" + IncorrectInfo.renderObj(o[p], true);
		} else {
			s += c + p + ' = ' + o[p];
		}
		c = nl;
	}
	return s;
}

IncorrectInfo.updateCheck = function(data, status, form) {
	// alert(IncorrectInfo.renderObj(data));return;

	if(data.status == 'success') {
		alert('The notification message of incorrect information successfully sent.\nThank you for your contribution !');
		$('#reportCloseButton').trigger('click'); // close the dialog box
	} else if(data.status == 'failure_message') {
		alert(data.message);
	} else {
		// show error message
		alert('Please correct the following errors:\n' + IncorrectInfo.renderValidationErrors(data.errors, '-').replace(/@/ig,''));
	}
	return false;
}

IncorrectInfo.renderValidationErrors = function(err_obj, start_char) {
	var e = '';
	var nl = '';
	for(var i in err_obj) {
		for(var j in err_obj[i]) {
			e += nl + start_char + ' ' + err_obj[i][j];
			nl = "\n";
		}
	}
	return e;
}

IncorrectInfo.loadScript = function(src_url) {
   var e = document.createElement("script");
   e.src = src_url;
   e.type = "text/javascript";
   document.getElementsByTagName("head")[0].appendChild(e);
}

$(document).ready(function() {
	IncorrectInfo.initialize(); 
});
