/* ad_management */

function ale() {
	alert('pro caraio'); 
}

function collapse_level1(id,type) {
	
	if ($('#level2_' + id).css("display") == 'none') {
		$('#loading_level1_' + id).html('<img src="images/loading-animated-circle.gif" border="0">');
		if (type == 'subscriber') {
			var str = "publisherId=" + encodeURI(id);
			var file = "get_level2_subs.php";
		} else if (type == 'publisher') {
			var str = "mag_id=" + encodeURI(id);
			var file = "get_level2.php";
		}
		
		 $.ajax({
            type: "GET",
            url: "http://"+window.location.hostname+"/php/ajax/" + file,
            data: str,
            success: function(response){
                $('#level2_' + id).show().html(response);
                $('#loading_level1_' + id).html('');
            },
            error: function(msg){        /* should not happen... */
                alert("SERVER ERROR!!! " + msg);
            }
    	});
		
	} else {
		$('#level2_' + id).hide();
		$('#level2_' + id).html('');
	}
}

function collapse_divAds(id1,id2,type) {
	
	if ($('#divAds_' + id1 + '_' + id2).css("display") == 'none') {
		$('#loading_level2_' + id1 + '_' + id2).html('<img src="images/loading-animated-circle.gif" border="0">');
		
		if (type == 'subscriber') {
			var str = "mag_id=" + encodeURI(id1) + "&publisherId=" + id2;
			/*var file = "get_level2_subs.php";*/
		} else if (type == 'publisher') {
			var str = "mag_id=" + encodeURI(id1) + "&client_id=" + id2;
			/*var file = "get_level2.php";*/
		}
		
		 $.ajax({
            type: "GET",
            url: "http://"+window.location.hostname+"/php/ajax/get_client_ads.php",
            data: str,
            success: function(response){
                $('#divAds_' + id1 + '_' + id2).show().html(response);
				//$('#btAds_' + id1).removeClass("closed");
				$('#btAds_' + id1 + '_' + id2).addClass("name opened");
				$('#loading_level2_' + id1 + '_' + id2).html('');

            },
            error: function(msg){        /* should not happen... */
                alert("SERVER ERROR!!! " + msg);
            }
    	});
		
	} else {
		$('#divAds_' + id1 + '_' + id2).hide();
		$('#divAds_' + id1 + '_' + id2).html('');
		$('#btAds_' + id1 + '_' + id2).removeClass("name opened");
		$('#btAds_' + id1 + '_' + id2).addClass("name closed");
	}
}

function expand_collapse_dom(divId) {
	if ($('#' + divId).css("display") == 'none') {
		$('#' + divId).show();
	} else {
		$('#' + divId).hide();
	}
}

function subscriber_Ads(publisherId,subscriberId,magazineId) {
	if (magazineId == 0) {
		var str = "subscriberId=" + encodeURI(subscriberId) + "&publisherId=" + publisherId;
		var div = 'divAds_publisher_' + publisherId;
		var bt = 'btAds_publisher_' + publisherId;
	} else if (publisherId == 0) {
		var str = "magazineId=" + encodeURI(magazineId) + "&subscriberId=" + subscriberId;
		var div = 'divAds_magazine_' + magazineId;
		var bt = 'btAds_magazine_' + magazineId;
	} 
	
	if ($('#' + div).css("display") == 'none') {
	
	 $.ajax({
        type: "GET",
        url: "http://"+window.location.hostname+"/php/ad_management/subscriber_list_ads.php",
        data: str,
        success: function(response){
            $('#' + div).show().html(response);
			//$('#btAds_' + publisherId).removeClass("closed");
			$('#' + bt).addClass("name opened");
			//$('#loading_level2_' + publisherId + '_' + subscriberId).html('');

        },
        error: function(msg){        /* should not happen... */
            alert("SERVER ERROR!!! " + msg);
        }
	});
		
	} else {
		$('#' + div).hide().html('');
		$('#' + bt).removeClass("name opened");
		$('#' + bt).addClass("name closed");
	}
}


