var RegType = "";

function InitRegGeneral() {
	$("div.jo").css( "opacity" , "0.0" );
	$("div.jo").css( "display" , "block" );
	$("div.jo").animate(
		{ opacity: 1.0 },
		1000,
		function() {
			// when it's done
		}
	);
}

function GetNumberOfSelectedLanguages( oF ) {
	checks = $("input[id^=working_languages_]");
	sels = 0;
	for (i=0;i<checks.length;i++) {
		ochk = $(checks[i]);
		if (ochk.attr("type")!="checkbox") continue;
		if (!ochk.attr("checked")) continue;
		sels++;
	}
	return sels;
}

function InitRegTranslator( b ) {
	if (b) {
		$("div.translator").css("opacity","0.0");
		$("div.translator").css("display","block");
		$("div.translator").animate( { opacity: 1.0 }, 1000, function() { } );
	} else {
		// $("div.translator").hide();
		$("div.translator input").attr( "disabled" , "disabled" );
		$("div.translator select").attr( "disabled" , "disabled" );
		$("div.translator textarea").attr( "disabled" , "disabled" );
		$("div.translator").css("opacity","0.0");
		$("div.translator").css("display","block");
		$("div.translator").animate( { opacity: 0.5 }, 500, function() { } );
	}
}

function InitRegAddress( b ) {
	if (b) {
		$("div.adr").css("opacity","0.0");
		$("div.adr").css("display","block");
		$("div.adr").animate(
			{ opacity: 1.0 },
			1000,
			function() {
				// when it's done
			}
		);
	} else {
		// $("div.adr").hide();
		$("div.adr input").attr( "disabled" , "disabled" );
		$("div.adr select").attr( "disabled" , "disabled" );
		$("div.adr textarea").attr( "disabled" , "disabled" );
		$("div.adr").css("opacity","0.0");
		$("div.adr").css("display","block");
		$("div.adr").animate( { opacity: 0.5 }, 500, function() { } );
	}	
	/*
	$("div.adr").css( "opacity" , "0.0" );
	$("div.adr").css( "display" , "block" );
	*/
}

function InitRegContact( b ) {
	if (b) {
		$("div.contact").css("opacity","0.0");
		$("div.contact").css("display","block");
		$("div.contact").animate( { opacity: 1.0 }, 1000, function() { } );
	} else {
		// $("div.contact").hide();
		$("div.contact input").attr( "disabled" , "disabled" );
		$("div.contact select").attr( "disabled" , "disabled" );
		$("div.contact textarea").attr( "disabled" , "disabled" );
		$("div.contact").css("opacity","0.0");
		$("div.contact").css("display","block");
		$("div.contact").animate( { opacity: 0.5 }, 500, function() { } );
	}
}


function InitRegistrationPane( rt ) {
	// fucking ie
	$(function () {
		if ($.browser.msie) {
			$('input:radio').click(function () {
				this.blur();
				this.focus();
			});
			$('input:checkbox').click(function () {
				this.blur();
				this.focus();
			});
		}
	});

	InitRegGeneral();
	InitRegTranslator( (rt!="jo") );
	InitRegAddress( (rt!="jo") );
	InitRegContact( (rt=="agency") );
	RegType = rt;
	/*
	InitRegAddress( (rt!="jo") );
	*/
	// RegTypeChange( rt );
}


function EnableRegTranslator( b ) {
	if (b) {
		// $("div.translator").slideDown( 500 , function() {} );
		$("div.translator").animate(
			{ opacity: 1.0 },
			500,
			function() {
				$("div.translator input").removeAttr( "disabled" );
				$("div.translator select").removeAttr( "disabled" );
				$("div.translator textarea").removeAttr( "disabled" );
			}
		);
	} else {
		$("div.translator input").attr( "disabled" , "disabled" );
		$("div.translator select").attr( "disabled" , "disabled" );
		$("div.translator textarea").attr( "disabled" , "disabled" );
		$("div.translator").animate( { opacity: 0.5 }, 500, function() { } );
	}
}

