var captureURL;
var captureCountry;
var captureState;
var captureState_asd;
var captureCounty;
var captureRanking;
var captureGeneralPromotion;
var captureMaxPromotion;

function initSystem() {

	captureURL = "";
	
	if ( viewPromotion != "" )
		eval(viewPromotion + "()");
		
	else {
	
		$("#telaCadastro").show();
		$("#telaCadastroAmigo").show();
		$("#viewRanking").show();

		$("#fullName").focus();

		getCaptcha();
		listCountry();
		getGeneralPromotion(true);
		getMaxPromotion();
		getRanking();
		
		if ( parseInt(captureGeneralPromotion) >= parseInt(captureMaxPromotion) ) {
			$("#telaCadastro").hide();
			$("#telaCadastroAmigo").hide();
			$("#viewRanking").hide();
			$("#telaEncerramento").show();
		}
	
		$("#sendPromotion").click(function() {
			if ( verifyDataOk() )
				sendPromotion();
		});
	
		$("#zipCode").blur(function() {
			getZipCode();
		});
	
		$("#country").change(function() {
			listState();
			$("#county").removeOption(/./);
		});
		
		$("#country_asd").change(function() {
			listState_asd();
			$("#county_asd").removeOption(/./);
		});
	
		$("#state").change(function() {
			listCounty();
		});
		
		$("#state_asd").change(function() {
			listCounty_asd();
		});
	
		$("#email").blur(function() {
			getTotalPromotion();
		});
		
		$("#getAjax1").click(function() {
			$.prompt($("#JanelaAjax1").html());
		});
		
		$("#getAjax2").click(function() {
			$.prompt($("#JanelaAjax2").html());
		});

	}

}

function listCountry() {

	$("#country").addOption("", "Carregando países...", false);

	captureCountry	= new Array();
	$.ajax({
		global: false,
		datatype: "xml",
		type: "GET",
		cache: false,
		url: captureURL + "/lib/getPromotionData.php?do=listCountry",
		async: true,
		charset: "iso-8859-1",
		success: function(xml) {
			$(xml).find("country").each(function(){
				captureCountry.push( { id: $(this).attr('id'), name: $(this).attr('name') } );
			});
			$("#country").removeOption(/./);
			$("#country_asd").removeOption(/./);
			for (var i=0; i < captureCountry.length; i++) {
				$("#country").addOption(captureCountry[i].id, captureCountry[i].name, false);
				$("#country_asd").addOption(captureCountry[i].id, captureCountry[i].name, false);
			}
			$("#country").selectOptions( "1", true );
			$("#country_asd").selectOptions( "1", true );
			listState_asd();
		}
	});

}

function listState() {

	if ( $("#country").val() != "1" ) {

		captureState = new Array();
		$("#state").removeOption(/./);
		$("#state").addOption(" ", "Carregando estados...", false);
		$.ajax({
			global: false,
			datatype: "xml",
			type: "GET",
			cache: false,
			url: captureURL + "/lib/getPromotionData.php?do=listState&country=" + $("#country").val(),
			async: true,
			charset: "iso-8859-1",
			success: function(xml) {
				$(xml).find("state").each(function(){
					captureState.push( { id: $(this).attr('id'), name: $(this).attr('name') } );
				});
				$("#state").removeOption(/./);
				$("#state").addOption(" ", "- Selecione -", false);
				for (var i=0; i < captureState.length; i++)
					$("#state").addOption(captureState[i].id, captureState[i].name, false);
			}
		});

	}
	$("#zipCode").focus();

}

function listState_asd() {

	captureState_asd = new Array();

	$.ajax({
		global: false,
		datatype: "xml",
		type: "GET",
		cache: false,
		url: captureURL + "/lib/getPromotionData.php?do=listState&country=" + $("#country_asd").val(),
		async: true,
		charset: "iso-8859-1",
		beforeSend: function(x){
			$("#solicitandoInformacoes").show();
		},
		success: function(xml) {
			$(xml).find("state").each(function(){
				captureState_asd.push( { id: $(this).attr('id'), name: $(this).attr('name') } );
			});
			$("#state_asd").removeOption(/./);
			$("#state_asd").addOption(" ", "- Estado -", false);
			for (var i=0; i < captureState_asd.length; i++)
				$("#state_asd").addOption(captureState_asd[i].id, captureState_asd[i].name, false);
			$("#solicitandoInformacoes").hide();
		}
	});

}

