var SITE_URL = 'http://www.smartkidszone.com/';

function relocate(url) {
  window.location = url;
}

function checkEmail(email) {
  var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if ( !filter.test(email) ) {
    return false;
  } else {
    return true;
  }
}

function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires=" + date.toGMTString();
	} else {
    var expires = "";
  }

  document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');

	for(var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') {
      c = c.substring(1,c.length);
    }

		if (c.indexOf(nameEQ) == 0) {
      return c.substring(nameEQ.length,c.length);
    }
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name, "", -1);
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function showMessage( msg ) {
  $("#inline").html(msg);
  $("#alerts").fancybox({centerOnScroll:true});
  $("#alerts").trigger('click');
}

function set_language( language_id ) {
  var ok = $.ajax({
   type: "POST",
   url: "includes/ajax/set_language.php",
   data: "language_id=" + language_id,
   async: false,
   success: function(msg){
   }
 }).responseText;

 if ( ok == 'changed' ) {
   window.location = window.location;
 }
}

function get_problem() {
  var ok = $.ajax({
   type: "POST",
   url: "includes/ajax/get_info.php",
   data: "type=problem&action=get",
   async: false,
   success: function(msg){
   }
 }).responseText;

 if ( ok != '' ) {
   $('#problem').html(ok);
 }
}

function send_answer( answer, next_action ) {
answer = answer.replace('+','#|#');
  var ok = $.ajax({
   type: "POST",
   url: "includes/ajax/get_info.php",
data: "type=problem&action=submit&answer=" + answer + "&next=" + next_action,
   async: false,
   success: function(msg){
   }
 }).responseText;


 if ( ok != '' ) {
   showMessage(ok);
 }
}

function submit_problem( next_action ) {
  var answer = "";
  var nr_options = $('#nr_options').val();

  if ( nr_options > 1 ) {
    for ( i = 0; i < nr_options; i++ ) {
       if ( document.getElementsByName("answer")[i].checked ) {
         answer = document.getElementsByName("answer")[i].value;
       }
    }
  } else {
    answer = $('#answer').val();
  }

 if ( answer == "" ) {
   var ok = $.ajax({
   type: "POST",
   url: "includes/ajax/get_info.php",
   data: "type=problem&action=submit&next=" + next_action + "&not_answered=",
   async: false,
   success: function(msg){
   }
 }).responseText;

   if ( ok != "" ) {
     showMessage(ok);
   }
 } else {
  send_answer( answer, next_action );
 }
}

function refresh_score( pr_attempted, score ) {
  $('#problems_attempted').html(pr_attempted);
  score = score + '';
  
  var digit1 = '';
  var digit2 = '';

  if ( score.length == 1 ) {
    digit1 = '0';
    digit2 = score;
  } else {
    digit1 = score[0];
    digit2 = score[1];
  }
  $('#digit1').html(digit1);
  $('#digit2').html(digit2);
}

function finish_test() {
  var timer = $('.countdown_amount').html();

  $('.buttons_test').html('');
  $('#score_column').hide();

  var ok = $.ajax({
   type: "POST",
   url: "includes/ajax/get_info.php",
   data: "type=problem&action=finish&timer=" + timer,
   async: false,
   success: function(msg){
   }
 }).responseText;

 $('#problem').html(ok);
}

function checkButton(){
  var period = $('#membership_type').val();
    if (val == 'yearly'){
      $('#price').html();
      $('#child').hide();
      $('#price1').show();
      $('#child1').show();
    }else{
      $('#price1').hide();
      $('#child1').hide();
      $('#price').show();
      $('#child').show();
    }
 }

function set_member_type () {
  var period = '';
  var children = $('#children').val();

  for ( i = 0; i < 2; i++ ) {
    if ( document.getElementsByName("period")[i].checked ) {
       period = document.getElementsByName("period")[i].value;
     }
  }
  
  $('#membership_type').val(period);

  var period_amount = $('#' + period + '_fee').val();
  var add_per_child = $('#' + period + '_fee_per_child').val();

  var total_amount = parseFloat(period_amount) + parseFloat(add_per_child) * (parseFloat(children) - 1);

  $('#total_amount').val(total_amount);
  $('#total_price').html(total_amount);

  if ( period == 'yearly' ) {
    $('#child').hide();
    $('#child1').show();
  } else {
    $('#child1').hide();
    $('#child').show();
  }
}

function serverTime() {
    var time = null;
    $.ajax({url: SITE_URL+'server_time.php',
        async: false, dataType: 'text',
        success: function(text) {
            time = new Date(text);
        }, error: function(http, message, exc) {
            time = new Date();
    }});
    return time;
}

function show_popup() {
  $('#buttons_test').hide();
  $('#popup_sign_up').show();
}

function switch_user() {
 var ok = $.ajax({
   type: "POST",
   url: "includes/ajax/user_account.php",
   data: "action=switch",
   async: false,
   success: function(msg){
   }
 }).responseText;

 if ( ok != '' ) {
   showMessage(ok);
 }

}

function choose_user( user_id ) {
  var ok = $.ajax({
   type: "POST",
   url: "includes/ajax/user_account.php",
   data: "action=choose&user_id=" + user_id,
   async: false,
   success: function(msg){
   }
 }).responseText;

 if ( ok == 'done' ) {
   window.location = window.location;
 }
}

function cancel_membership() {
  var r = confirm('Are you sure you want to cancel your membership?');

  if ( r == true ) {
    window.location = 'profile?action=remove_memership';
  }
}
