// JavaScript Document

var ts = "";

$(document).ready(function(){
	$("a").attr({ 
	  tabindex: "-1"
	});
});

window.onload = function(){
	$.get("token.asp",function(txt){
	  ts = '<input type="hidden" name="ts" value="'+txt+'" />'
	  $("#registerForm").append(ts);
	});	
	initRegisterForm();
	// store form html in var for later use
	formHtml = $('#registerformHolder').html();
	//registration modal:
	if(window.location.search == "?reg"){
		setTimeout('initPopup()', 1000);
	}else{
		setTimeout('initPopup()', 31000);	
	}
}

function initRegisterForm(theform){

	$('#countrydropdown').change(function() {
		
		if (this.value == ""){
			$('#countrydropdown_label').html("-- Select --");
		}else{
			$('#countrydropdown_label').html(this.value);
		$("#countrydropdown_container").removeClass('selecterror');
		}
	
	});
	$('#countrydropdown_container').mouseover(function() {
		$('#countrydropdown_container').addClass('dropdown_over');
	
	});
	
	$('#countrydropdown_container').mouseout(function() {
		$('#countrydropdown_container').removeClass('dropdown_over');
	
	});


	$('#industrydropdown').change(function() {
		
		if (this.value == ""){
			$('#industrydropdown_label').html("-- Select --");
		}else{
			$('#industrydropdown_label').html(this.value);
		$("#industrydropdown_container").removeClass('selecterror');
		}
	
	});
	$('#industrydropdown_container').mouseover(function() {
		$('#industrydropdown_container').addClass('dropdown_over');
	
	});
	
	$('#industrydropdown_container').mouseout(function() {
		$('#industrydropdown_container').removeClass('dropdown_over');
	
	});






	// override default error messages
	//jQuery.validator.messages.required = "";
	$("#registerForm").validate({
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
	
			if (errors) {
				var message = errors == 1
					? 'You missed 1 field. It has been highlighted below'
					: 'You missed ' + errors + ' fields.  They have been highlighted below';
				$("div.errorWrap h4").html(message);
				$("div.errorWrap").show();
			} else {
				$("div.errorWrap").hide();
			}
		},
		focusInvalid: false,
		errorPlacement: function(error, element) {
		
		 if (element.attr("name") == "email"){
				error.insertAfter(element);
			}else if (element.attr("name") == "country"){
				$("#countrydropdown_container").addClass('selecterror');
		 	}else if (element.attr("name") == "industry"){
				$("#industrydropdown_container").addClass('selecterror');
			}
		 },

		messages: {
			email: {
				required: "Please enter a valid email address",
				email: "Please enter a valid email address",
				remote: jQuery.validator.format("{0} is already taken, please enter a different address.")	
			}
		},

		submitHandler: function() {
			$("div.errorWrap").hide();
			sendFormdata();
			return false
		}
	});
}


function initPopup(){
		
		// use same form in popup
		$('.formHolderPopup').html(formHtml);
		$(".formHolderPopup #registerForm").append(ts);
		$('#registerformHolder').html("");
		initRegisterForm();
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
}


function sendFormdata(){

	var formdata = $("#registerForm").serialize();
		//alert (formdata);
			$.ajax({   
			type: "POST",   
			url: "form_respond.asp",   
			data: formdata, 
			success: formSuccess,
	   		error: formError  
			
			});   


}

function restoreForm(){
		$('#registerformHolder').html(formHtml);
		$("#registerForm").append(ts);
		//document.registerForm.reset();
		//$('#countrydropdown_label').html("-- Select --");
		//$('#industrydropdown_label').html("-- Select --");

		if(popupStatus==1){
				disablePopup();
		}
		$('.formHolderPopup').html("");
		$('#done').hide(); 
		$("div.errorWrap").hide();
		
		$('#formwrap').fadeIn('slow');
		initRegisterForm();

}


function formSuccess(sResult){
		//alert(sResult);
		if(sResult != "success"){
			$("#done p").html("An error occurred, please refresh and try again later.");		
		}
		 //hide the form   
		$('#formwrap').hide();
		$('#done').fadeIn('slow'); 
		setTimeout (restoreForm, 2500);

}

function formError(){
		$("div.errorWrap h4").html("An error occurred, please refresh and try again later.");
		$("div.errorWrap").show();		
}