function listCounty() {

	captureCounty = new Array();
	$("#county").removeOption(/./);				
	$("#county").addOption(" ", "Carregando cidades...", false);
	$.ajax({
		global: false,
		datatype: "xml",
		type: "GET",
		cache: false,
		url: captureURL + "/lib/getPromotionData.php?do=listCounty&country=" + $("#country").val() + "&state=" + $("#state").val(),
		async: true,
		charset: "iso-8859-1",
		success: function(xml) {
			$(xml).find("county").each(function(){
				captureCounty.push( { id: $(this).attr('ic'), name: $(this).attr('name') } );
			});
			$("#county").removeOption(/./);
			$("#county").addOption(" ", "- Selecione -", false);
			for (var i=0; i < captureCounty.length; i++)
				$("#county").addOption(captureCounty[i].name, captureCounty[i].name, false);
		}
	});

}

function listCounty_asd() {

	var objCounty = new Array();

	$.ajax({
		global: false,
		datatype: "xml",
		type: "GET",
		cache: false,
		url: captureURL + "/lib/getPromotionData.php?do=listCounty&country=" + $("#country_asd").val() + "&state=" + $("#state_asd").val(),
		async: true,
		charset: "iso-8859-1",
		beforeSend: function(x){
			$("#solicitandoInformacoes").show();
		},
		success: function(xml) {
			$(xml).find("county").each(function(){
				objCounty.push( { id: $(this).attr('ic'), name: $(this).attr('name') } );
			});
			$("#county_asd").removeOption(/./);
			$("#county_asd").addOption(" ", "- Cidade -", false);
			for (var i=0; i < objCounty.length; i++)
				$("#county_asd").addOption(objCounty[i].name, objCounty[i].name, false);
			$("#solicitandoInformacoes").hide();
		}
	});

}

function getZipCode() {

	$("#zipCode").val($("#zipCode").val().replace(/\-/g, ""));

	if ( $("#zipCode").val() != "" && $("#country").val() == "1" ) {
		$("#carregandoEndereco").show();
		$.ajax({
			global: false,
			datatype: "xml",
			type: "GET",
			cache: false,
			url: captureURL + "/lib/getPromotionData.php?do=getZipCode&zipCode=" + $("#zipCode").val(),
			async: true,
			charset: "iso-8859-1",
			success: function(xml) {
				$(xml).find("cep").each(function(){
					$("#street").val($(this).attr('street'));
					$("#block").val($(this).attr('block'));
					$("#county").removeOption(/./);
					$("#county").addOption($(this).attr('county'), $(this).attr('county'), false);
					$("#state").removeOption(/./);
					$("#state").addOption($(this).attr('state'), $(this).attr('state'), false);
					$("#number").focus();
					$("#carregandoEndereco").hide();
				});
			}
		});
	}

}

