$(function(){
    
	var paths = window.location.pathname.split('/');
	var country=paths[1];
	if(country==''||country==undefined) country='tw' ; 
	var phone = paths[2];
	var report = paths[3];

	var href = $("#go_home").attr('href');
	href+="?t="+String(new Date().getTime());
	$("#go_home").attr('href',href);

	$("section").eq(0).find('.card').eq(0).find("button").click(function(){
		event.preventDefault(); 
		var text = $(this).text();
		$("#whois").val(text); 
		$("main").find('button[type="submit"]').click();
	})
 
	
	var btn_submit = $("main").find('button');
	$("#whois").keyup(function(){
		
		var whois = $("#whois").val();
		if(validateWhois(whois)){

			btn_submit.removeClass('btn-success');
			btn_submit.addClass('btn-primary');

		} else {
		
			btn_submit.removeClass('btn-primary');
			btn_submit.addClass('btn-success');
		
		}

		console.log(phone)
	})

	

	$("main").find('button[type="submit"]')
		.click(function(){
			event.preventDefault(); 
			var whois = $("#whois").val();

			var stringRegx = /[~@\#$%<>^&*\()\=+_\']/gi; 
			whois = whois.replace(stringRegx, '');

			if(validateWhois(whois)){
				$("main").find('button').hide();
				$("main").find('button').parent().append('Your report have been submitted. Thanks.');
				$.post('/js/ajax_set.php',{country:country,phone:phone,whois:whois},function(ret){  
					reflash_report_data();
				},'json')
			} else {
				location.href='./'
			}
			
			return false;
		})

	$(".btn-remove-phone")
		.click(function(){
			event.preventDefault(); 
			var phone = $(this).attr('data-phone');
			if(phone>''){
				$.post('/js/ajax_remove_phone.php',{country:country,phone:phone},function(ret){  
					location.reload();
				},'json')
			} 			
			return false;
		})

	$(".btn-remove-report")
		.click(function(){
			event.preventDefault(); 
			var phone = $(this).attr('data-phone');
			var s_time = $(this).attr('data-time'); 
			if(phone>''){
				$.post('/js/ajax_remove_report.php',{country:country,phone:phone,s_time:s_time},function(ret){  
					location.reload();
				},'json')
			} 			
			return false;
		})

});

function reflash_report_data(){
	var paths = window.location.pathname.split('/');
	var country=paths[1];
	var phone = paths[2]; 
	if(country>'' && phone>''){
		s_count = 1  + parseInt($(".s_count").text());
		$(".s_count").text(s_count);
		alert($('#thanks_word').text());
		location.reload();
	}
}
 
 
function validateWhois(whois){  
	return whois.length>0; 
}

