/*********
pop up a new window
*********/
function openWindow(url,x,y,toolbar,scrollbars,resizable) {
	if(x == 0)
	{
		x = 720;
	}

	if(y == 0)
	{
		y = 520;
	}

	new_x=x+20;
	new_y=y+20;
	var options = "toolbar=" + toolbar + ",scrollbars=" + scrollbars + ",resizable=" + resizable + ",width=" + new_x + ",height=" + new_y;
	newWindow=window.open(url,"WinOpen",options);
}

/*********
toggle the text inside a form on/off
*********/
function toggleFormText(thisId, thisText) {
	//alert(document.getElementById(thisId).value + ',' + thisText);
	if (document.getElementById(thisId).value == thisText)
	{
		document.getElementById(thisId).value='';
	}
	else if (document.getElementById(thisId).value == '')
	{
		document.getElementById(thisId).value=thisText;
	}
}

/*********
validates an email address
*********/
function validateEmail(thisEmail) {
	//initiate returnMessage variable
	var returnMessage="";

	if(thisEmail == '')
	{
		// alert the user
		returnMessage = "Please type in a valid email address.";
	}
	else
	{
		// function to check email address vilidity
		function emailCheck(emailStr)
		{
			/* The following pattern is used to check if the entered e-mail address
			   fits the user@domain format.  It also is used to separate the username
			   from the domain. */
			var emailPat=/^(.+)@(.+)$/
			/* The following string represents the pattern for matching all special
			   characters.  We don't want to allow special characters in the address.
			   These characters include ( ) < > @ , ; : \ " . [ ]    */
			var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
			/* The following string represents the range of characters allowed in a
			   username or domainname.  It really states which chars aren't allowed. */
			var validChars="\[^\\s" + specialChars + "\]"
			/* The following pattern applies if the "user" is a quoted string (in
			   which case, there are no rules about which characters are allowed
			   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
			   is a legal e-mail address. */
			var quotedUser="(\"[^\"]*\")"
			/* The following pattern applies for domains that are IP addresses,
			   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
			   e-mail address. NOTE: The square brackets are required. */
			var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
			/* The following string represents an atom (basically a series of
			   non-special characters.) */
			var atom=validChars + '+'
			/* The following string represents one word in the typical username.
			   For example, in john.doe@somewhere.com, john and doe are words.
			   Basically, a word is either an atom or quoted string. */
			var word="(" + atom + "|" + quotedUser + ")"
			// The following pattern describes the structure of the user
			var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
			/* The following pattern describes the structure of a normal symbolic
			   domain, as opposed to ipDomainPat, shown above. */
			var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


			/* Finally, let's start trying to figure out if the supplied address is
			   valid. */

			/* Begin with the coarse pattern to simply break up user@domain into
			   different pieces that are easy to analyze. */
			var matchArray=emailStr.match(emailPat);
			//alert(emailStr);
			var checkMatch='-' + matchArray + '-';
			if (checkMatch == "-null-")
			{
			  /* Too many/few @'s or something; basically, this address doesn't
				 even fit the general mould of a valid e-mail address. */
				returnMessage = "The email address seems incorrect (check @ and .'s).";
			}
			else
			{
				var user=matchArray[1];
				var domain=matchArray[2];

				// See if "user" is valid
				if (user.match(userPat)==null)
				{
					// user is not valid
					returnMessage = "The email's username doesn't seem to be valid (before the @).";
				}

				/* if the e-mail address is at an IP address (as opposed to a symbolic
				   host name) make sure the IP address is valid. */
				var IPArray=domain.match(ipDomainPat)
				if (IPArray!=null) {
					// this is an IP address
					  for (var i=1;i<=4;i++) {
						if (IPArray[i]>255) {
							returnMessage = "The email's destination IP address is invalid.";
						}
					}
				}

				// Domain is symbolic name
				var domainArray=domain.match(domainPat)
				if (domainArray==null) {
					returnMessage = "The email's domain name doesn't seem to be valid (after the @).";
				}

				/* domain name seems valid, but now make sure that it ends in a
				   three-letter word (like com, edu, gov) or a two-letter word,
				   representing country (uk, nl), and that there's a hostname preceding
				   the domain or country. */

				/* Now we need to break up the domain to get a count of how many atoms
				   it consists of. */
				var atomPat=new RegExp(atom,"g")
				var domArr=domain.match(atomPat)
				var len=domArr.length
				if (domArr[domArr.length-1].length<2 ||
					domArr[domArr.length-1].length>4) {
				   // the address must end in a two letter or three letter word.
				   returnMessage = "The email must end in a four-letter domain, three-letter domain, or two letter country.";
				}

				// Make sure there's a host name preceding the domain.
				if (len < 2) {
				   returnMessage="This email is missing a hostname!";
				}
			}
		}
		// call the validation function and return its result
		val=emailCheck(thisEmail);
		// if it returns val=no_submit, stop form
	}
	return returnMessage;
}

