// Basic framework javascript

// open a popup window
function ShowPopup(url, name, width, height ) {
	popURL = (typeof(url) == "string" && url.length) ? url : "''";
	MyWindowName = (name.length) ? name : "MyPopupWindow";
	MyWidth = (width > 0) ? width : 300;
	MyHeight = (height > 0) ? height : 300;

    if (document.all)
         xMax = screen.width, yMax = screen.height;
    else
        if (document.layers)
             xMax = window.outerWidth, yMax = window.outerHeight;
        else
             xMax = 640, yMax=480;
     xOffset = (xMax - MyWidth)/2, yOffset = (yMax - MyHeight)/2;
a = window.open(popURL,MyWindowName,'scrollbars=1,toolbar=yes,width='+MyWidth+',height='+MyHeight+',screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+'');

}

// validate the number of characters a user enters into a textarea tag
function validateLength(value, limit, formName, elementName) {
	if (value.length > limit) {
		alert("ERROR! You have specified " + value.length + " characters!  You can only specify " + limit + " characters for this field.  Please modify your input.");
		eval("document." + formName + "." + elementName + ".focus();");
		eval("document." + formName + "." + elementName + ".select();");
	} else {
		// do nothing
	}
}

function validatePOBox(value, formName, elementName) {
	var regexp  = /^p(ost)?[ |\.]*o(ffice)?[ |\.]*(box)?[ 0-9]*[^[a-z ]]*$/;

	if (regexp.test(value)) {
		eval("document." + formName + "." + elementName + ".focus();");
		eval("document." + formName + "." + elementName + ".select();");
		alert("Currently, May Advertising does not support shipping to P.O. Boxes!.");
		return false;
	} else {
		return true;
	}
}

// this javascript supports the pop-up calendar selection functions.
var today = new Date();
var day   = today.getDate();
var month = today.getMonth();
var year  = today.getFullYear();
var text_field = "";

function padout(number) {
	return (number < 10) ? '0' + number : number;
}

function restart() {
    text_field.value = '' + padout(month - 0 + 1) + '/' + padout(day) + '/' + year;
	//text_field.value = '' + padout(month - 0 + 1) + '/' + year;
    mywindow.close();
}

function newWindow(fieldname,evnt,path) {
    c_today = new Date();
    day = c_today.getDate();
    month = c_today.getMonth();
    year = c_today.getFullYear();
    text_field = fieldname;
    var properties = "left=" + (evnt.screenX + 20);
    properties += ",top=" + (evnt.screenY + 1);
    properties += ",titlebar=0,resizable=no,width=205,height=255";
    mywindow=open(path,'myname',properties);
    mywindow.location.href = path;
    if (mywindow.opener == null) mywindow.opener = self;
    mywindow.focus();
}
// end of calendar javascript support

// added by mjr on 04/12/2002
function validateEmailAddress(emailAddress, formField, formName) {
	var regexp  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
	if (regexp.test(emailAddress)) {
		return true;
	} else {
		eval("document." + formName + "." + formField + ".focus();");
		eval("document." + formName + "." + formField + ".select();");
		alert("Sorry, you entered an invalid email address.  Please correct the email address.");
		return false;
	}
}

/**
 * Strips hyphens out of phone number and verifies that the phone number is valid.
 *
 * Any phone number in the format:
 *	xxxxxxxxxx, xxx-xxx-xxxx, or (xxx) xxx-xxxx will be valid
 */
 function validatePhoneNumber(phoneNumber, formField, formName) {
 	var regexp = /^(\d{10}|\d{3}-\d{3}-\d{4}|\(\d{3}\) \d{3}-\d{4})$/;
	
	if (regexp.test(phoneNumber) || phoneNumber.length == 0) {
		return true;
	} else {
		eval("document." + formName + "." + formField + ".focus();");
		eval("document." + formName + "." + formField + ".select();");
		alert("Sorry, you entered an invalid phone number: " + phoneNumber + "\r\rPlease use one of the following formats:\r\n\t1. xxxxxxxxxx\r\n\t2. xxx-xxx-xxxx\r\n\t3. (xxx) xxx-xxxx");
		return false;
	}
 }
 
 function subscribe(emailaddress, formField, formName, form) {
	if(validateEmailAddress(emailaddress, formField, formName)) {
		return true;
	} else {
		return false;
	}
 }
 
 function searchString (value) {
 	if(value == '') {
		alert('Please provide a search string!');
		return false;
	} else {
		return true;
	}
 }