function ValidEmail( email )
{
    if ( email == null || $.trim( email ) == "" )
        return false;

	email = email.toLowerCase();

    var at = email.indexOf( "@" );

    // must contain @ sign internally, and only one of them
    if ( at <= 0 || at >= (email.length - 1) || email.indexOf( "@", at + 1 ) > - 1 )
		return false;

	var domain = email.substr( at + 1 );

    // @ must be followed at some point by a period, but not right away
    if ( domain.substr( 1 ).indexOf( "." ) < 0 )
        return false;

	var invalidChars = "\/\"'\\ ;:?!()[]\{\}^|";

	for ( var i = 0; i < invalidChars.length ; i++)
		if ( email.indexOf( invalidChars.charAt( i ), 0 ) > -1)
			return false;

	for ( var i = 0; i < email.length ; i++ )
		if ( email.charCodeAt( i ) > 127 )
			return false;

	if ( email.indexOf( "@." ) > -1 || email.indexOf( ".@" ) > -1 || email.indexOf( ".." ) > -1 )
		return false;

	var suffix = email.substring( email.lastIndexOf( "." ) + 1 ),
		found = false;

	if ( suffix.length > 2 )
	{
		var allowed = [ "com", "net", "org", "edu", "int", "mil", "gov", "arpa", "biz", "aero", "name", "coop", "info", "pro", "museum" ];

		for ( var i = 0 ; i < allowed.length ; i++ )
			found |= (suffix == allowed[ i ]);

		if ( !found )
			return false;
	}

    return true;
}


