function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl; 
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
JSONscriptRequest.prototype.buildScriptTag = function () {
    // Create the script tag
    this.scriptObj = document.createElement("script");
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
// removeScriptTag method
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
}

// addScriptTag method
JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}

jQuery.noConflict();

jQuery(document).ready(function(){

jQuery.noConflict();
var file_name = document.location.href;
jQuery('.wba_email_btn').closest("a").click(function() {
	jQuery("#emailFriend").dialog({ closeOnEscape: true, modal: true, height: 445, width: 500, dialogClass: 'dialogBox', title: 'Email A Friend'});
	return false;
});

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	var result= pattern.test(emailAddress);
	return pattern.test(emailAddress);
}

jQuery('#emailFriend #message').val('I thought you might be interested in this Dutch Boy product: ' + file_name + ' (please copy and paste the link your browser if it doesn\'t open automatically.');
jQuery('#emailFriend form').submit(function (e) {
	jQuery('#emailFriend #flash-message').remove();
	var formErrors = false;
	var testEmail = "";
	jQuery("#emailFriend input").removeClass("error")
	jQuery("#emailFriend input").each(function (i) {
		if (jQuery(this).hasClass("emailInput")) {	
			testEmail=jQuery(this).val();
			if (isValidEmailAddress(testEmail)==false) {
				jQuery(this).addClass("error");
				formErrors = true;
			}
		}
		else
		{
			if (jQuery(this).val().length < 1) {
				jQuery(this).addClass("error");
				formErrors = true;
			}
		}
	});
	if (formErrors) {
		jQuery(".dialogBox").css("height","495px");
		jQuery("#emailFriend").css("height","440px");	
		jQuery('#callout').append('<div class="error" id="flash-message"><em>Please be sure that all the required fields are completed and correct.</em></div>');
		return false;
	}
	else
	{
		var mydata = jQuery("#emailFriend form").serialize();
		var req = 'http://dev.dutchboy.com/webstore/email-friend.jsp?' + mydata; 
		bObj = new JSONscriptRequest(req); // create a new request object 
		bObj.buildScriptTag(); // build a dynamic script tag 
		bObj.addScriptTag(); // add the script to the page
		jQuery('#callout').append('<div class="confirmation" id="flash-message"><strong>Your email was sent.  You can tell another friend about this Dimensions Color Sample or click the "X" to close the form</strong></div>');
		jQuery(".dialogBox").css("height","495px");
		jQuery("#emailFriend").css("height","440px");
		return false;
	}
	return false;
}); // end ('#contact-submit').click

}); // document ready