function publisher_Ads(publisherId,subscriberId,magazineId) {
	if (magazineId == 0) {
		var str = "subscriberId=" + encodeURI(subscriberId) + "&publisherId=" + publisherId;
		var div = 'divAds_subscriber_' + subscriberId;
		var bt = 'btAds_subscriber_' + subscriberId;
	} else if (subscriberId == 0) {
		var str = "magazineId=" + encodeURI(magazineId) + "&publisherId=" + publisherId;
		var div = 'divAds_magazine_' + magazineId;
		var bt = 'btAds_magazine_' + magazineId;
	} 

	if ($('#' + div).css("display") == 'none') {
	
	 $.ajax({
        type: "GET",
        url: "http://"+window.location.hostname+"/php/ad_management/publisher_list_ads.php",
        data: str,
        success: function(response){
            $('#' + div).show().html(response);
			$('#' + bt).addClass("name opened");

        },
        error: function(msg){        /* should not happen... */
            alert("SERVER ERROR!!! " + msg);
        }
	});
		
	} else {
		$('#' + div).hide().html('');
		$('#' + bt).removeClass("name opened");
		$('#' + bt).addClass("name closed");
	}
}
/* End ad_management */

/* check client */
function invite_client() {
	var email = $('#email_address').val();
	var str = "email=" + encodeURI(email);
	$.ajax({
        type: "GET",
        url: "http://"+window.location.hostname+"/php/ajax/invite_client.php",
        data: str,
        success: function(response){
        	$('#check_client_response').html(response);
        },
        error: function(msg){        /* should not happen... */
            alert("SERVER ERROR!!! " + msg);
        }
	});
}
/* End check client */

function register_as_publisher() {
	$('#registration_form_1').show();
	$('#registration_form_2').show();
	$('#registration_form_tc').show();
	$('#last_step_registration_button').show();
	$('#select_accopunt_type').hide();
	$('#account_type').attr('value','1');
	$('#title_registration_form').html('<h1>&nbsp;Publisher</h1>');
}

function register_as_subscriber() {
	$('#registration_form_1').show();
	$('#registration_form_2').hide();
	$('#registration_form_tc').show();
	$('#last_step_registration_button').show();
	$('#select_accopunt_type').hide();
	$('#account_type').attr('value','0');
	$('#title_registration_form').html('<h1>&nbsp;Subscriber</h1>');
}

/* *************************************************************** create advert */
function get_magazine_publisher(magazineId) {
	var str = "magazineId=" + magazineId;
	$.ajax({
        type: "GET",
        url: "http://"+window.location.hostname+"/php/ajax/get_magazine_publisher.php",
        data: str,
        success: function(response){
        	/* Obj: magPub */
        	var JsonData = response;
        	eval(JsonData);
        	$('#magazinePublisher').html("<b>Publisher:</b>&nbsp;" + magPub.name);
        	$('#magazinePublisherId').val(magPub.publisherId);
        	show_next_button();
        	//$('#create_add_next_1').show();
        },
        error: function(msg){        /* should not happen... */
            alert("SERVER ERROR!!! " + msg);
        }
	});
}

function cancel_title_insertion() {
	$('#title_drop_down').show();
	$('#insert_title').hide();
}

function insert_new_title(creator) {
	if ($('#name_title').val() != '' && $('#descr_title').val() != '') {
		var name = $('#name_title').val();
		var descr = $('#descr_title').val();
		var str = "name=" + encodeURI(name) + "&descr=" + encodeURI(descr) + "&creator=" + creator;
		$.ajax({
	        type: "GET",
	        url: "http://"+window.location.hostname+"/php/ajax/insert_new_title.php",
	        data: str,
	        success: function(response){
	        	/* Obj: result */
	    		var JsonData = response;
	    		eval(JsonData);
	    		
	        	if (result.status == 1) {
	        		$("#title").addOption(result.id, name);
	        		$('#title_drop_down').show();
					$('#insert_title').hide();
	        	}
	        },
	        error: function(msg){        /* should not happen... */
	            alert("SERVER ERROR!!! " + msg);
	        }
		});
	}
}

