// Javascript functions for error reporting using EPA's Integrated Error
// Correction Process (http://oaspub.epa.gov/enviro/ets_grab_error.smart_form).

// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return '';
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

function JSLGetValue(pctl, ptype, pfalse) {
   var i = 0;
   if (ptype == null) { return pctl.value; }
   if (ptype == "CHECK") {
      if (pctl.checked) { return pctl.value; }
      else { return pfalse; }
   }
   if (ptype == "RADIO") {
      for (i=0;i<pctl.length;i++) {
         if (pctl[i].checked) { return pctl[i].value; }
      }
      return "";
   }
   if (ptype == "LIST") {
      if (pctl.selectedIndex >= 0) {
         if (pctl.options[pctl.selectedIndex].value != "") {
            return pctl.options[pctl.selectedIndex].value;
         }
         // ML, 4/1/02: commented out to ensure Null value is passed along for p_affiliation type
         //else { return pctl.options[pctl.selectedIndex].text; }
         else { return "";}
      }
      else { return ""; }
   }
}

// If all required submitter fields are filled with valid data,
//   set cookie values and return true.
// else
//   focus on the bad field and return false.
function check_submitter_fields(formname)
{
	if (document[formname].p_first_name.value == "")
	{
		alert("Please enter your first name");
		document[formname].p_first_name.focus();
		return false;
	}
	if (document[formname].p_last_name.value == "")
	{
		alert("Please enter your last name");
		document[formname].p_last_name.focus();
		return false;
	}
	if (document[formname].p_email.value == "" & document[formname].p_phone.value == "")
	{
		alert("Please enter your E-mail and/or Phone number");
		document[formname].p_email.focus();
		return false;
	}
	if (document[formname].p_phone.value != "")
	{
		if (document[formname].p_area_code.value == "")
		{
			alert("Please enter the complete phone number");
			document[formname].p_area_code.focus();
			return false;
		}
		if (document[formname].p_area_code.value.length != 3)
		{
			alert("Please enter the complete area code");
			document[formname].p_area_code.focus();
			return false;
		}
		if (document[formname].p_phone.value.length < 7)
		{
			alert("Please enter the complete phone number");
			document[formname].p_phone.focus();
			return false;
		}
		var  locphstr ;
            var  locphstr1 = "";
            var  notallow = "~`!@#$%^&(){}+=_*,<>." ;
            var  donotadd ;
		locphstr = document[formname].p_area_code.value + document[formname].p_phone.value ;
		for (i = 0;  i < locphstr.length;  i++)
  		{
    		  ch = locphstr.charAt(i);
              donotadd = false ;
              for (j=0 ; j < notallow.length ; j++)
		    {
              if ( ch == notallow.charAt(j) )
				{
					   donotadd = true ;
					   break ;
				  }
                 }
               if ( donotadd ){
						alert("Invalid characters in Phone Number");
						document[formname].p_area_code.focus();
								return false;
				}
		   else
				{
				if (i < 3 ){
						if( ch == "-" || ch == " ")
							{ alert("Invalid Area Code");
								document[formname].p_area_code.focus();
								return false;
							}
                                    else {
							locphstr1 = locphstr1 + ch ;
							}
					      }
                        else {
					if( ch == "-" || ch == " "){locphstr1 = locphstr1 ;}
						else{
				     locphstr1 = locphstr1 + ch ;
						    }
                             }
				}
             }
		if ( locphstr1.length != 10 )
			{ alert("Invalid Phone number" + " "+ locphstr1);
			document[formname].p_phone.focus();
			return false;
			}
	}

	// test if valid email address, must have @ and .
	if (document[formname].p_email.value != "")
	{
  		var checkEmail = "@.";
  		var checkStr = document[formname].p_email.value;
  		var EmailValid = false;
  		var EmailAt = false;
  		var EmailPeriod = false;
  		for (i = 0;  i < checkStr.length;  i++)
  		{
    		ch = checkStr.charAt(i);
    		for (j = 0;  j < checkEmail.length;  j++)
    		{
      			if (ch == checkEmail.charAt(j) && ch == "@")
        			EmailAt = true;
      			if (ch == checkEmail.charAt(j) && ch == ".")
        			EmailPeriod = true;
          		if (EmailAt && EmailPeriod)
                	break;
          		if (j == checkEmail.length)
                	break;
        	}
        	// if both the @ and . were in the string
    		if (EmailAt && EmailPeriod)
    		{
                EmailValid = true
                break;
        	}
  		}
  		if (!EmailValid)
  		{
    		alert("Please enter a valid e-mail. It must contain an \"@\" and a \".\".");
    		document[formname].p_email.focus();
    		return false;
		}
  	}

	// check for pref fields in sync.
	if (JSLGetValue(document[formname].p_pref_cnt_mthd, "RADIO", false) == "email") {
		if (document[formname].p_email.value == "") {
 			alert("Please enter your E-mail address if your preferred contact method is E-mail");
			document[formname].p_email.focus();
			return false;
		}
	}
	else {
		if (document[formname].p_phone.value == "") {
			alert("Please enter your phone number if your preferred contact method is Phone");
			document[formname].p_area_code.focus();
			return false;
		}
	}

	// set cookie values from form fields
	setCookie("INFO_SRC_FNAME",    document[formname].p_first_name.value);
	setCookie("INFO_SRC_LNAME",    document[formname].p_last_name.value);
	setCookie("INFO_SRC_EMAIL",    document[formname].p_email.value);
	setCookie("INFO_SRC_ARCODE",   document[formname].p_area_code.value);
	setCookie("INFO_SRC_PHNUM",    document[formname].p_phone.value);
	setCookie("INFO_SRC_EXT",      document[formname].p_phone_ext.value);
	setCookie("INFO_SRC_ORG",      document[formname].p_organization.value);
	setCookie("INFO_SRC_PREFCONT", JSLGetValue(document[formname].p_pref_cnt_mthd, "RADIO", false));
	setCookie("INFO_SRC_AFFL",     JSLGetValue(document[formname].p_affiliation, "LIST", false));

	return true;
}

