// JavaScript Document
var numSpams = 0;
var contactID = 0;
var oldPass = '';

function initializeLogReg() {
	initializeBase();
	initializeLog();
	initializeReg( false );
}

function initializeBase() {
	if( Browser.Version() >= 8 ) {
		$('input.txt').wrap( '<div class="rounded"></div>' );
		$('input.txtDis').wrap( '<div class="roundedGray"></div>' );
	}
	$('select.txt').not( $('select.selSmall')).wrap( '<div class="roundedSel"></div>' );
	$('select.selSmall').wrap( '<div class="roundedSelSmall"></div>' );
	$('textarea.txt').wrap( '<div class="roundedArea"></div>' );

	$.get("includes/token.php", function( txt ) {
  		$(".secure").append( '<input type="hidden" name="ts" id="ts" value="' + txt + '" />' );
	});
}

function initializeLog() {
	setupLogin();
}

function initializeReg( isAccount ) {
	$('#reg').ajaxContactForm( '', {
		language: "en", custFlash: true, custFlashFunc: flashField
	});

	$("#doReg").bind( "click", function() {
		$("#divLogAll").animate( { opacity:0 }, 500, function() {
				$("#divLogAll").css( "display", "none" );
				$("#divRegAll").css( "display", "block" );
				paintReg();
			});
	});

	setupReg( isAccount );
}

function handleLogin() {
	var uname = $("#Username").val();
	var pass = $("#Password").val();

	if( uname == '' ) {
		$("Username").focus();
		alert( "Please enter your user name." );
		flashField( $("#Username"), false );
		return;
	}
	if( pass == '' ) {
		$("Password").focus();
		alert( "Please enter your password." );
		flashField( $("#Password"), false );
		return;
	}

	var f = document.formLogin;
	f.action = "includes/nslogin.php?Username=" + uname + "&Password=" + pass + "&ts=" + $("#ts").val();
	f.submit();
}

function setupLogin() {
	$("#Username, #Password").keyup(function(e) {
		if( e.keyCode == 13 ) handleLogin();
	});

	$("#submitLog").bind( "click", function() {
		handleLogin();
	});
}

function setupReg( isAccount ) {
	$("#submitReg").bind( "click", function() {
		if( $('#reg').ajaxContactForm.verifyForm() ) {
			var fSpam = false;
			var uname = isAccount ? '' : $("#uname").val();
			var pass = $("#upass").val();
			var passC = $("#upassconf").val();
			var eml = $("#email").val();
			var emlC = $("#emailconf").val();
			var dnc = isAccount ? $("#dnc").is( ":checked" ) : false;

			if( !isAccount ) {
				if( uname.indexOf( " " ) > -1 ) {
					$("#uname").focus();
					alert( "No spaces are allowed in the user name.");
					flashField( $("#uname"), false );
					return;
				}
				if( uname.length > 25 ) {
					$("#uname").focus();
					alert( "User name cannot be greater than 25 characters.");
					flashField( $("#uname"), false );
					return;
				}
				if( pass.length > 15 ) {
					$("#upass").focus();
					alert( "Passwords cannot be greater than 15 characters.");
					flashField( $("#upass"), false );
					return;
				}
				if( pass.toUpperCase() == uname.toUpperCase() ) {
					$("#upass").focus();
					alert( "Password cannot be the same as the username." );
					flashField( $("#upass"), false );
					return;
				}
				if( pass != passC ) {
					$("#upass").focus();
					alert( "Your passwords did not match." );
					flashField( $("#upass"), true );
					flashField( $("#upassconf"), true );
					return;
				}
			}
			else {
				if( ( pass != "" ) && ( pass != oldPass ) && ( pass != passC ) )
				{
					$("#upass").focus();
					alert( "You did not correctly confirm your password. Please reenter." );
					flashField( $("#upass"), true );
					flashField( $("#upassconf"), true );
					return;
				}
			}
			if( $("#fname").val().toUpperCase() == $("#lname").val().toUpperCase() ) fSpam = true;
			if( !isAccount && ( eml != emlC ) ) {
				$("#email").focus();
				alert( "Your emails did not match." );
				flashField( $("#email"), true );
				flashField( $("#emailconf"), true );
				return;
			}
			if( $("#country :selected").val() == '00' ) {
				$("#country").focus();
				alert( "Please specify a country." );
				flashField( $("#country"), false );
				return;
			}
			if( isAccount ) {
				if( $("#source").val() == "Reference from Friend/Colleague" ) {
					if( ( $("#other").val() == "" ) || ( $("#refemail").val() == "" ) ) {
						$("#other").focus();
						alert( "You must enter the name and email of the referring person." );
						flashField( $("#other"), true );
						flashField( $("#refemail"), true );
						return;
					}
				}
				if( ( $("#comments").val().indexOf( "http:" ) > -1 ) || ( $("#comments").val().indexOf( "www." ) > -1 ) ) fSpam = true;
			}
			if( fSpam == true ) {
				numSpams++;
				$.ajax({
					type: "get",
					url: "includes/spam.php",
					timeout: 10000
				});
				if( numSpams <= 3 ) {
					alert( "You have been identified as a possible spammer.\nPlease, provide legitimate information." );
					return;
				}
				else {
					alert( "You have been identified as a spammer too many times.\nPlease contact NeuroScript or try at a later time to register." );
					return;
				}
			}
			if( isAccount ) submitReg( isAccount );
			else verifyUser();
		}
		else scroll( 0, 0 );
	});
}