function EnableRegAddress( b ) {
	if (b) {
		$("div.adr").animate(
			{ opacity: 1.0 },
			500,
			function() {
				$("div.adr input").removeAttr( "disabled" );
				$("div.adr select").removeAttr( "disabled" );
				$("div.adr textarea").removeAttr( "disabled" );
			}
		);
	} else {
		// $("div.adr").slideUp( 500 , function() {} );
		$("div.adr input").attr( "disabled" , "disabled" );
		$("div.adr select").attr( "disabled" , "disabled" );
		$("div.adr textarea").attr( "disabled" , "disabled" );
		$("div.adr").animate( { opacity: 0.5 }, 500, function() { } );
	}
}

function EnableRegContact( b ) {
	if (b) {
		$("div.contact").animate(
			{ opacity: 1.0 },
			500,
			function() {
				$("div.contact input").removeAttr( "disabled" );
				$("div.contact select").removeAttr( "disabled" );
				$("div.contact textarea").removeAttr( "disabled" );
			}
		);
	} else {
		$("div.contact input").attr( "disabled" , "disabled" );
		$("div.contact select").attr( "disabled" , "disabled" );
		$("div.contact textarea").attr( "disabled" , "disabled" );
		$("div.contact").animate( { opacity: 0.5 }, 500, function() { } );
	}
}

function RegTypeChange( e ) {
	RegType = e;
	switch (e) {
	case "jo":
		EnableRegAddress( false );
		EnableRegTranslator( false );
		EnableRegContact( false );
		break;
	case "parttime":
		EnableRegAddress( true );
		EnableRegTranslator( true );
		EnableRegContact( false );
		break;
	case "freelancer":
		EnableRegAddress( true );
		EnableRegTranslator( true );
		EnableRegContact( false );
		break;
	case "agency":
		EnableRegAddress( true );
		EnableRegTranslator( true );
		EnableRegContact( true );
		break;
	}
}