/************
verify Contact Form
************/
function verifyContactForm() {
	//form variables
	submitBtnMessage='Saving... Please Wait';
	formName='ContactForm';

	//initialize submitForm value
	submitForm=true;
	warningMessage="";
	formFocus="";

	//check name
	if (submitForm)
	{
		formElement="Contact_Name";
		thisDOM=eval('document.' + formName + '.' + formElement);
		if (thisDOM.value == "")
		{
			warningMessage='Please type in your name.';
			formFocus=formElement;
			submitForm=false;
		}
	}

	//check Company
	if (submitForm)
	{
		formElement="Contact_Company";
		thisDOM=eval('document.' + formName + '.' + formElement);
		if (thisDOM.value == "")
		{
			warningMessage='Please type in your company name.';
			formFocus=formElement;
			submitForm=false;
		}
	}

	//check the email
	if (submitForm)
	{
		formElement="Contact_Email";
		thisDOM=eval('document.' + formName + '.' + formElement);
		//check the email address
		thisMessage=validateEmail(thisDOM.value);
		//if there is no message sent back, set the alert.
		if (thisMessage.length != 0)
		{
			warningMessage=thisMessage;
			formFocus=formElement;
			submitForm=false;
		}
	}
	//check comment
	if (submitForm)
	{
		formElement="Message";
		thisDOM=eval('document.' + formName + '.' + formElement);
		if (thisDOM.value == "")
		{
			warningMessage='Please type in your comments or questions.';
			formFocus=formElement;
			submitForm=false;
		}
	}

	//disable the form if the form checks out
	if (!submitForm)
	{
		alert(warningMessage);
	}

	//focus the form if necessary
	if (formFocus.length > 0)
	{
		thisDOM=eval('document.' + formName + '.' + formFocus);
		thisDOM.focus();
	}

	//return the boolean value whether or not we should submit this form
	return submitForm;
}

/************
verify EMMA Newsletter
************/
function verifyEmmaSubscribe() {
	//form variables
	submitBtnMessage='Saving... Please Wait';
	formName='formEmmaSubscribe';

	//initialize submitForm value
	submitForm=true;
	warningMessage="";
	formFocus="";

	//check name_first
	if (submitForm)
	{
		formElement="name_first";
		thisDOM=eval('document.' + formName + '.' + formElement);
		if (thisDOM.value == "")
		{
			warningMessage='Please type in your first name.';
			formFocus=formElement;
			submitForm=false;
		}
	}

	//check name_last
	if (submitForm)
	{
		formElement="name_last";
		thisDOM=eval('document.' + formName + '.' + formElement);
		if (thisDOM.value == "")
		{
			warningMessage='Please type in your last name.';
			formFocus=formElement;
			submitForm=false;
		}
	}

	//check the email
	if (submitForm)
	{
		formElement="email_address";
		thisDOM=eval('document.' + formName + '.' + formElement);
		//check the email address
		thisMessage=validateEmail(thisDOM.value);
		//if there is no message sent back, set the alert.
		if (thisMessage.length != 0)
		{
			warningMessage=thisMessage;
			formFocus=formElement;
			submitForm=false;
		}
	}

	//disable the form if the form checks out
	if (!submitForm)
	{
		alert(warningMessage);
	}

	//focus the form if necessary
	if (formFocus.length > 0)
	{
		thisDOM=eval('document.' + formName + '.' + formFocus);
		thisDOM.focus();
	}

	//return the boolean value whether or not we should submit this form
	return submitForm;
}

