//REGISTRATION

function register() {   

	var frm = document.getElementById("registrationForm"); 
	var onlyNumText = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|]/;
	function validateEmail(field) {
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(field.value))
		{ return true; }
		 return false;
	}
		
	if (frm.LName.value == ""){
		alert('The \'Last Name\' field is required!');
		frm.LName.focus();
		return false;
	   }
	if (frm.FName.value == ""){
		alert('The \'First Name\' field is required!');
		frm.FName.focus();
		return false;
	   }
	if (frm.Address1.value == ""){
		alert('The \'Address\' field is required!');
		frm.Address1.focus();
		return false;
	   }
	if (frm.City.value == ""){
		alert('The \'City\' field is required!');
		frm.City.focus();
		return false;
	   } 
	if (frm.Zip.value == ""){
		alert('The \'Zip\' field is required!');
		frm.Zip.focus();
		return false;
	   }
	if (frm.Country.value == ""){
		alert('The \'Country\' field is required!');
		frm.Country.focus();
		return false;
	   }
	if (frm.Country.value != "US" && frm.State.value.length == 2){
		alert('US State cannot be selected for non-US countries!');
		frm.State.focus();
		frm.State.selectedIndex = 0;
		return false;
	   }  
	if (frm.Phone1.value == ""){
		alert('The \'Phone\' field is required!');
		frm.Phone1.focus();
		return false;
	   }
	//email validation ---------------------------  
	if (frm.Email.value == ""){
		alert('The \'Email\' field is required!');
		frm.Email.focus();
		return false;
	   }   
	if (!validateEmail(frm.Email)){
		 alert('You must enter a valid email address.');
		 frm.Email.focus();
		 frm.Email.select();
		 return false;
	}
	//end email validation ---------------------------   

	if ((frm.UserName.value == "") || (frm.UserName.length < 5)){
		alert('The \'User Name\' field is required! \nIt must be at least 5 characters long');
		frm.UserName.focus();
		return false;
	}
	if (onlyNumText.test(frm.UserName.value)) {
		alert("The username contains illegal characters.\nOnly letters and numbers are allowed.");
		frm.UserName.focus();
		return false;
	}
	if (frm.Terms.value == ""){
		alert('You must agree to Terms!');
		frm.Terms.focus();
		return false;
	} 

	var options = { 
        target:        '#registrationOutput',   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse  // post-submit callback 
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
	$("#registrationForm").ajaxSubmit(options);
	vunet.fadePage(400,250,"<div id='registrationOutput'>Sending... Please wait!</div><input onclick='vunet.unfadePage()' type='button' class='content' value='OK'>")	
	  
  return false; 
}

// pre-submit callback 
function showRequest(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 
   // alert('About to submit: \n\n' + queryString); 
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText, xhr, $form)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 
    //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
      //  '\n\nThe output div should have already been updated with the responseText.'); 
} 
