var Recaptcha = "";

function move_login() { // calculate new position for login window based on window size
	var margin = ($(window).width() - 800) / 2;
	if(margin <= 0) margin = 0;
	var left = margin + 800 - 265 + "px" 
	$("#login_window").css("left",left);
}

$(window).resize(function(){
  move_login();
});

function toggle_login() { // show the login window and update the arrow
	move_login();
	$("#login_window").slideToggle("fast");
	if($("#client_arrow").attr("src") == "images/down.png") $("#client_arrow").attr("src","images/up.png");
	else $("#client_arrow").attr("src","images/down.png");
}

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
	var bracket=3
	strPhone=trim(strPhone)
	if(strPhone.indexOf("+")>1) return false
	if(strPhone.indexOf("-")!=-1)bracket=bracket+1
	if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
	var brchr=strPhone.indexOf("(")
	if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
	if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

var msg_icon_error = "<img src=\"images/form_error.png\" height=\"20\" width=\"20\">";
var msg_icon_ok = "<img src=\"images/form_ok.png\"  height=\"20\" width=\"20\">";
var msg_icon_big_error = "<img src=\"images/form_error.png\"  height=\"32\" width=\"32\">";
var msg_icon_big_ok = "<img src=\"images/form_ok.png\"  height=\"32\" width=\"32\">";

function check_name_empty(field) {
	if(field == "") {
		$('#error_name').html(msg_icon_error + msg_name_error);
		return true;
	}
	else {
		$('#error_name').html(msg_icon_ok + msg_ok);
		return false;	
	}
}

function check_message_empty(field) {
	if(field == "") {
		$('#error_message').html(msg_icon_error + msg_message_error);
		return true;
	}
	else {
		$('#error_message').html(msg_icon_ok + msg_ok);
		return false;	
	}
}

function check_captcha_empty(field) {
	if(field == "") {
		$('#error_captcha').html(msg_icon_error + msg_captcha_error);
		return true;
	}
	else {
		$('#error_captcha').html(msg_icon_ok + msg_ok);
		return false;	
	}
}


function check_email(email) {
	var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
	if(!pattern.test(email)) {
		$('#error_email').html(msg_icon_error + msg_email_error);
		return false;
	}
	else {
		$('#error_email').html(msg_icon_ok + msg_ok);
		return true;
	}
}

function check_phone(phone) {
		if(checkInternationalPhone(phone) == false) {
		$('#error_phone').html(msg_icon_error + msg_phone_error);
		return false;
	}
	else {
		$('#error_phone').html(msg_icon_ok + msg_ok);
		return true;
	}
}

function check_form() {
	var form_ok = true;
	var error_msg = msg_icon_big_error + msg_form_error + "<br><br>";
	if(check_name_empty(document.forms.contact_form.fullname.value)) {
		form_ok = false;
		error_msg = error_msg + msg_name_error + "<br>";
	}
	if(!check_email(document.forms.contact_form.email.value)) {
		form_ok = false;
		error_msg = error_msg + msg_email_error + "<br>";	
	}
	if(check_phone(document.forms.contact_form.phone.value) == false) {
		form_ok = false;
		error_msg = error_msg + msg_phone_error + "<br>";
	}
	if(check_message_empty(document.forms.contact_form.message.value)) {
		form_ok = false;
		error_msg = error_msg + msg_message_error + "<br>";
	}
	if(check_captcha_empty(document.forms.contact_form.recaptcha_response_field.value)) {
		form_ok = false;
		error_msg = error_msg + msg_captcha_error + "<br>";
	}
	if(form_ok) {
		$("#msg").html(msg_sending);
		$("#action").html(msg_sending_action);
		return true;
	}
	else {
		$("#msg").html(error_msg);
		$("#action").html("<a href=\"javascript:close_msg()\" id=\"btn_dialog\" class=\"close btn\">" + msg_close + "</a>");
		return false;
	}
}
	
function send_msg() {
	$.post("mail.php", { fullname: document.forms.contact_form.fullname.value, 
							 email: document.forms.contact_form.email.value, 
							 phone: document.forms.contact_form.phone.value, 
							 message: document.forms.contact_form.message.value,
							 challenge: document.forms.contact_form.recaptcha_challenge_field.value,
                             response: document.forms.contact_form.recaptcha_response_field.value
							},
 		function(data){
 				if(data == "ok") {
 			 		$("#msg").html(msg_icon_big_ok + msg_sent);
 			 		$("#action").html("<a href=\"javascript:close_msg();window.location.href='?p=home'\" id=\"btn_dialog\" class=\"close btn\">" + msg_close_return + "</a>");
			 		return true;  
			 	}
			 	else if(data == "fail-captcha")  {
			 		$("#msg").html(msg_icon_big_error + msg_captcha_fail);
 			 		$("#action").html("<a href=\"javascript:close_msg();Recaptcha.focus_response_field();\" id=\"btn_dialog\" class=\"close btn\">" + msg_close_try_again + "</a>");
			 		Recaptcha.reload();
			 		return false; 
			 	}
			 	else if(data == "fail-email") {
			 		$("#msg").html(msg_notsent_email);
 				 	$("#action").html("<a href=\"javascript:close_msg();\" id=\"btn_dialog\" class=\"close btn\">" + msg_close + "</a>");
			 	}
			 	else { 
			 		$("#msg").html(msg_notsent);
 				 	$("#action").html("<a href=\"javascript:close_msg();\" id=\"btn_dialog\" class=\"close btn\">" + msg_close + "</a>");
			 	}
  		});
  		return false;
}

function do_login() {
	$("#login_form").submit();
}

function close_msg() {
	$('#mask, .window').hide();
	$("#msg").html("");
 	$("#action").html("");
}

$(document).ready(function(){

	$(document).pngFix(); 

	$(".project_container").hover(
	  function () {
		$(this).css("background","url(images/nav_bg.png) repeat-x #333");
	  }, 
	  function () {
		$(this).css("background","");
	  }
	);
	
	//select all the a tag with name equal to modal
	$('a[name=modal]').click(function(e) {
	
		if(check_form()) { 
		 	send_msg();
				 
		}
		
        //Cancel the link behavior
		e.preventDefault();
		//Get the A tag
		var id = $(this).attr('href');
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set height and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight,'zIndex':'998'});
		
		//transition effect		
		$('#mask').fadeIn(250);		
		
		//position dialog fixed-center, borrowed from: http://andreaslagerkvist.com/jquery/center/
		
		$(id).css({
            position:    'fixed', 
            left:        '50%', 
            top:        '50%', 
            zIndex:        '999'
        }).css({
            marginLeft:    '-' + ($(id).width() / 2) + 'px', 
            marginTop:    '-' + ($(id).height() / 2) + 'px'
        });

	/*	//Get the window height and width
		var winH = $(window).scrollTop();
		var winW = $(window).scrollLeft();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
	*/
		//transition effect
		$(id).fadeIn(250); 
	});
	


	$('input[name=recaptcha_response_field]').blur(function() {
		check_captcha_empty(this.value);
	});
 });
 
 