function sendPromotion() {

	var name		= $("#fullName").val().replace(/\"/g, "\"");
	var email		= $("#email").val().replace(/\"/g, "\"");
	var county_asd	= $("#county_asd").val();
	var state_asd;
	var country_asd;
	
	var friend		= $("#friendName").val().replace(/\"/g, "\"");
	var zipCode		= $("#zipCode").val().replace(/\"/g, "\"");
	var state		= $("#state").val();
	var county		= $("#county").val();
	var street		= $("#street").val().replace(/\"/g, "\"");
	var number		= $("#number").val().replace(/\"/g, "\"");
	var complement	= $("#complement").val().replace(/\"/g, "\"");
	var block		= $("#block").val().replace(/\"/g, "\"");
	var codeCaptcha	= $("#codeCaptcha").val().replace(/\"/g, "\"");
	var country;
	
	for (var i=0; i < captureCountry.length; i++) {
		if ( $("#country").val() == captureCountry[i].id )
			country = captureCountry[i].name;
		if ( $("#country_asd").val() == captureCountry[i].id )
			country_asd = captureCountry[i].name;
	}
	
	if ( $("#country").val() != "1" ) {
		for (var i=0; i < captureState.length; i++) {
			if ( $("#state").val() == captureState[i].id )
				state = captureState[i].name;
		}
	}
	
	for (var i=0; i < captureState_asd.length; i++) {
		if ( $("#state_asd").val() == captureState_asd[i].id )
			state_asd = captureState_asd[i].name;
	}

	var URL			= captureURL + "/lib/getPromotionData.php?do=sendPromotion";
	var parameters	= "fullName=" + name + "&email=" + email + "&country_asd=" + country_asd + "&state_asd=" + state_asd + "&county_asd=" + county_asd + "&friend=" + friend + "&country=" + country + "&zipCode=" + zipCode + "&state=" + state + "&county=" + county + "&street=" + street + "&number=" + number + "&complement=" + complement + "&block=" + block + "&codeCaptcha=" + codeCaptcha;
	
	$.ajax({
		type: "POST",
		url: URL,
		data: parameters,
		beforeSend: function(x){
			$("#sendPromotion").hide();
			$("#enviandoSolicitacao").show();
		},
		success: function(msg){
			var arrayMsg = msg.split("#");
			$("#enviandoSolicitacao").hide();
			$("#sendPromotion").show();
			getTotalPromotion();
			getGeneralPromotion(true);
			getCaptcha();
			if ( arrayMsg[0] == "1" ) clearFriend();
			alert( arrayMsg[1] );
		}
	});

}

function getTotalPromotion() {

	var URL			= captureURL + "/lib/getPromotionData.php?do=getTotalPromotion";
	var parameters	= "email=" + $("#email").val().replace(/\"/g, "\"");
	
	$.ajax({
		type: "POST",
		url: URL,
		data: parameters,
		beforeSend: function(x){
			$("#solicitandoInformacoes").show();
		},
		success: function(msg){
			$("#solicitandoInformacoes").hide();
			$("#totalPromotion").html(msg);
		}
	});

}

function getGeneralPromotion( viewLabel ) {

	var URL	= captureURL + "/lib/getPromotionData.php?do=getGeneralPromotion";
	
	$.ajax({
		type: "POST",
		url: URL,
		async: false,
		beforeSend: function(x){
			$("#solicitandoInformacoes").show();
		},
		success: function(msg){
			captureGeneralPromotion = msg;
			
			$("#solicitandoInformacoes").hide();
			$("#labelGeneralPromotion").html(msg);
			$("#totalGeneralPromotion").html(msg);
			if ( viewLabel )
				$("#generalPromotion").show();
			else
				$("#totalGeneralPromotion").show();
				
		}
	});

}

function getMaxPromotion() {

	var URL	= captureURL + "/lib/getPromotionData.php?do=getMaxPromotion";
	
	$.ajax({
		type: "POST",
		url: URL,
		async: false,
		beforeSend: function(x){
			$("#solicitandoInformacoes").show();
		},
		success: function(msg){
			$("#solicitandoInformacoes").hide();
			captureMaxPromotion = msg;
		}
	});

}

function getRanking() {

	captureRanking	= new Array();
	$.ajax({
		global: false,
		datatype: "xml",
		type: "GET",
		cache: false,
		url: captureURL + "/lib/getPromotionData.php?do=getRanking&top=" + topRanking,
		async: true,
		charset: "iso-8859-1",
		beforeSend: function(x){
			$("#solicitandoInformacoes").show();
		},
		success: function(xml) {
			$(xml).find("ranking").each(function(){
				captureRanking.push( { id: $(this).attr('id'), name: $(this).attr('name'), top: $(this).attr('top') } );
			});
			$("#telaRanking").show();
			$("#ranking").removeOption(/./);
			for (var i=0; i < captureRanking.length; i++)
				$("#ranking").append("<tr><td class='cel_indique' id='linha'>" + captureRanking[i].top + "</td><td class='celname'>" + captureRanking[i].name + "</td></tr>");
			$("tr:even").css('background', '#E5EAF5');
			$("#solicitandoInformacoes").hide();
		}
	});

}