function add_item_feature(session_id) {
	if ($('#features').val() != '') {
		var feature = $('#features').val();
		$('#features').val('');
		var str = "feature=" + encodeURI(feature) + "&session_id=" + session_id;
		$.ajax({
		    type: "GET",
		    url: "http://"+window.location.hostname+"/php/ajax/add_item_features.php",
		    data: str,
		    success: function(response){
		    	var JsonData = response;
	    		eval(JsonData);
		    	$('#div_list_features').append(result.link);
		    	$('#item_features').append(result.preview);
		    },
		    error: function(msg){        /* should not happen... */
		        alert("SERVER ERROR!!! " + msg);
		    }
		});
	}
}

function remove_item_feature(index) {
	var str = "action=remove&index=" + index;
	$.ajax({
	    type: "GET",
	    url: "http://"+window.location.hostname+"/php/ajax/add_item_features.php",
	    data: str,
	    success: function(response){
	    	var JsonData = response;
	    	eval(JsonData);
	    	$('#div_list_features').html(result.link);
	    	$('#item_features').html(result.preview);
	    },
	    error: function(msg){        /* should not happen... */
	        alert("SERVER ERROR!!! " + msg);
	    }
	});
}

function remove_extra_links(index) {
	var str = "action=remove&index=" + index;
	$.ajax({
	    type: "GET",
	    url: "http://"+window.location.hostname+"/php/ajax/add_extra_links.php",
	    data: str,
	    success: function(response){
	    	var JsonData = response;
	    	eval(JsonData);
	    	$('#div_list_links').html(result.form);
	    	$('#item_extra_links').html(result.preview);
	    },
	    error: function(msg){        /* should not happen... */
	        alert("SERVER ERROR!!! " + msg);
	    }
	});
}

function add_extra_links(session_id) {
	if ($('#title_link').val() != '' && $('#url_link').val() != '') {
		var title = $('#title_link').val();
		var link = $('#url_link').val();
		$('#title_link').val('');
		$('#url_link').val('');
		var str = "title=" + encodeURI(title) + "&link=" + encodeURI(link) + "&session_id=" + session_id;
		$.ajax({
		    type: "GET",
		    url: "http://"+window.location.hostname+"/php/ajax/add_extra_links.php",
		    data: str,
		    success: function(response){
		    	var JsonData = response;
	    		eval(JsonData);
		    	$('#div_list_links').append(result.form);
		    	$('#item_extra_links').append(result.preview);
		    },
		    error: function(msg){        /* should not happen... */
		        alert("SERVER ERROR!!! " + msg);
		    }
		});
	}
}

function check_credit(pubId,subId) {
	var str = "pubId=" + pubId + "&subId=" + subId;
	$.ajax({
	    type: "GET",
	    url: "http://"+window.location.hostname+"/php/ajax/check_credit.php",
	    data: str,
	    success: function(response){
	    	if (response == '0') {
	    		alert('Not enough credits');
	    	} else {
	    		if (subId == 0) { $('#title').removeAttr('disabled'); }
	    	}
	    },
	    error: function(msg){        /* should not happen... */
	        alert("SERVER ERROR!!! " + msg);
	    }
	});
}

function show_next_button() {
	$('#create_add_next_1').show();
}

function title_selection_check(pubId,subId) {
	$('#pmagazines').attr('disabled','1');
	$('#magazinePublisher').html("");
	check_credit(pubId,subId);
}

function submit_adv_form(step) {
	if (step == 2) {
		if ($('#prod_name').val() == '') {
			alert('Name is required!');
		} else if ($('#start-date').val() == '') {
			alert('Start date required!');
		} else if ($('#end-date').val() == '') {
			alert('End date required!');
		} else {
			document.frm_adv.submit();
		}
	} else if (step == 3) {
		
	}
}
/* End create advert */

