
function submit_login(evt) {
	if($('#email').val() == '' || $('#password').val() == ''){
		return false;
	}
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode :
        ((evt.which) ? evt.which : evt.keyCode);
    if (charCode == 13){
		login();  
    }
}

function features_all(){
	$.cookie('temp', "features", { expires: 1, path: '/', secure: false });
	window.location = base+"index.php/pages_c/services/";	
}

function view_rates(){
	$.cookie('temp', "dialin_rates", { expires: 1, path: '/', secure: false });
	window.location = base+"index.php/pages_c/services/";
}

function view_free_dests(){
	$.cookie('temp', "free_dests", { expires: 1, path: '/', secure: false });
	window.location = base+"index.php/pages_c/services/";
}

function login(){
	var rem = 0;
	if($('#remember_me').is(':checked')){
		rem = 1;
	}
	show_msgbox('logging you in...');
	ptBase.login($('#email').val(),$('#password').val(), rem, function(data){
		if (!data.result){
			$("#msgbox").dialog('close');
			show_error(data.msg);
		} else {
			ptBase.get_user(function(data){
				if (rem==1){
					expires=30;
				} else {
					expires=0;
				}
				
				var fullname = data.result.fullname;
				var phone1   = data.result.phone1;
				var gender   = data.result.gender;
					
				if ((!fullname) || (fullname=="")) {
					fullname = "";
				}
				
				$.cookie('balance', ""+data.result.balance, { expires: expires, path: '/', secure: false });
				$.cookie('phone1', ""+phone1, { expires: expires, path: '/', secure: false });
				$.cookie('phone2', ""+data.result.e164_2, { expires: expires, path: '/', secure: false });
				$.cookie('country', ""+data.result.country, { expires: expires, path: '/', secure: false });
				//$.cookie('city', ""+data.result.city, { expires: expires, path: '/', secure: false });
				//$.cookie('postal_code', ""+data.result.postal_code, { expires: expires, path: '/', secure: false });
				$.cookie('register_date', ""+data.result.register_date, { expires: expires, path: '/', secure: false });
				$.cookie('fullname', ""+fullname, { expires: expires, path: '/', secure: false });
				$.cookie('gender', ""+gender, { expires: expires, path: '/', secure: false });
				$.cookie('occupation', ""+data.result.occupation, { expires: expires, path: '/', secure: false });
				$.cookie('birthdate', ""+data.result.birthdate, { expires: expires, path: '/', secure: false });
				$.cookie('wizard_step', "", { expires: expires, path: '/', secure: false });
				$.cookie('last_dialed', "", { expires: expires, path: '/', secure: false });
				$.cookie('tmp_splash', "1", { expires: expires, path: '/', secure: false });
				$.cookie('tmp_services_count', "", { expires: expires, path: '/', secure: false });		
				
				$.cookie('promo_pop', "true", { expires: expires, path: '/', secure: false });
						
				window.location = base+"index.php/pages_c/main/";
			});
		} 
	});
}


function show_forgot_pass(){
	$('#forgot_pass').dialog("open");
}

function send_forgot_pass(){
	var email = $('#forgot_pass_email').val();
	if (email == "") {
		show_msgbox("please enter a valid email");
		return;
	}
	
	ptBase.forgot_pass_send(email,function(data){
		$('#forgot_pass').dialog("close");
		
		if (!data.result){
			show_error(data.msg);				
		} else {
			show_msgbox(data.msg);	
		}
		return;
	});	
}

function set_new_pass(){
	var new_pass = $('#fp_pass').val();
	if (new_pass != $('#fp_pass_conf').val()){
		show_error('Your passwords do not match');
		return;
	}
	var fp_key = $('#fp_key').val();
	$('#new_pass').dialog("close");			
	ptBase.set_new_pass(new_pass,fp_key,function(data){		
		if (!data.result){
			show_error(data.msg);				
		} else {
			show_msgbox(data.msg);	
		}
		return;
	});	
}

function stripslashes(str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\\\/g,'\\');
	str=str.replace(/\\0/g,'\0');
	return str;
}

