/**
* ----------------------------------------------------------------------
* Copyright (C) 2009 by Dynitri Technologies
* http://dynitri.com/
* ----------------------------------------------------------------------
* 
* This file and its associated application are the express property of
* of Dynitri Technologies. It is not to be redistributed in any form
* without direct permission from an authorized company executive.
* 
* All rights are reserved.
* 
* ----------------------------------------------------------------------
* Filename:    validateForm.js
* Author(s):   Matt Runkle <matt@dynitri.com>
* Purpose:     Performs client-side data validation on forms
* ----------------------------------------------------------------------
*/

// This will determine if the passed field is empty
function isEmpty(field) {
	with(field) {
		
		if (value == "" || value == null) {
			style.borderColor = "#FF3100";
			focus();
			return true;
		} else {
			style.borderColor = "#FDBD6E";
		}
	}
	return false;
}

// This will determine if the supplied URL is valid
function isValidURL(field){
    with(field) {
    	
		var RegExp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
	    
		if ( RegExp.test(value) ) {
	        style.borderColor = "#FDBD6E";
			return true;
	    } else {
	    	style.borderColor = "#FF3100";
			focus();
	    }
    }
    return false;
}

// This will determine if the supplied Email is valid
function isValidEmail(field){
    with(field) {
    	
		var RegExp = /[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i;
	    
		if ( RegExp.test(value) ) {
	        style.borderColor = "#FDBD6E";
			return true;
	    } else {
	    	style.borderColor = "#FF3100";
			focus();
	    }
    }
    return false;
} 

// This validates the FTP account info form
function validateFTP(thisform) {
	with (thisform) {
		//Check remoter server field
		if ( isEmpty(ftpserver) || !isValidURL(ftpserver) ) {
			return false;
		}
		
		//Check username field
		if ( isEmpty(ftpuser) ) {
			return false;
		}
		
		//Check password field
		if ( isEmpty(ftppass) ) {
			return false;
		}
	}

	return true;
}

// This validates the FTP account info form
function validateContact(thisform) {
	with (thisform) {
		//Check email field
		if ( isEmpty(fromemail) || !isValidEmail(fromemail) ) {
			return false;
		}
		
		//Check message field
		if ( isEmpty(message) ) {
			return false;
		}
		
	}

	return true;
}