var emailRadio;

$j( document ).ready( function(){
	emailRadio = $j('input[@name="data[Mailbox][mail]"]');

	//what should be done when UploadButton is clicked
	$j("#ContinueButton").bind("click", function(){

		if( emailRadio.getValue() == "outlook" ){
			var error = true;
			var file = $j("input[@type='file']");

			if( !empty.test(file.val()) ){
				var fext=getFileExt( file.val() ).toLowerCase();

				if( fext==".csv" || fext==".vcf"){
					displayError(file, "");
					file.hide();

					//submit the ajax file upload
					$j("#ajax_file_upload_form").submit();

					//in the fileupload form attach the type of file we are uploading
					$j("#outlook_form").append("<input type='hidden' name='data[Friend][fileupload]' value='"+fext+"' />");

					//disable all buttons in the page
					$j("input[@type='button']").attr("disabled",true);
					$j("input[@type='submit']").attr("disabled",true);

					error = false;
				} else {
					displayError (file, "Only CSV and VCF files are allowed.");
					error = true;
				}
			} else {
				displayError (file, "Please select an Outlook CSV or vCard (csv or vcf) file.");
				error = true;
			}
			
			if( error ){
				//showError();
				revertDisabledSubmit();
			} 
		} else {
			$j("#email_form").submit();
		}

	}); 
});

function toggleForm(val){
	if( !empty.test(val) ){
		$j("#mail_outlook").setValue("");
		
		//hide/show forms
		$j("#outlook_form_inputs").hide();
		$j("#email_cred_form").slideDown();
		
		//clear form content
		$j("#MailboxUsername").setValue("").focus();
		$j("#MailboxPasswd").setValue("");
		
	} else {
		$j("#mail_yahoo").setValue("");
		$j("#mail_hotmail").setValue("");
		$j("#mail_gmail").setValue("");
		$j("#mail_aol").setValue("");
		
		//hide/show forms
		$j("#outlook_form_inputs").show();
		$j("#email_cred_form").slideUp();
	}
	//either way, enable continue button
	$j("#ContinueButton").removeAttr("disabled").attr("value", "Continue");
}

function fileUploadAfterComplete(){
	$j("#outlook_form").submit();
}