/* events */
$(function(){
	//search
	$('#searchByMag').click(function() {
		$('#date_text').attr('disabled','true');
	});
	
	$('#searchByPub').click(function() {
		$('#date_text').attr('disabled','true');
	});
	
	$('#searchByAdv').click(function() {
		$('#date_text').removeAttr('disabled');
	});
	
	/*$('#title_selection').click(function() {
		title_selection();
	});*/
	
	$('#title_selection_p').click(function() {
		$('#title').attr('disabled','1');
		$('#pmagazines').removeAttr('disabled');
	});
	
	$('#add_new_title').click(function() {
		$('#title_drop_down').hide();
		$('#insert_title').show();
	});
	
	$('#titleAdvert').blur(function() {
		$('#item_titleAdvert').html($('#titleAdvert').val());
	});
	
	$('#description').blur(function() {
		$('#item_description').html($('#description').val());
	});
	
	$('#aboutAdvertiser').blur(function() {
		$('#item_about').html(
			$('#aboutAdvertiser').val() + "<br><br>"
		);
	});
	
	$('#company').blur(function() {
		$('#item_about').html(
			$('#aboutAdvertiser').val() + "<br><br>" +
			$('#company').val() + "<br>"
		);
	});
	
	$('#addr1').blur(function() {
		$('#item_about').html(
			$('#aboutAdvertiser').val() + "<br><br>" +
			$('#company').val() + "<br>" +
			$('#addr1').val() + "<br>"
		);
	});
	
	$('#addr2').blur(function() {
		$('#item_about').html(
			$('#aboutAdvertiser').val() + "<br><br>" +
			$('#company').val() + "<br>" +
			$('#addr1').val() + "<br>" +
			$('#addr2').val() + "<br>"
		);
	});
	
	$('#city').blur(function() {
		$('#item_about').html(
			$('#aboutAdvertiser').val() + "<br><br>" +
			$('#company').val() + "<br>" +
			$('#addr1').val() + "<br>" +
			$('#addr2').val() + "<br>" +
			$('#city').val() + "<br>"
		);
	});
	
	$('#zip_code').blur(function() {
		$('#item_about').html(
			$('#aboutAdvertiser').val() + "<br><br>" +
			$('#company').val() + "<br>" +
			$('#addr1').val() + "<br>" +
			$('#addr2').val() + "<br>" +
			$('#city').val() + "<br>" +
			$('#zip_code').val() + "<br>"
		);
	});
	
	$('#region').blur(function() {
		$('#item_about').html(
			$('#aboutAdvertiser').val() + "<br><br>" +
			$('#company').val() + "<br>" +
			$('#addr1').val() + "<br>" +
			$('#addr2').val() + "<br>" +
			$('#city').val() + "<br>" +
			$('#zip_code').val() + "<br>" +
			$('#region').val() + "<br>"
		);
	});
	
	$('#tel').blur(function() {
		$('#item_about').html(
			$('#aboutAdvertiser').val() + "<br><br>" +
			$('#company').val() + "<br>" +
			$('#addr1').val() + "<br>" +
			$('#addr2').val() + "<br>" +
			$('#city').val() + "<br>" +
			$('#zip_code').val() + "<br>" +
			$('#region').val() + "<br>" +
			$('#tel').val() + "<br>"
		);
	});
	
});

/* Wait for document to be ready */
/*$(document).ready(function() {
  
});*/


/* Normal functions */
function submit_passwd_form() {
	if ($('#gen_email').val() == '') {
		alert('Please, type your email.');
	} else if ($('#main_opasw').val() == '') {
		alert('Please, type your actual password.');
	} else if ($('#main_pasw').val() == '') {
		alert('Please, type your new password.');
	} else if ($('#main_pasw_conf').val() == '') {
		alert('Please, retype your new password.');
	} else {
		document.passwd_form.submit();
	}
}

function openwin_noscroll(w,h,url){
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	win = window.open(url,'search','height='+h+', width='+w+', scrollbars=no, status=no, left='+winl+', top='+wint);
}
