/*========= Check Delivery address Form ==================*/

function checkDeliveryForm()
{

        // instantiate object
        fv = new validateData();

        // perform checks

        // check First Name field (Required)
	setCookie365("b_first",document.Delivery.b_first.value);
        if (fv.isEmpty(document.Delivery.b_first.value))
        {
                fv.raiseError("Please enter your First Name");
        } else {
		if (!fv.isAlphabeticSpecial(document.Delivery.b_first.value)) {
                	fv.raiseError("First Name Invalid. This field may only contain alphabetic characters plus \- \' , ");
		} else {
			setCookie365("b_first",document.Delivery.b_first.value,365);
		}
	}

        // check Last Name field (Required)
	setCookie365("b_last",document.Delivery.b_last.value);
        if (fv.isEmpty(document.Delivery.b_last.value))
        {
                fv.raiseError("Please enter your Last Name");
        } else {
		if (!fv.isAlphabeticSpecial(document.Delivery.b_last.value)) {
                	fv.raiseError("Last Name Invalid. This field may only contain alphabetic characters plus \- \' , ");
		} else {
			setCookie365("b_last",document.Delivery.b_last.value,365);
		}
	}

        // check Email address (Required)
	setCookie365("b_email",document.Delivery.b_email.value);
        if (fv.isEmpty(document.Delivery.b_email.value))
        {
                fv.raiseError("Please enter your Email address");
        } else {
                // check for valid Email address format
                if (!fv.isEmailAddress(document.Delivery.b_email.value))
                {
                        fv.raiseError(document.Delivery.b_email.value + " - is not a valid Email address, please re-enter");
		} else {
			setCookie365("b_email",document.Delivery.b_email.value,365);
		}
        }

        // check phone (Optional)
	setCookie365("b_phone",document.Delivery.b_phone.value);
        if (!fv.isEmpty(document.Delivery.b_phone.value)) {
		setCookie365("b_phone",document.Delivery.b_phone.value,365);
	}

        // check address field (Required)
	setCookie365("b_addr",document.Delivery.b_addr.value);
        if (fv.isEmpty(document.Delivery.b_addr.value))
        {
                fv.raiseError("Please enter your Address");
	} else {
		setCookie365("b_addr",document.Delivery.b_addr.value,365);
	}
        // check address field 2 
	setCookie365("b_addr2",document.Delivery.b_addr2.value);
        if (!fv.isEmpty(document.Delivery.b_addr2.value))
        { 
		setCookie365("b_addr2",document.Delivery.b_addr2.value,365);
	}


        // check city field (Required)
	setCookie365("b_city",document.Delivery.b_city.value);
        if (fv.isEmpty(document.Delivery.b_city.value))
        {
                fv.raiseError("Please enter your City");
        } else {
		if (!fv.isAlphabeticSpecial(document.Delivery.b_city.value)) {
                	fv.raiseError("City Invalid. This field may only contain alphabetic characters plus \- \' , ");
		} else {
			setCookie365("b_city",document.Delivery.b_city.value,365);
		}
	}


        // if errors, display, else proceed
        if (fv.numErrors() > 0)
        {
                fv.displayErrors();
                return false;
        }
        else
        {
                return true;
        }

}

/*========= End of Address Form check ======================*/

/*========= Cookie functions ======================*/

function setCookie365(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
}


/*========= Popup functions ======================*/
function popupC(mylink, windowname) 
{
 var width  = 620;
 var height = 420;
 var left   = (screen.width  - width)/2;
 var top    = (screen.height - height)/2;
 var params = 'width='+width+', height='+height;
 params += ', top='+top+', left='+left;
 params += ', directories=no';
 params += ', location=no';
 params += ', menubar=no';
 params += ', resizable=no';
 params += ', scrollbars=no';
 params += ', status=no';
 params += ', toolbar=no';
 newwin=window.open(mylink,windowname, params);
 if (window.focus) {newwin.focus()}
 return false;
}

function popup1(mylink, windowname) {
	if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
		href=mylink;
	else
		href=mylink.href;
	window.open(href, windowname, 'width=620, height=420, scrollbars=yes, top=200, left=200, resizable=yes');
	return false;
}

function popup2(mylink, windowname) {
	if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
		href=mylink;
	else
		href=mylink.href;
	window.open(href, windowname, 'width=250, height=400, scrollbars=yes, top=200, left=200, resizable=yes');
	return false;
}

/*========= end Popup functions ======================*/



/*========= Validate Data ==========================*/

function validateData()
{
	// set up array to hold error messages
	this.errorList = new Array;

	// set up object methods
	this.isEmpty = isEmpty;	
	this.isNumber = isNumber;	
	this.isAlphabetic = isAlphabetic;	
	this.isAlphabeticSpecial = isAlphabeticSpecial;	
	this.isAlphaNumeric = isAlphaNumeric;	
	this.isWithinRange = isWithinRange;	
	this.isEmailAddress = isEmailAddress;	
	this.isChecked = isChecked;	

	this.raiseError = raiseError;	
	this.numErrors = numErrors;	
	this.displayErrors = displayErrors;	
}

function isEmpty(val)
{
	//
	// check to see if input is whitespace only or empty
	//
	if (val.match(/^s+$/) || val == "")
	{
		return true;
	}
	else
	{
		return false;
	}	
}

function isNumber(val)
{
	//
	// check to see if input is number
	//
	if (isNaN(val))
	{
		return false;
	}
	else
	{
		return true;
	}	
}

function isAlphabetic(val)
{
	//
	// check to see if input is alphabetic
	//
	if (val.match(/^[a-zA-Z]+$/))
	{
		return true;
	}
	else
	{
		return false;
	}	
}

function isAlphabeticSpecial(val)
{
	//
	// check to see if input is alphabetic with spaces and some special characters allowed
	//
	if (val.match(/^[a-zA-Z' ',\-\']+$/))
	{
		return true;
	}
	else
	{
		return false;
	}	
}

function isAlphaNumeric(val)
{
	//
	// check to see if input is alphanumeric
	//
	if (val.match(/^[a-zA-Z0-9]+$/))
	{
		return true;
	}
	else
	{
		return false;
	}	
}

function isWithinRange(val, min, max)
{
	//
	// check to see if value is within range
	//
	if (val >= min && val <= max)
	{
		return true;
	}
	else
	{
		return false;
	}	
}

function isEmailAddress(val)
{
	//
	// check to see if input is a valid email address
	//
        if (val.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/))
	{
		return true;
	}
	else
	{
		return false;
	}	
}

function isChecked(obj)
{
	//
	// check to see if form value is checked
	//
	if (obj.checked)
	{
		return true;
	}
	else
	{
		return false;
	}	
}


function displayErrors()
{
	//	
	// display all errors
	// iterate through error array and print each item
	//
	stR = "\n";
	for (x=0; x<this.errorList.length; x++)
	{
		stR += this.errorList[x] + "\n";
	}
	alert("Error: " + stR);
}

function raiseError(msg)
{
	//
	// add an error to error list
	//
	this.errorList[this.errorList.length] = msg;
}

function numErrors()
{
	//
	// return number of errors in error array
	//
	return this.errorList.length;
}

/*========= End of validateData ====================*/

