function openwindow(page,ww,hh) {
        ww = ww + 30;
        hh = hh + 30;
        windowprops = "width="+ww+",height="+hh+",location=no,scrollbars=yes,menubars=no,toolbars=no,resizable=no";
        window.open(page, "kjdh", windowprops);
}

//This is the variable declaration for all java scripting on page

var defaultEmptyOK = false;

var dfltWarningOn = true;

//var digitsInPhoneNumber = 10;

var digits = "0123456789";

var visadigits = " 0123456789";

var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz";

var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

var whitespace = " \t\n\r";

var pEntryPrompt = "Please enter a ";

var pPhone = "10 digit phone number (like 403 555 1212). If it does not meet this format, use the comments.";

var pEmail = "valid email address (like name@domain.com). ";

var iCity = "You must select a valid city from this list.";

var iPhone = "This field must be a valid 10 digit phone number (like 403 555 1212). Please re-enter it now.  If your phone number does not follow this pattern, enter it in the comments.";

var iEmail = "This field must be a valid email address (like name@domain.com). Please re-enter it now.";

var sLastName = "Last Name";

var sFirstName = "First Name";

var sAddress = "Address";

var sCity = "City";

var sHomePhone = "Home Phone Number";

var sEmail = "Your Email";

var sGraduation = "The year(s) you graduated";

var sCollege = "The college(s) you were in";

var sDegree = "The degree(s), diploma(s) and/or certificate(s) you received";

var sCardNumber = "Credit Card Number";

var sCardExp = "Expiry Date";

var sCountry = "Country";

var phoneNumberDelimiters = "()- ";

var mWork = "One of the required fields has not been entered. Please fill in all fields marked with an asterisk (*) now. Additionally, Phone must be a 10 digit Canadian phone number (like 403 555 1212) or E-mail must be a valid e-mail address (like name@domain.com).";
var check = false;
var totalprice  = 0.00;
var isIE = true;
var todayDate = new Date();



//MY FUNCTIONS

function showModal(s)
{
	if(navigator.appName == "Microsoft Internet Explorer")
  {
  	if(s == "gold")
			window.showModalDialog('gold.html','gold','dialogHeight:240px;dialogWidth:240px;center:yes;help:no;resizable:no;scroll:no;status:no;unadorned:yes');
		else if (s == "wood")
			window.showModalDialog('wood.html','wood','dialogHeight:240px;dialogWidth:240px;center:yes;help:no;resizable:no;scroll:no;status:no;unadorned:yes');
		else if (s == "briar")
			window.showModalDialog('briar.html','briar','dialogHeight:240px;dialogWidth:240px;center:yes;help:no;resizable:no;scroll:no;status:no;unadorned:yes');
		else if (s == "diplomat")
			window.showModalDialog('diplomat.html','diplomat','dialogHeight:240px;dialogWidth:240px;center:yes;help:no;resizable:no;scroll:no;status:no;unadorned:yes');
	}
	else
	{
		if(s == "gold")
			openWindow('gold.html','gold','240','230','200','200');
		else if (s == "wood")
			openWindow('wood.html','wood','240','230','200','200');
		else if (s == "briar")
			openWindow('briar.html','briar','240','230','200','200');
		else if (s == "diplomat")
			openWindow('diplomat.html','diplomat','240','230','200','200');
	}
}

function runYes(form)
{
	form.submit();
}

function isEmpty(s)
{
	return ((s == null) || (s.length == 0))
}

function checkString (theField)
{
	//Wrapper class. Returns True if theField contains data.
	if (isWhitespace(theField.value))
		return false;
	else
		return true;
}

function isWhitespace (s)
{
	//If s has any non whitespace characters in it a value of false will be returned
	var i;

  if (isEmpty(s)) return true;

	for (i = 0; i < s.length; i++)
  {
   	var c = s.charAt(i);
    if (whitespace.indexOf(c) == -1) return false;
	}
	
	return true;
}

