// jquery image preloader

(function($) {

  var cache = [];

  // Arguments are image paths relative to the current page.

  $.preLoadImages = function() {

    var args_len = arguments.length;

    for (var i = args_len; i--;) {

      var cacheImage = document.createElement('img');

      cacheImage.src = arguments[i];

      cache.push(cacheImage);

    }

  }

})(jQuery)



function trim(stringToTrim) {

	return stringToTrim.replace(/^\s+|\s+$/g,"");

}



function validateForm(formObj)

{

    isValid = true;

	form_id = $(formObj).attr("id");

	$(".errMsg").css("display", "none");

    

	$( "#"+form_id+" .required").each(function(){

		if( $(this).val() == "" ) {

            errorMsg = " * Field is required";

			

			$(".errMsg[for="+$(this).attr("id")+"]").css("display", "inline");

			$(".errMsg[for="+$(this).attr("id")+"]").html(errorMsg);

			isValid = false;

        }

	});

	

	$( "#"+form_id+" input:text").each(function(){

        errorFound = true;

        errorMsg = "";

        

		if( $(this).val()!="" ) 

		{

			if( $(this).hasClass("email") )

			{

				emailReg = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;

				errorFound = emailReg.test($(this).val());

				errorMsg = " * Invalid email format";

			}

			else if ( $(this).hasClass("regex") )

			{

				var regex = new RegExp($(this).attr("expression"));

				targetStr = $(this).val();

				errorFound = regex.test(targetStr);

				errorMsg = " * Invalid format " + $(this).attr("format");

			}

			

			if( !errorFound )

			{

				$(".errMsg[for="+$(this).attr("id")+"]").css("display", "inline");

				$(".errMsg[for="+$(this).attr("id")+"]").html(errorMsg);

				isValid = errorFound;

			}

		}

    });

	

    return isValid;

}