function clearFriend() {

	$("#friendName").val("");
	$("#zipCode").val("");
	$("#street").val("");
	$("#number").val("");
	$("#complement").val("");
	$("#block").val("");
	$("#codeCaptcha").val("");
	$("#country").selectOptions( "1", true );
	$("#state").removeOption(/./);
	$("#county").removeOption(/./);

}

function verifyDataOk() {

	if ( !$.trim($("#fullName").val()) ) {
		alert("Favor informar seu Nome Completo");
		$("#fullName").focus();
		return false;
	}
			
	var splitName = $("#fullName").val().split(" ");
	if ( splitName.length <= 1 ) {
		alert("Favor informar seu Nome e Sobrenome");
		$("#fullName").focus();
		return false;
	}
	
	if ( !$.trim($("#email").val()) ) {
		alert("Favor informar seu E-mail");
		$("#email").focus();
		return false;
	}
	
	if ( !isEmail($("#email").val()) ) {
		alert("Favor informar seu E-mail corretamente");
		$("#email").focus();
		return false;
	}
	
	if ( !$.trim($("#country_asd").val()) ) {
		alert("Favor informar o seu País");
		$("#country_asd").focus();
		return false;
	}
	
	if ( !$.trim($("#state_asd").val()) ) {
		alert("Favor informar o seu Estado");
		$("#state_asd").focus();
		return false;
	}
	
	if ( !$.trim($("#county_asd").val()) ) {
		alert("Favor informar a sua Cidade");
		$("#county_asd").focus();
		return false;
	}
	
	if ( !$("#friendName").val() ) {
		alert("Favor informar o Nome Completo de seu amigo");
		$("#friendName").focus();
		return false;
	}
	
	var splitName = $("#friendName").val().split(" ");
	if ( splitName.length <= 1 ) {
		alert("Favor informar o Nome e Sobrenome do seu amigo");
		$("#friendName").focus();
		return false;
	}
	
	if ( !$("#country").val() ) {
		alert("Favor informar o país do seu amigo");
		$("#country").focus();
		return false;
	}
	
	if ( $("#country").val() == "1" && !$("#zipCode").val() ) {
		alert("Favor informar o CEP do seu amigo");
		$("#zipCode").focus();
		return false;
	}
	
	if ( $("#country").val() != "1" && $("#state").val() == " " ) {
		alert("Favor informar o Estado do seu amigo");
		$("#state").focus();
		return false;
	}
	
	if ( $("#country").val() != "1" && $("#county").val() == " " ) {
		alert("Favor informar a Cidade do seu amigo");
		$("#county").focus();
		return false;
	}
	
	if ( !$("#street").val() ) {
		alert("Favor informar a Rua do seu amigo");
		$("#street").focus();
		return false;
	}
	
	if ( !$("#number").val() ) {
		alert("Favor informar o Número da rua do seu amigo");
		$("#number").focus();
		return false;
	}
	
	return true;

}

function isEmail(emailStr) {

    var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
    if ( filter.test(emailStr) )
		return true;
	else
		return false;    
	
}

function sendEmailBibleSchool() {

	captureRanking	= new Array();
	$.ajax({
		global: false,
		datatype: "xml",
		type: "POST",
		cache: false,
		url: captureURL + "/lib/getPromotionData.php?do=sendEmailBibleSchool&start=" + startId + "&finish=" + finishId,
		async: true,
		charset: "iso-8859-1",
		success: function(msg){
			alert(msg);
		}
	});

}

function getCaptcha() {
	$("#imageCaptcha").attr("src", "lib/Captcha.php?rand=" + Math.random());
}

