// JavaScript Document
function WriteOffset()
{
	// Get the user utcoffset
	var sField;
	var utcoffset = (new Date()).getTimezoneOffset()/60;
	sField = "<input type='hidden' name='utcoffset' id='utcoffset' value='"+utcoffset+"' />";
	document.write(sField);
}

function displayDiv(divId)
{
	document.getElementById(divId).style.display='block';
	//document.getElementById( 'rewRew1' ).style.display = 'block';
}

function hideDiv(divId)
{
	document.getElementById(divId).style.display='none';
}
function handleProblem()
{
	var iSelected;
	iSelected = document.getElementById('cboSubject').selectedIndex;
	if (iSelected==3)
	{
		displayDiv('problem');
	}
	else
		hideDiv('problem');
	
}
function handleOp()
{
	var iSelected;
	iSelected = document.getElementById('cboOperatingSystem').selectedIndex;
	if (iSelected==13)
	{
		displayDiv('OtherOp');
	}
	else
		hideDiv('OtherOp');
}

function validateEmailForm(frm)
{

//check name
if((document.getElementById('txtName').value=="")||(document.getElementById('txtName').value.length<3))
	{
		alert("Please enter your name");
		document.frm.txtName.focus();
		document.frm.txtName.select();
		return false;
	}
	
//check email	
var email = document.getElementById('txtEmail').value;
if(isValidEmail(email)==false)
	{
		alert("Please enter a valid email address");
		document.frm.txtEmail.focus();
		document.frm.txtEmail.select();
		return false;
	}	
//make sure subject was selected

if (document.getElementById('cboSubject').selectedIndex==0)
	{
		alert("Please select the subject of your email");
		document.frm.cboSubject.focus();
		return false;
	}
//if problem is selected make sure other appropriate fields are populated
if (document.getElementById('cboSubject').selectedIndex==3)
{
	//check for date
	if(document.getElementById('txtDateOfProblem').value=="")
	{
		alert("Please enter the approximate time/date of your problem");
		document.frm.txtDateOfProblem.focus();
		document.frm.txtDateOfProblem.select();
		return false;
	}
	//validate the date
	var dtDate = document.getElementById('txtDateOfProblem').value;
	if(isValidDate(dtDate)==false)
	{
		//alert("Please enter a valid date");
		document.frm.txtDateOfProblem.focus();
		document.frm.txtDateOfProblem.select();
		return false;
	}	
	
	
	//check operating system
	if (document.getElementById('cboOperatingSystem').selectedIndex==0)
	{
		alert("Please select the operating system you are using");
		document.frm.cboOperatingSystem.focus();
		return false;
	}
	//check for other operating system selection
	if (document.getElementById('cboOperatingSystem').selectedIndex==12)
	{
		//check that other box was populated
		if(document.getElementById('txtOtherOperatingSystem').value=="")
		{
			alert("Please enter the operating system you are using.");
			document.frm.txtOtherOperatingSystem.focus();
			document.frm.txtOtherOperatingSystem.select();
			return false;
		}
	}
	//check internet connection
	if (document.getElementById('cboInternetConnection').selectedIndex==0)
	{
		alert("Please select what type of internet connection you are using.");
		document.frm.cboInternetConnection.focus();
		return false;
	}
	
}

//check for comments
if(document.getElementById('txtDescription').value=="")
		{
			alert("Please enter your comments.");
			document.frm.txtDescription.focus();
			document.frm.txtDescription.select();
			return false;
		}


}
function isValidEmail(str) {
  var at="@"
  var dot="."
  var lat=str.indexOf(at)
  var lstr=str.length
  var ldot=str.indexOf(dot)
  if (str.indexOf(at)==-1){
     return false
  }
  if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
     return false
  }
  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
      return false
  }
   if (str.indexOf(at,(lat+1))!=-1){
      return false
   }
   if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
      return false
   }
   if (str.indexOf(dot,(lat+2))==-1){
      return false
   }
   if (str.indexOf(" ")!=-1){
      return false
   }
   return true          
}

//validate date
function isValidDate(dateStr) {
// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
// Also separates date into month, day, and year variables

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

// To require a 4 digit year entry, use this line instead:
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("Date is not in a valid format.")
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
   }
}
return true;  // date is valid
}


