/****js include*****/

/****get url value****/
/****form validation required, phone, email, radio, checkbox******/
/****date validation *****/
/****popup centered values need to be passed in the following order
	required variable url 
	optional variable 
	width default 400, 
	height default 400, 
	name default popup, 
	fullscreen default no, 
	scrollbars default no, 
	toolbar default no, 
	menubar default no, 
	resizable default no
****/
/****call syntax function - fill in form values and field names
function checkForm(theForm) {
	if (!checkRequired (theForm.elName, 'fieldName') ) return false;
	if (!checkEmail(theForm.elName, 'fieldName') ) return false;
	if (!checkPhone(theForm.elName, 'fieldName') ) return false;
	if (checkRadio(theForm.elName, 'fieldName') == "") return false;
	if (!checkDate(theForm.elName, 'fieldName') ) return false;
	return true;
}
place onsubmit in form opening tag
onsubmit="return checkForm(this);"
*****************************************************************************************************/



function include_dom(script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}

include_dom('/includes/js/validate_date.js');

function checkVar() {
    var thanksPage = getValue("thanks");
    if (thanksPage != "") {
        document.getElementById('offer_form').style.display = "none";
        document.getElementById('consent').style.display = '';
        document.getElementById('details').style.display = 'none';
    }
}

function getValue(varname) {
    var url = window.location.href;
    var qparts = url.split("?");

    if (qparts.length == 0) {
        return "";
    }
    var query = qparts[1];
    var vars = query.split("&");
    var value = "";

    for (i=0;i<vars.length;i++) {
        var parts = vars[i].split("=");
        if (parts[0] == varname) {
            value = parts[1];
            break;
        }
    }
    value = unescape(value);
    value.replace(/\+/g," ");
    return value;
}



function checkRequired(string, field) {
    if (string.value.length == "") {
        alert(field + " is a required field.");
        string.focus();
        return false;
    }
    return true;
}



function checkPhone (string, field) {
    if (string.value == "") {
        alert("Please enter a " + field + ".");
        string.focus();
        return false;
    }
    else {
        var stripped = string.value.replace(/[\(\)\.\-\ ]/g, '');
        if (isNaN(parseInt(stripped))) {
            alert("Please enter a valid phone number.");
            string.focus();
            return false;

        }
        if (!(stripped.length == 10)) {
            alert("Make sure you included an area code.");
            string.focus();
            return false;
        }
    }
    string.value = string.value.replace(/[\(\)\.\-\ ]/g, '');
    return true;
}




function checkEmail (string, field) {
    if (string.value == "") {
        alert("Please enter an " + field + ".");
        string.focus();
        return false;
    }

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(string.value))) {
        alert("Please enter a valid " + field + ".");
        string.focus();
        return false;
    }
    else {
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
       if (string.value.match(illegalChars)) {
           alert("The " + field + " contains illegal characters.");
           string.focus();
           return false;
       }
    }
    return true;
}



function checkRadio(obj, field) {
	for (var i = 0; i < obj.length; i++) {
		if (obj[i].checked){ 
			return (obj[i].value);
		}
	}
	alert("Please select an option for " + field + ".");
	obj[0].focus();
	return "";
}


function checkSelect (obj, field) {
	if (obj.selectedIndex > 0)  return true;
	else {
		alert("Please select an option for " + field + ".");
		obj[0].focus();
		return false;
	}
}


function checkDate (obj, field) {
	if (isDate(obj.value) != true){
		error = isDate(obj.value);
		alert("Please enter a " + error + " for " + field + ".");
		obj.focus();
		return false;
	}
    return true;
}




/*function checkFileSize(obj) {
/*var axobj = new ActiveXObject("Scripting.FileSystemObject");
var fileObj = axobj.getFile(obj.value);
var fileSize = fileObj.size;
alert(fileSize + " bytes");
var maxFileSize = 1500000
var FormSizeLimit = 52428800
function checkFileSize() {
  var totalSize = 0;
  var htmlSize;
  var overLimit = false;
  for (j = 1; j <= nfiles; j++) {
    var himg = document.getElementById('himg' + j);
    var size = document.getElementById('size' + j);
    var fileSize = himg.fileSize ;

    fileSize = parseInt(fileSize);
    
    if (fileSize < 0) size.innerHTML = '-';
	else {
      htmlSize = formatSize(fileSize);
      if ( fileSize>maxFileSize ) {
        htmlSize += ' (over limit, ' + formatSize(maxFileSize) + ' max)'
        size.style.color = 'red';
        overLimit = true;
      } 
	  else size.style.color = '';
      size.innerHTML = htmlSize;
      totalSize += fileSize;
    }//if (fileSize < 0) {
  }//for (j = 1; j <= nfiles; j++)

  var htotalSize = document.getElementById('totalSize');
  
  if (totalSize > 0) htmlSize = formatSize(totalSize);
  else htmlSize = '-';
  
  if (totalSize > FormSizeLimit) {
    htotalSize.style.color = 'red';
    htmlSize += ' (over limit, ' + formatSize(FormSizeLimit) + ' max)';
    overLimit = true;
  }
  else htmlSize += ' (of ' + formatSize(FormSizeLimit) + ' max)';
  htotalSize.innerHTML = htmlSize;
  return overLimit;
}

function formatSize(size) {
	// < 1 MB
	if (size < 0x100000) return Math.round(size / 0x400) + "&nbsp;kB"; 
	// > 1 MB
	else return (Math.round((size / 0x100000) * 10) / 10) + "&nbsp;MB"; 
}*/
/*******************************************************Suporting Functions DO NOT EDIT*********************************************************************/
var dtCh = "/";
var minYear = new Date().getFullYear();
var maxYear = minYear + 10;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if ((c < "0") || (c > "9")) return false;
    }
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    for (i = 0; i < s.length; i++) {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31;
		if (i == 4 || i == 6 || i == 9 || i == 11) 
			this[i] = 30;
		if (i == 2) 
			this[i] = 29;
   }
   return this;
}
function isDate(dtStr) {
	var daysInMonth = DaysArray(12);
	var pos1 = dtStr.indexOf(dtCh);
	var pos2 = dtStr.indexOf(dtCh, pos1 + 1);
	var strMonth = dtStr.substring(0, pos1);
	var strDay = dtStr.substring(pos1 + 1, pos2);
	var strYear = dtStr.substring(pos2 + 1);
	strYr = strYear;
	
	if (strDay.charAt(0) == "0" && strDay.length > 1) 
		strDay=strDay.substring(1);
	if (strMonth.charAt(0) == "0" && strMonth.length > 1) 
		strMonth = strMonth.substring(1)
	for (var i = 1; i <= 3; i++)
		if (strYr.charAt(0) == "0" && strYr.length > 1) strYr=strYr.substring(1);

	month = parseInt(strMonth);
	day = parseInt(strDay);
	year = parseInt(strYr);

	if (pos1 == -1 || pos2 == -1)
		return "valid format MM/DD/YYYY, please use /";
		
	if (strMonth.length < 1 || month < 1 || month > 12)
		return "valid format MM/DD/YYYY";
		
	if (strDay.length < 1 || day < 1 || day > 31 || (month==2 && day > daysInFebruary(year)) || day > daysInMonth[month])
		return "valid day";

	if (strYear.length != 4 || year==0 || year < minYear || year > maxYear)
		return "valid 4 digit year between " + minYear + " and " + maxYear;

	if (dtStr.indexOf(dtCh,pos2+1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false)
		return "valid date";

	return true;
}