function RegistrationFormSubmitValidator( oF ) {
	BotJSBeforeSubmit( "BotJS" );
	if ($("#name").hasClass( "invalid" )) {
		alert( aJSLangDefs["Empty_name"] );
		$("#name").focus();
		return false;
	}
	if ($("#email").hasClass( "invalid" )) {
		alert( aJSLangDefs["Wrong_or_used_email_address"] );
		$("#email").focus();
		return false;
	}
	if ($("#pwd").hasClass( "invalid" )) {
		alert( aJSLangDefs["Empty_password"] );
		$("#pwd").focus();
		return false;
	}
	if ($("#pwd2").hasClass( "invalid" )) {
		alert( aJSLangDefs["Empty_password"] );
		$("#pwd2").focus();
		return false;
	}
	if ($("#pwd").val()!=$("#pwd2").val()) {
		alert( aJSLangDefs["Passwords_differ"] );
		$("#pwd2").focus();
		return false;
	}
	if ($("#contact_phone").hasClass( "invalid" )) {
		alert( aJSLangDefs["Empty_contact_phone"] );
		$("#contact_phone").focus();
		return false;
	}
	if (!IsValidPhone( $("#contact_phone").val() )) {
		alert( aJSLangDefs["Invalid_contact_phone"] );
		$("#contact_phone").focus();
		return false;
	}
	if (RegType=="jo") return true; // at this point

	if ($("#native_language").val()=="") {
		alert( aJSLangDefs["No_native_language"] );
		$("#native_language").focus();
		return false;
	}
	
	
	// number of languages - check
	numoflangs = GetNumberOfSelectedLanguages( oF );
	switch (RegType) {
	case "jo":
		maxlangs = 90000;
		break;
	case "parttime":
		maxlangs = aJSVars.max_languages_parttime;
		break;
	case "freelancer":
		maxlangs = aJSVars.max_languages_freelancer;
		break;
	case "agency":
		maxlangs = aJSVars.max_languages_agency;
		break;
	}

	// legalabb 1 nyelv
	if (!numoflangs) {
		alert( aJSLangDefs.No_working_languages );
		return false;
	}

	// maximum nyelvek
	if (numoflangs>maxlangs) {
		alert(
			aJSLangDefs.registration_Too_much_working_language + "\n" +
			aJSLangDefs.registration_Acceptable_number_of_languages + " " +
			maxlangs );
		return false;
	}
	

	// check for contact information
	if ($("#zip").hasClass( "invalid" )) {
		alert( aJSLangDefs["Empty_zipcode"] );
		$("#zip").focus();
		return false;
	}
	if ($("#city").hasClass( "invalid" )) {
		alert( aJSLangDefs["Empty_city"] );
		$("#city").focus();
		return false;
	}
	if ($("#address").hasClass( "invalid" )) {
		alert( aJSLangDefs["Empty_address"] );
		$("#address").focus();
		return false;
	}
	if ($("#county").hasClass( "invalid" )) {
		alert( aJSLangDefs["Empty_county"] );
		$("#county").focus();
		return false;
	}
	if ($("#country").hasClass( "invalid" )) {
		alert( aJSLangDefs["Empty_country"] );
		$("#country").focus();
		return false;
	}

	if ($("#billing_name").hasClass( "invalid" )) {
		alert( aJSLangDefs["Empty_billing_name"] );
		$("#billing_name").focus();
		return false;
	}
	
	if ($("#billing_zip").hasClass( "invalid" )) {
		alert( aJSLangDefs["Empty_billing_zipcode"] );
		$("#billing_zip").focus();
		return false;
	}
	if ($("#billing_city").hasClass( "invalid" )) {
		alert( aJSLangDefs["Empty_billing_city"] );
		$("#billing_city").focus();
		return false;
	}
	if ($("#billing_address").hasClass( "invalid" )) {
		alert( aJSLangDefs["Empty_billing_address"] );
		$("#billing_address").focus();
		return false;
	}
	if ($("#billing_country").hasClass( "invalid" )) {
		alert( aJSLangDefs["Empty_billing_country"] );
		$("#billing_country").focus();
		return false;
	}
	if ($("#shipping_zip").hasClass( "invalid" )) {
		alert( aJSLangDefs["Empty_shipping_zipcode"] );
		$("#shipping_zip").focus();
		return false;
	}
	if ($("#shipping_city").hasClass( "invalid" )) {
		alert( aJSLangDefs["Empty_shipping_city"] );
		$("#shipping_city").focus();
		return false;
	}
	if ($("#shipping_address").hasClass( "invalid" )) {
		alert( aJSLangDefs["Empty_shipping_address"] );
		$("#shipping_address").focus();
		return false;
	}
	if ($("#shipping_country").hasClass( "invalid" )) {
		alert( aJSLangDefs["Empty_shipping_country"] );
		$("#shipping_country").focus();
		return false;
	}

	// most mar csak az agency ellenorzes van
	if (RegType!="agency") return true;
	if ($("#contact_name").hasClass( "invalid" )) {
		alert( aJSLangDefs["Empty_contact_name"] );
		$("#contact_name").focus();
		return false;
	}
	if ($("#contact_email").hasClass( "invalid" )) {
		alert( aJSLangDefs["Empty_contact_email"] );
		$("#contact_email").focus();
		return false;
	}
	return true;
}

function Contact2Billing( chk ) {
	if (!chk.checked) return;
	$("#billing_name").val( $("#name").val() );FrontendTextboxNonEmptyChanged( "billing_name" );
	$("#billing_zip").val( $("#zip").val() );FrontendTextboxNonEmptyChanged( "billing_zip" );
	$("#billing_city").val( $("#city").val() );FrontendTextboxNonEmptyChanged( "billing_city" );
	$("#billing_address").val( $("#address").val() );FrontendTextboxNonEmptyChanged( "billing_address" );
	$("#billing_country").val( $("#country").val() );FrontendTextboxNonEmptyChanged( "billing_country" );
}

function Contact2Shipping( chk ) {
	if (!chk.checked) return;
	$("#shipping_name").val( $("#name").val() );FrontendTextboxNonEmptyChanged( "shipping_name" );
	$("#shipping_zip").val( $("#zip").val() );FrontendTextboxNonEmptyChanged( "shipping_zip" );
	$("#shipping_city").val( $("#city").val() );FrontendTextboxNonEmptyChanged( "shipping_city" );
	$("#shipping_address").val( $("#address").val() );FrontendTextboxNonEmptyChanged( "shipping_address" );
	$("#shipping_country").val( $("#country").val() );FrontendTextboxNonEmptyChanged( "shipping_country" );
}