/*********
returns true if the phone number is valid, false otherwise
Valid:
(343) 234-3432
2342342342
234-242-2342
*********/
function isPhoneValid(phone_number) {
	valid = true;

	if (thisDOM.value.search(/^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$/) == -1)
	{
		valid = false;
	}

	return valid;
}


var theText = new Array() // do not change this
theText[0] = 'Our goal is to provide the best customer service possible';
theText[1] = 'Our goal is to make each client feel like the only client';
theText[2] = 'Our goal is to deliver our service on-time and on-budget';
theText[3] = 'Our goal is to deliver cutting edge technology';


// do not edit anything below this line
var j = 0
var p = theText.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
	preBuffer[i] = new Array()
	preBuffer[i].src = theText[i]
}
var whichText = Math.round(Math.random()*(p-1));
function showText(){
//This a wrapper for the text array it can be changed if need
	document.write('<p id="goalsMessage">'+theText[whichText]+'</p>');
}

/************
verify Quote Form
************/
function verifyQuoteForm() {
	//form variables
	formName='formQuote';

	//initialize submitForm value
	submitForm=true;
	warningMessage="";
	formFocus="";

	//check name
	if (submitForm)
	{
		formElement="name";
		thisDOM=eval('document.' + formName + '.' + formElement);
		if (thisDOM.value == "")
		{
			warningMessage='Please type in your name.';
			formFocus=formElement;
			submitForm=false;
		}
	}

	//check the email
	if (submitForm)
	{
		formElement="email_address";
		thisDOM=eval('document.' + formName + '.' + formElement);
		//check the email address
		thisMessage=validateEmail(thisDOM.value);
		//if there is no message sent back, set the alert.
		if (thisMessage.length != 0)
		{
			warningMessage=thisMessage;
			formFocus=formElement;
			submitForm=false;
		}
	}
	//type
	if (submitForm)
	{
		formElement="type";
		thisDOM=eval('document.' + formName + '.' + formElement);
		if (thisDOM.value == "" || thisDOM.value == "Please Select One")
		{
			warningMessage='Please select the type of mailer.';
			formFocus=formElement;
			submitForm=false;
		}
	}
	// quantity
	if (submitForm)
	{
		formElement="quantity";
		thisDOM=eval('document.' + formName + '.' + formElement);
		if (thisDOM.value == "")
		{
			warningMessage='Please enter the quantity you would like.';
			formFocus=formElement;
			submitForm=false;
		}
	}
	// size
	if (submitForm)
	{
		formElement="size";
		thisDOM=eval('document.' + formName + '.' + formElement);
		if (thisDOM.value == "" || thisDOM.value == "Please Select One")
		{
			warningMessage='Please select the size of your mailer.';
			formFocus=formElement;
			submitForm=false;
		}
	}
	// paper
	if (submitForm)
	{
		formElement="paper";
		thisDOM=eval('document.' + formName + '.' + formElement);
		if (thisDOM.value == "" || thisDOM.value == "Please Select One")
		{
			warningMessage='Please select the paper for your mailer.';
			formFocus=formElement;
			submitForm=false;
		}
	}
	// coating
	if (submitForm)
	{
		if (!(document.getElementById('coating_UV_1').checked) && !(document.getElementById('coating_UV_2').checked))
		{
			warningMessage='Please select the type of coating.';
			formFocus=formElement;
			submitForm=false;
		}
	}
	// paper finish
	if (submitForm)
	{
		if (!(document.getElementById('finish_gloss').checked) && !(document.getElementById('finish_matte').checked) && !(document.getElementById('finish_dull').checked) && !(document.getElementById('finish_none').checked))
		{
			warningMessage='Please select the type of paper finish.';
			formFocus=formElement;
			submitForm=false;
		}
	}
	// mail service
	if (submitForm)
	{
		if (!(document.getElementById('mailservice_yes').checked) && !(document.getElementById('mailservice_yes').checked))
		{
			warningMessage='Please select if you need mail service.';
			formFocus=formElement;
			submitForm=false;
		}
	}
	// mailing list
	if (submitForm)
	{
		if (!(document.getElementById('list_provided').checked) && !(document.getElementById('list_needed').checked))
		{
			warningMessage='Please select whether your mailing list will be provided or not.';
			formFocus=formElement;
			submitForm=false;
		}
	}
	//paper
	if (submitForm)
	{
		formElement="paper";
		thisDOM=eval('document.' + formName + '.' + formElement);
		if (thisDOM.value == "")
		{
			warningMessage='Please select the type of paper.';
			formFocus=formElement;
			submitForm=false;
		}
	}
	//postage
	if (submitForm)
	{
		formElement="postage";
		thisDOM=eval('document.' + formName + '.' + formElement);
		if (thisDOM.value == "")
		{
			warningMessage='Please select the type of postage.';
			formFocus=formElement;
			submitForm=false;
		}
	}
	//securitycode
	if (submitForm)
	{
		formElement="securitycode";
		thisDOM=eval('document.' + formName + '.' + formElement);
		if (thisDOM.value == "")
		{
			warningMessage='Please type in the confirmation numbers to submit your request.';
			formFocus=formElement;
			submitForm=false;
		}
	}

	//disable the form if the form checks out
	if (!submitForm)
	{
		alert(warningMessage);
	}

	//focus the form if necessary
	if (formFocus.length > 0)
	{
		thisDOM=eval('document.' + formName + '.' + formFocus);
		thisDOM.focus();
	}

	//return the boolean value whether or not we should submit this form
	return submitForm;
}