function isInteger (s)
{
	var i;
  for (i = 0; i < s.length; i++)
  {
   	// Check that current character is number.
   	var c = s.charAt(i);
    if (!isDigit(c))
      return false;
  }
  // All characters are numbers.
  return true;
}

function isDigit (c)
{
	return ((c >= "0") && (c <= "9"))
}

function isDate(s)
//check all places and make sure they are digits
{
	var i;
	var word = s.value;

	for (i = 0; i < word.length; i++)
	{
		if((i==2)||(i==5)||(i==8))
			;
		//Small piece of logic to make sure the month falls in the range of 01-12
		else if(i==3)
		{
			if(word.charAt(i) == "0")
				;
			else if(word.charAt(i) == "1")
			{
				if((word.charAt(i+1) == "1")||(word.charAt(i+1) == "2") ||(word.charAt(i+1) == "0"))
					;
				else
					return false;
			}
			else
				return false;
		}
		else
		{
			if(isDigit(word.charAt(i)))
				;
			else
				return false;
		}
	}
	return true;
}

//ensure the field is 16 characters long and all are digits
function isCardNumber(s)
{
	var i;
	var string = s.value;


	if(string.length != 16)
		return false;
	else
	{
		for (i=0; i<string.length; i++)
		{
			if(isDigit(string.charAt(i)))
				;
			else
				return false;
		}
	}
	return true;
}

//ensure expiration date on card is legitimate
function expireDate(s)
{
	var month = todayDate.getMonth() + 1;
	var year = takeYear(todayDate);
	var string = s.value;

}

function takeYear(theDate)
{
	x = theDate.getYear();
	var y = x % 100;
	y += (y < 38) ? 2000 : 1900;
	return y;
}

function isEmail (s)
{
	var i = 1;
  var sLength = s.length;

  //if nothing entered in email field return false

	if(isEmpty(s))
		return false;

  if(isWhitespace(s))
   	return false;

  //Start looking for the "@" symbol
  while ((i < sLength) && (s.charAt(i) != "@"))
  {
  	i++
  }

  if ((i >= sLength) || (s.charAt(i) != "@"))
   	return false;
  else
   	i += 1;

  // look for .
  while ((i < sLength) && (s.charAt(i) != "."))
  {
  	i++
  }

  // there must be at least one character after the .
  if ((i >= sLength - 1) || (s.charAt(i) != "."))
   	return false;
	else
	 	return true;
}

function checkEmail(theField)
{
	if(!isEmail(theField.value))
		return false;
	else
		return true;
}

function checkValidPhone (theField)
{
	var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters);
  if (!isValidPhoneNumber(normalizedPhone))
	{
		//alert("This field must be a valid 10 digit phone number (like 403 555 1212). Please re-enter it now.");
    return false;
	}
  else
  {
  	theField.value = reformatValidPhone(normalizedPhone);
    return true;
  }
}

function reformatValidPhone (Phone)
{
	if (Phone.length < 10)
	  return (reformat (Phone, "", 3, "-", 4))
	else
		return (reformat (Phone, "(", 3, ") ", 3, "-", 4))
}

function reformat (s)
{
	var arg;
  var sPos = 0;
  var resultString = "";

  for (var i = 1; i < reformat.arguments.length; i++)
  {
  	arg = reformat.arguments[i];
		if (i % 2 == 1)
		{
			resultString += arg;
		}
	  else
	  {
	  	resultString += s.substring(sPos, sPos + arg);
	    sPos += arg;
		}

  }
  return resultString;
}

function stripCharsInBag (s, bag)
{
	var i;
  var returnString = "";

  // Search through string's characters one by one.
  // If character is not in bag, append to returnString.

  for (i = 0; i < s.length; i++)
  {
    // Check that current character isn't whitespace.
    var c = s.charAt(i);
    if (bag.indexOf(c) == -1) returnString += c;
  }
  return returnString;
}