function populate_trial_countries(prefix){
	var html = "";
	if(countries_list){
		var len = countries_list.length;
		for(var i=0; i<len; i++){
			var item =  countries_list[i];
			var selected = "";
			if(prefix && prefix == item.prefix){
				selected = "selected";
				$("#nb_prefix").val(item.prefix);
			}
			if((prefix == null || prefix == '') && i==0){
				selected = "selected";
				$("#nb_prefix").val(item.prefix);
			}
			html += "<option value='"+item.prefix+"' "+selected+">"+item.countryname+"</option>";
			
		}
	}else{
		html += "<option value='0'>Empty list</option>";
	}
	//$("#nb_country").html(html);
	$("<option value=''></option>").appendTo($("#nb_country"));
	$("<option value=''>---- Pro Destinations ---</option>").appendTo($("#nb_country"));
	$(html).appendTo($("#nb_country"));
	$("#nb_country").bind('change', function(){
		$("#nb_prefix").val($(this).val());
	});
	$("#nb_prefix").bind('keyup', function(){
		var countries = $("#nb_country").children("option");
		var prefix = $(this).val();
		var len = countries.length;
		for(var i=0; i<len; i++){
			if (countries[i].value == prefix) {
				countries[i].selected = true;
				return;
			};
		}
	});
}


function do_trial_call(){
	var last_dialed = $('#nb_prefix').val()+$('#nb_number').val();
	$.cookie('last_dialed', ""+last_dialed, { expires: 1, path: '/', secure: false });

	var temp_api = $.cookie('temp_api');
	$.cookie('temp_api', "", { expires: 1, path: '/', secure: false });
	window.location = base+"index.php/pages_c/signup/1/"+temp_api;
}

	
function change_slider(param){

	var pos = $('#film').offset();
	var container_pos = $('#film_container').offset();
	
	container_pos = container_pos.left;
	

	
	if (param == 'next'){
		if (feature_index>=4){
			return;
		}
		$('#film').animate({left:(pos.left-400-container_pos),top:0});
		feature_index++;
	}
	if (param == 'back'){
		if (feature_index<=1){
			return;
		}
		$('#film').animate({left:(pos.left+400-container_pos),top:0});
		feature_index--;
	}	
}


function get_hp_counter(){
//	ptBase.get_homepage_counter(function(data){
//		if (data.result) {
//			$('#callers_counter').html(data.result);
//			
//		}
//	});
	
//	var delay = function() { get_hp_counter(); };
//	setTimeout(delay,13000);
}




$(document).ready(function(){
	
	
	ptBase.check_login(function(data){
		if (data.result) {
			window.location = base+"index.php/pages_c/main/";
		} else {
			$.cookie('balance', null); // delete cookie
			$.cookie('fullname', null); // delete cookie
		}
	});



	$("#forgot_pass").dialog({
		modal: false,
		autoOpen: false,
		closeOnEscape: true,
		buttons: {
		"send": function(){
				send_forgot_pass();
			},
		"close": function(){
				$(this).dialog("close");
			}
		}
	});

	$("#new_pass").dialog({
		modal: false,
		autoOpen: false,
		closeOnEscape: true,
		buttons: {
		"save": function(){
				set_new_pass();
			},
		"close": function(){
				$(this).dialog("close");
			}
		}
	});	
	
	if (fp_key != 0){
		$('#new_pass').dialog("open");
		return;
	}

	//populate_trial_countries(from_country);
	//$.cookie('home_feature', ""+1, { expires: 1, path: '/', secure: false });
	//rotate_feature(1); //load the first feature into the slider
	
	//	$("#amount").val('$' + $("#slider").slider("value"));

	var testimonial_var = Math.floor(Math.random()*4);
	if (testimonial_var ==0){
		$('#testimonial_text').html('"My calling from Norway to Moscow used to be so expensive until I discovered your amazing service. Not sure if this is voip or not, but calls are pefect quality. Prekrasnaya sistema!!"');
		$('#testimonial_author').html('Ivan Kuznetzov');
	}
	if (testimonial_var ==1){
		$('#testimonial_text').html('"There are hundreds of calling cards, Talk India, Hyderabad special… for India but they all charge more than is really stated.  I start using PokeTALK and they have great rates to India less than 2 cents with no hidden fees. I pay exactly what is stated."');
		$('#testimonial_author').html('Abhishek Patel');
	}
	if (testimonial_var ==2){
		$('#testimonial_text').html('"I was travelling in the mountain reagion of central Turkey in the middle of nowhere and needed to call my friend. no internet, no credit in my phone, and i used the SMS-Callback. it didnt work the 1st time but its cause i dialled wrongly! then i got connected and WA-LA! it worked~!"');
		$('#testimonial_author').html('Larry L. Schild');
	}
	if (testimonial_var >=3){
		$('#testimonial_text').html('"As a global manager calling from New York I get bombarded with offers all the time for international calls. Along comes PokeTALK from a friends recommendation and I am stunned. I use their sim card and web interface for reliable and best of all cheap calls."');
		$('#testimonial_author').html('Dar Sandler');
	}
	
	
	get_hp_counter();	
});