/************
Change the quote form values based on type
************/
function updateQuoteFormBasedOnType(thisType,sizeArray,paperArray,displayStyle) {

	document.getElementById('envelopeSizeDisplay').style.display = displayStyle;
	/*if (thisType == "postcard")
	{
		sizeArray = new Array("Please Select One","4 1/2 x 6","5 1/2 x 8 1/2","6 x 11","8 1/2 x 11","Other");
		paperArray = new Array("Please Select One","100# Coated Cover","Other");
	}
	else if (thisType == "letter/envelope")
	{
		sizeArray = new Array("Please Select One","8 1/2 x 11","8 1/2 x 14","11 x 17","Other");
		paperArray = new Array("Please Select One","24# White Wove","70# Book","80# Book","100# Book","Other");
		$('envelopeSizeDisplay').style.display = 'block';
	}
	else if (thisType == "self mailer")
	{
		sizeArray = new Array("Please Select One","8 1/2 x 11","11 x 14","11 x 17","Other");
		paperArray = new Array("Please Select One","100# Book","80# Cover","Other");
		$('envelopeSizeDisplay').style.display = 'none';
	}
	else
	{
		sizeArray = new Array();
		paperArray = new Array();
	}*/

	// change size
	thisSize = document.getElementById('size');
	thisSize.options.length = 0;
	for (i=0; i<sizeArray.length; i++)
	{
		val = sizeArray[i];
		thisSize.options[i] = new Option(val,val);
	}

	// change paper
	thisPaper = document.getElementById('paper');
	thisPaper.options.length = 0;
	for (j=0; j<paperArray.length; j++)
	{
		val = paperArray[j];
		thisPaper.options[j] = new Option(val,val);
	}
}

$(document).ready(function() {
	// Slider
	$('#heroSlider').nivoSlider({
		effect: 'fade',
		pauseTime: 3500,
		controlNav: false,
		directionNav:false
	});

});