function paintReg() {
	$("#divReg").backgroundCanvasPaint( RoundedShadowPaint );
	$(".rounded").backgroundCanvasPaint( RoundedShadowPaint );
	$(".roundedGray").backgroundCanvasPaint( TopDownPaintButtonsGraySolid );
	$(".roundedSel").backgroundCanvasPaint( RoundedShadowPaint );
	$(".roundedSelSmall").backgroundCanvasPaint( RoundedShadowPaint );
	$(".roundedArea").backgroundCanvasPaint( RoundedShadowPaint );
}

var curID = null;
var setTO = false;
var flashField = function( obj, doAll ) {
	var id = "#" + $(obj).attr( "id" );
	$(id).parent().parent().backgroundCanvasPaint( TopDownPaintButtonsOrange );
	curID = id;
	if( doAll ) { if( !setTO ) { setTO = true; setTimeout( flashReturnAll, 1000 ); } }
	else setTimeout( flashReturn, 1000 );
}
var flashReturn = function() {
	if( !setTO && ( curID != null ) ) {
		$(curID).parent().parent().backgroundCanvasPaint( RoundedShadowPaint );
		curID = null;
	}
}
var flashReturnAll = function() {
	$(".rounded").backgroundCanvasPaint( RoundedShadowPaint );
	setTO = false;
}

function onChangeHowHear() {
	var en = ( $("#source").val().indexOf( 'Reference' ) >= 0 ) ? true : false;
	if( !en ) {
		$(".divRefs :input").attr( "disabled", true );
		$(".divRefs :input").val( '' );
		$(".roundedGray").backgroundCanvasPaint( TopDownPaintButtonsGraySolid );
	}
	else {
		$(".divRefs :input").removeAttr( "disabled" );
		$(".roundedGray").backgroundCanvasPaint( RoundedShadowPaint );
	}
}

function onClickLogin() {
	$("#logRegReq").animate( { opacity:0 }, 500, function() {
			$("#logRegReq").css( "display", "none" );
			$("#divLogAll").css( "display", "block" );
			$("#divLog").backgroundCanvasPaint( RoundedShadowPaint );
			$(".rounded").backgroundCanvasPaint( RoundedShadowPaint );
			$("#Username").focus();
		}
	);
}

function onClickRegister() {
	$("#logRegReq").animate( { opacity:0 }, 500, function() {
			$("#logRegReq").css( "display", "none" );
			$("#divRegAll").css( "display", "block" );
			$("#divReg").backgroundCanvasPaint( RoundedShadowPaint );
			$(".rounded").backgroundCanvasPaint( RoundedShadowPaint );
			$(".roundedGray").backgroundCanvasPaint( TopDownPaintButtonsGraySolid );
			$(".roundedSel").backgroundCanvasPaint( RoundedShadowPaint );
			$(".roundedSelSmall").backgroundCanvasPaint( RoundedShadowPaint );
			$(".roundedArea").backgroundCanvasPaint( RoundedShadowPaint );
		}
	);
}

function verifyUser() {
	var params = "username=" + $("#uname").val();
	params += "&email=" + $("#email").val();
	params += "&ts=" + $("#ts").val();

	$.ajax({
		type: "get",
		url: "includes/verifyuser.php",
		data: params,
		timeout: 10000,
		success: function( ret ) {
			if( ret != '' ) {
				var msg = '';
				if( ret == "user name" ) {
					msg = "The specified " + ret + " is already registered.";
					alert( msg );
					flashField( $("#uname"), false );
					return;
				}
				else if( ret == "email" ) {
					msg = "The specified " + ret + " is already registered.";
					alert( msg );
					flashField( $("#email"), true );
					flashField( $("#emailconf"), true );
					return;
				}
				else {			// email is bad
					msg = ret;
					alert( msg );
					flashField( $("#email"), true );
					flashField( $("#emailconf"), true );
					return;
				}
			}
			submitReg( false );
		},
		error: function() {
			alert( "There was an error processing your request." );
		}
	});
}

function submitReg( isAccount ) {
	if( isAccount ) {
		var areas = '';
		$("input:checked[name=area]").each( function( i ) {
			if( i > 0 ) areas += ",";
			areas += $(this).val();
		});
		$("#areas").val( areas );
	}

	var params = $('#reg').ajaxContactForm.getFormData( false, true );

	if( isAccount ) {
		params += "&update=1&uname=" + $("#uname").html();
		params += "&usrpass=" + oldPass;
		params += "&contid=" + contactID;
	}

	$.ajax({
		type: "get",
		url: "includes/reg.php",
		data: params,
		timeout: 10000,
		success: function( ret ) {
			document.location.href = ret;
		},
		error: function() {
			alert( "There was an error processing your request." );
		}
	});
}

function showPrivacy() {
	popUpWin('privacy.html','no','no','no','no','yes','yes',580,600,80,80);
}

function showLA() {
	popUpWin('licenseagreement.html','no','no','no','no','yes','yes',580,600,80,80);
}