function Form1Init( data )
{

var holderDiv = data.holderDiv || $( "content2" );
var emailfile = data.emailfile || "imacsweb/landingpages/eimacs/quasi/email1.txt";
var page = data.page || "unknwown";
var emailsubject = data.emailsubject || "Online account for Advanced Course Aptitude Test with IMACS";
var lid = "none"; try { lid = tracking.id; } catch(e){}

holderDiv.append("<div class=\"theform\"><table><tr><td class=\"r\">Student's first name:</td><td><input type=\"text\" class=\"large\" name=\"studentfirstname\" /></td></tr><tr><td class=\"r\">Student's last name:</td><td><input type=\"text\" class=\"large\" name=\"studentlastname\" /></td></tr><tr><td class=\"r\">Age:</td><td><input type=\"text\" class=\"small\" name=\"studentage\" /> Grade: <input type=\"text\" class=\"small\" name=\"studentgrade\" /></td></tr><tr><td class=\"r\">Parent's first name:</td><td><input type=\"text\" class=\"large\" name=\"parentfirstname\" /></td></tr><tr><td class=\"r\">Parent's last name:</td><td><input type=\"text\" class=\"large\" name=\"parentlastname\" /></td></tr><tr><td class=\"r\">Parent's email:</td><td><input type=\"text\" class=\"large\" name=\"parentemail\" /></td></tr><tr class=\"ta\"><td class=\"r\">City/State:</td><td><input type=\"text\" class=\"large\" name=\"citystate\" /></td></tr><tr class=\"ta\"><td class=\"r\">Country:</td><td><input type=\"text\" class=\"small\" name=\"country\" value=\"US\" /></td></tr><tr><td class=\"r\">Phone:</td><td><input type=\"text\" class=\"large\" name=\"phone\" /></td></tr><tr class=\"ta\"><td class=\"r\">How did you hear<br/>about IMACS?</td><td><textarea name=\"howheard\" rows=\"3\" class=\"large\" ></textarea></td></tr><tr><td class ='s' colspan=\"2\"><input type=\"button\" value=\"Submit\" /></td></tr></table></div>");


/* TS 
holderDiv.append( "<input type='hidden' id='to' value='gcLPWVOBNUb5ReNlsdlyh+/xTdW/GtHO' />" );
*/


/* TK&TS */
holderDiv.append( "<input type='hidden' id='to' value='gcLPWVOBNUb5ReNlsdlyhzG119qOYJfAlr72/Jqgr2uAu+F4yyFWcA==' />" );

var theform = $( ".theform" );

theform.find( "input[name=studentfirstname]" ).change( function() {
    if ( $( this ).val() == "tedtest" )
    {
        theform.find( "input[name=studentlastname]" ).val( "tedtest" );
        theform.find( "input[name=studentage]" ).val( "1" );
        theform.find( "input[name=studentgrade]" ).val( "1" );
        theform.find( "input[name=parentfirstname]" ).val( "tedtest" );
        theform.find( "input[name=parentlastname]" ).val( "tedtest" );
        theform.find( "input[name=parentemail]" ).val( "tsweet@imacs.org" );
        theform.find( "input[name=citystate]" ).val( "tedtest" );
        theform.find( "input[name=phone]" ).val( "1231231234" );
        theform.find( "textarea" ).val(  "tedtest" );
    }
} );

theform.find("input[type=button]").click(
			function()
			{
				var t = $(this),
					data =
					{
						command: "eimacslanding",
						subcommand: "alert",
                                                emailfile: emailfile ,
                                                subject: emailsubject ,
						studentfirstname: $.trim( theform.find( "input[name=studentfirstname]" ).val() ) ,
                                                forwardtoserver: "http://www.imacs.org/imacsweb/dataserver/dataserver.aspx" ,
                                                studentlastname: $.trim( theform.find( "input[name=studentlastname]" ).val() ) ,
						studentage: $.trim( theform.find( "input[name=studentage]" ).val() ),
						studentgrade: $.trim( theform.find( "input[name=studentgrade]" ).val() ),
						parentfirstname: $.trim( theform.find( "input[name=parentfirstname]" ).val() ),
                                                parentlastname: $.trim( theform.find( "input[name=parentlastname]" ).val() ),
						parentemail: $.trim( theform.find( "input[name=parentemail]" ).val() ),
						citystate: $.trim( theform.find( "input[name=citystate]" ).val() ),
						country: $.trim( theform.find( "input[name=country]" ).val() ),
						phone: $.trim( theform.find( "input[name=phone]" ).val() ),
						howheard: $.trim( theform.find( "textarea" ).val() ) + "; " +
                                                          "Student Grade: " + $.trim( theform.find( "input[name=studentgrade]" ).val() ) + "; " +
                                                          "Enrolled from Page " + page + "; " +
                                                          "Log ID: " + lid ,
						href: location.href,
						to: $("#to").val()
					};

				$(".alert").remove();

				try
				{

					if ( data.studentfirstname == "" )
						throw {focus: theform.find( "input[name=studentfirstname]" ), message: "Please provide the student's first name."};

                                        if ( data.studentlastname == "" )
						throw {focus: theform.find( "input[name=studentlastname]" ), message: "Please provide the student's last name."};

					if ( data.studentage == "" || isNaN( parseInt( data.studentage ) ) || parseInt( data.studentage ) < 0 )
						throw {focus: theform.find( "input[name=studentage]" ), message: "Please provide the student's age in years."};

                                        if ( data.parentfirstname == "" )
						throw {focus: theform.find( "input[name=parentfirstname]" ), message: "Please provide the parent's first name."};

                                        if ( data.parentlastname == "" )
						throw {focus: theform.find( "input[name=parentlastname]" ), message: "Please provide the parent's last name."};

					if ( !ValidEmail( data.parentemail ) )
						throw {focus: theform.find( "input[name=parentemail]" ), message: "Please provide the parent's email address."};

					if ( data.citystate == "" )
						throw {focus: theform.find( "input[name=citystate]" ), message: "Please provide the parent's city and state or province."};

					if ( data.country == "" )
						throw {focus: theform.find( "input[name=country]" ), message: "Please provide the parent's country of residence."};

					if ( data.phone.replace( /[^0-9]/g, "" ).length < 10 )
						throw {focus: theform.find( "input[name=phone]" ), message: "Please provide a phone number, including area code."};

					if ( data.howheard == "" )
						throw {focus: theform.find( "textarea" ), message: "Please let us know how you heard about IMACS."};

					t.css( {color: "#404040"} ).attr( "disabled", "disabled" );
					theform.css( {opacity: 0.4} );

					$.ajax(
						{
							type: "POST",
							url: "http://www.eimacs.com/eimacscode/dataserver/dataserver.aspx",
							dataType: "xml",
							async: true,
							data: data,
							success:
								function( xml )
								{
									theform.css( {opacity: 1 } );
									theform.empty().append( "<p>Thank you for your application. You will soon receive an email detailing the procedure for taking the aptitude test. If you have any questions, do not hesitate to call us toll-free from the US at <em>866.634.6227</em>, or email us at <em>info@eimacs.com</em>.</p>" );
								},
							error:
								function( XMLHttpRequest, textStatus, errorThrown )
								{
								}
						} );
                                       $.ajax(
                                        {
                                            type: "POST",
                                            url: "http://www.eimacs.com/eimacscode/dataserver/dataserver.aspx",
                                            dataType: "xml" ,
                                            async: true ,
                                            data:
                                                {
                                                    command: "eimacslanding" ,
                                                    subcommand: "newlogentry" ,
                                                    forwardtoserver: "http://www.imacs.org/imacsweb/dataserver/dataserver.aspx" ,
                                                    log: 'landing2' ,
                                                    logid: lid ,
                                                    entry: "submitted form, student " + data.studentfirstname + " " + data.studentlastname
                                                } ,
                                            success:
                                                function( xml ) {} ,
                                            error:
                                                    function( XMLHttpRequest, textStatus, errorThrown )
                                                    {
                                                    }
                                        } );
				}
				catch ( e )
				{
					if ( e.focus != null )
					{
						e.focus.parent().append( "<div class=\"alert\">" + e.message + "</div>" );
						e.focus.get( 0 ).focus();
					}
				}
			}
		);




$( "div.theform td" ).css( "padding" , "9px 9px 0px 9px" );
$( "div.theform td" ).css( "text-align" , "left" );
$( "div.theform td.r" ).css( "text-align" , "right" );
$( "div.theform td.s" ).css( "text-align" , "center" ).css( "padding" , "7px 0px 9px 0px" );






}