// Return true iff comments field is non-empty.
function check_comments_field(formname)
{
	if (document[formname].p_comments.value == "")
	{
		alert("Please enter a description of the error.");
		document[formname].p_comments.focus();
		return false;
	}
	return true;
}

function fill_fields(formname)
{
	// fill form values from cookie
	document[formname].p_first_name.value    = getCookie("INFO_SRC_FNAME");
	document[formname].p_last_name.value     = getCookie("INFO_SRC_LNAME");
	document[formname].p_email.value         = getCookie("INFO_SRC_EMAIL");
	document[formname].p_area_code.value     = getCookie("INFO_SRC_ARCODE");
	document[formname].p_phone.value         = getCookie("INFO_SRC_PHNUM");
	document[formname].p_phone_ext.value     = getCookie("INFO_SRC_EXT");
	document[formname].p_organization.value  = getCookie("INFO_SRC_ORG");
	document[formname].p_pref_cnt_mthd.value = getCookie("INFO_SRC_PREFCONT");
	document[formname].p_affiliation.value   = getCookie("INFO_SRC_AFFL");

	// focus on next field to be filled
	if (document[formname].p_first_name.value == '')
		document[formname].p_first_name.focus();
	else if (document[formname].p_last_name.value == '')
		document[formname].p_last_name.focus();
	else if (JSLGetValue(document[formname].p_pref_cnt_mthd, "RADIO", false) == "email"
		&& document[formname].p_email.value == "")
		document[formname].p_email.focus();
	else if (JSLGetValue(document[formname].p_pref_cnt_mthd, "RADIO", false) == "phone"
		&& document[formname].p_phone.value == "")
		document[formname].p_phone.focus();
//	else if (JSLGetValue(document[formname].p_affiliation, "LIST", false) == "")
//		document[formname].p_affiliation.focus();
//	else if (document[formname].p_organization.value == '')
//		document[formname].p_organization.focus();
	else
		document[formname].p_comments.focus();
}

var resp , resp1, resp2, resp3,resp4, resp5, resp6, resp7, resp8;
resp = getCookie("INFO_SRC_FNAME");
resp1 = getCookie("INFO_SRC_LNAME");
resp2 = getCookie("INFO_SRC_EMAIL");
resp3 = getCookie("INFO_SRC_ARCODE");
resp4 = getCookie("INFO_SRC_PHNUM");
resp5 = getCookie("INFO_SRC_EXT");
resp6 = getCookie("INFO_SRC_ORG");
resp7 = getCookie("INFO_SRC_PREFCONT");
resp8 = getCookie("INFO_SRC_AFFL");