function isValidPhoneNumber (s)
{
	if (isEmpty(s))
		return false;
	else
	  return (isInteger(s) && ((s.length == 10) || (s.length == 7)))

}


function quickWarn(theField)
{
	if(!isEmail(theField.value))
	{
		alert("You must enter a valid email address (like name@domain.com)");
		return false;

	}
	else
		return true;
}

function setFocus(form)
{

	form.elements["email_address"].value='';
	form.elements["email_address"].focus();

}


function runClear(form)
{
	form.elements["pickUpDay"].value = '';
	
	return true;
}

function enoughInfo(form)
{
	var requiredString = "";
	var invalidString = "";
	var warningMsg = "";
	var focusSet = false;
	var InvalidFound = false;
	var requiredFound = false;

	if(!checkString(form.elements["question"]))
	{
		requiredString += " The skill testing question.";
		form.question.focus();
		focusSet = true;
		requiredFound = true;

	}
	
	// If the name field is empty, then we want to verify against first name and last name
	if (isEmpty(form.elements["Name"]))
	{
		if(!checkString(form.elements["first_name"]))
		{
			requiredString += " First Name.";
			form.first_name.focus();
			focusSet = true;
			requiredFound = true;
		}
		
		if(!checkString(form.elements["last_name"]))
		{
			requiredString += " Last Name.";
			form.last_name.focus();
			focusSet = true;
			requiredFound = true;
		}
		
		
	}
	else
	{
		
		if(!checkString(form.elements["Name"]))
		{
			requiredString += " Full Name.";
			form.Name.focus();
			focusSet = true;
			requiredFound = true;
		}
	}


	if(!checkString(form.elements["from_email"]))
	{
		requiredString += " Email address.";
		requiredFound = true;
		if(!focusSet)
		{
			form.from_email.focus();
			focusSet = true;
		}
	}
	else if(!checkEmail(form.elements["from_email"]))
	{
		invalidString += " You must enter a valid email address (like name@domain.com).";
		InvalidFound = true;
		if(!focusSet)
		{
			form.from_email.focus();
			focusSet = true;
		}
	}
	
	if(!checkString(form.elements["degree"]))
	{
		requiredString += " Degree.";
		form.degree.focus();
		focusSet = true;
		requiredFound = true;

	}
	
	if(!checkString(form.elements["phone_res"]))
	{
		requiredString += " Residential Phone.";
		requiredFound = true;
		if(!focusSet)
		{
			form.phone_res.focus();
			focusSet = true;
		}
	}
	else if(!checkValidPhone(form.elements["phone_res"]))
	{
		invalidString += " The residential phone number must be a valid 7 or 10 digit phone number.";
		InvalidFound = true;
		if(!focusSet)
		{
			form.phone_res.focus();
			focusSet = true;
		}
	}

	if(!checkString(form.elements["phone_bus"]))
	{
		requiredString += " Buisness Phone.";
		requiredFound = true;
		if(!focusSet)
		{
			form.phone_bus.focus();
			focusSet = true;
		}
	}
	else if(!checkValidPhone(form.elements["phone_bus"]))
	{
		invalidString += " The business phone number must be a valid 7 or 10 digit phone number.";
		InvalidFound = true;
		if(!focusSet)
		{
			form.phone_bus.focus();
			focusSet = true;
		}
	}
	
	if(!checkString(form.elements["Address"]))
	{
		requiredString += " Street Address.";
		requiredFound = true;
		if(!focusSet)
		{
			form.Address.focus();
			focusSet = true;
		}
	}



	if(requiredFound || InvalidFound)
	{

		if(requiredFound)
		{
			warningMsg = "The following required fields were left blank or not selected: " + requiredString;
			warningMsg += invalidString;
			alert(warningMsg);
			return false;
		}
		else
		{
			alert(invalidString);
		}
	}
	else
	{
		return true;
	}
}

function openWindow(url, name, w, h)
{
	popupWin = window.open(url, name, 'width='+ w + ',height=' + h);

}