var ATTACHMENT_REGEX  = /\.(DOC|DOCX|RTF|GIF|JPG|JPEG|PDF|TIFF|TIF|XLS|XLSX|TXT|WPS|PNG|BMP)$/i;
var ATTACHMENT_STRING = "DOC, DOCX, RTF, GIF, JPG, JPEG, PDF, TIFF, TIF, XLS, XLSX, TXT, PNG, BMP or WPS";

function ValidateFaxForm(oForm)
{
	var errors = '';
	
	// sender name
	if ( oForm.fax_s_name.value.length == 0 ) {
		errors += "Empty sender name\n";
	}
	else if ( /[^\w ,\.\'\-]/.test(oForm.fax_s_name.value) ) {
		errors += "Sender name contains invalid characters\n";
	}

	// sender company
	if ( oForm.fax_s_company.value.length > 0 && /[^\w ,\.\'\-]/.test(oForm.fax_s_company.value) ) {
		errors += "Sender company contains invalid characters\n";
	}

	// sender fax
	if ( oForm.fax_s_fax.value.length > 0 && /[^\d\s\.\(\)\-\+]/.test(oForm.fax_s_fax.value) ) {
		errors += "Sender fax contains invalid characters\n";
	}

	// sender email
	if ( oForm.fax_s_email.value.length == 0 ) {
		errors += "Empty sender email\n";
	}
	else if ( !/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(oForm.fax_s_email.value) ) {
		errors += "Invalid sender email\n";
	}

	// receiver name
	if ( oForm.fax_r_name.value.length == 0 ) {
		errors += "Empty receiver name\n";
	}
	else if ( /[^\w ,\.\'\-]/.test(oForm.fax_r_name.value) ) {
		errors += "Receiver name contains invalid characters\n";
	}

	// receiver company
	if ( oForm.fax_r_company.value.length > 0 && /[^\w ,\.\'\-]/.test(oForm.fax_r_company.value) ) {
		errors += "Receiver company contains invalid characters\n";
	}

	// receiver fax
	if ( oForm.fax_r_fax.value.length == 0 ) {
		errors += "Empty receiver fax\n";
	}
	else if ( /[^\d\s\.\(\)\-\+]/.test(oForm.fax_r_fax.value) ) {
		errors += "Receiver fax contains invalid characters\n";
	}

	// file
	var haveAttachment = false;
	for ( var i = 0; i <= 2; i++ ) {
		if ( oForm.attachment[i].value.length != 0 ) { 
			if ( !ATTACHMENT_REGEX.test(oForm.attachment[i].value) ) {
				errors += "Attachment #" + ( i + 1 ) + " may be " + ATTACHMENT_STRING + "\n";
			} else {
				haveAttachment = true; 
			}
		}
	}	
	if ( !haveAttachment ) {
		errors += "No fax file provided\n";
	}

	// captcha
	if (oForm.captcha) {
		if ( oForm.captcha.value.length == 0 ) {
			errors += "Empty confirmation code\n";
		}
		else if ( !/^\d\d\d\d$/.test(oForm.captcha.value) ) {
			errors += "Invalid confirmation code\n";
		}
	}

	// see if any errors
	if ( errors.length > 0 ) {
		alert( errors );
		return false;
	}

	return true;
}

function OnPremiumLogin(oForm)
{
	if (oForm.p_user.value.length == 0 || oForm.p_pass.value.length == 0) {
		alert("E-mail and Password are required");
		return false;
	}

	if (!/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(oForm.p_user.value)) {
		alert("E-mail must contain a valid email field");
		return false;
	}

	var oPremiumForm = document.p_login_form;

	if (oPremiumForm) {
		oPremiumForm.user.value = oForm.p_user.value;
		oPremiumForm.pass.value = oForm.p_pass.value;
		oPremiumForm.submit();
	}

	return false;
}

function show( id )
{
	var el  = document.getElementById( id );
	var img = document.getElementById( id + '_img' );

	if ( el.style.display == 'none' ) {
		el.style.display	= 'block';
		img.src			= '/images/icon_minus.gif';
	}
	else {
		el.style.display	= 'none';
		img.src			= '/images/icon_plus.gif';
	}
}

function OnFilterSubmit( form )
{
	var attach = form.attachment;

	if ( attach.value.length == 0 ) { 
		alert("Missing PDF file attachment\nPlease attach PDF document");
		return false;
	}

	if ( !/\.PDF$/i.test(attach.value) ) {
		alert("Attached file is not PDF document\nPlease attach PDF document");
		return false;
	}

	form.submit();

	return true;
}

function OnConvertSubmit( form )
{
	var haveAttachment = false;
	var errors = '';

	for ( var i = 0; i <= 2; i++ ) {
		if ( form.attachment[i].value.length != 0 ) { 
			if ( !ATTACHMENT_REGEX.test(form.attachment[i].value) ) {
				errors += "Attachment #" + ( i + 1 ) + " may be " + ATTACHMENT_STRING + "\n";
			} else {
				haveAttachment = true; 
			}
		}
	}	
	if ( !haveAttachment ) {
		errors += "No convert files provided\n";
	}

	if ( errors.length > 0 ) {
		alert( errors );
		return false;
	}

	form.submit();

	return true;
}

function OnMergeSubmit( form )
{
	var regex 	   = /\.PDF$/i;
	var errors 	   = '';
	var numAttachments = 0;

	for ( var i = 0; i <= 2; i++ ) {
		if ( form.attachment[i].value.length != 0 ) { 
			if ( !regex.test(form.attachment[i].value) ) {
				errors += "Attachment #" + ( i + 1 ) + " is not PDF document\n";
			} else {
				numAttachments++;
			}
		}
	}	
	if ( numAttachments < 2 ) {
		errors += "You need to attach at least two PDF documents\n";
	}

	if ( errors.length > 0 ) {
		alert( errors );
		return false;
	}

	form.submit();

	return true;
}

