﻿function checkContact(form, id) {
	if(!isFilled(form.elements["contact[email]"].value) && !isFilled(form.elements["contact[phone]"].value)) {
		document.getElementById(id).innerHTML = "Error. Please enter an email address or phone.";
		form.elements["contact[email]"].focus();
		return false;
	}
	if(isFilled(form.elements["contact[email]"].value) && !isEmail(form.elements["contact[email]"].value)) {
		document.getElementById(id).innerHTML = "Error. Please enter a valid email address.";
		form.elements["requester[email]"].focus()
		return false;
	}
	if(isFilled(form.elements["contact[phone]"].value) && !isPhone(form.elements["contact[phone]"].value)) {
		document.getElementById(id).innerHTML = "Error. Invalid phone number. Please use the following format: 555-555-5555.";
		form.elements["contact[phone]"].focus()
		return false;
	}
	if(!isFilled(form.elements["contact[message]"].value)) {
		document.getElementById(id).innerHTML = "Error. Please enter a brief message regarding your inquiry.";
		form.elements["contact[message]"].focus()
		return false;
	}
	if(!isFilled(form.elements["contact[verification_code]"].value)) {
		document.getElementById(id).innerHTML = "Error. Please the verification code shown to the right.";
		form.elements["contact[verification_code]"].focus()
		return false;
	}
	
	return true;
}

function isFilled(elm) {
	if (elm == "" || elm == "null") {
		return false;
	}
	return true;
}

function isFilled2(elm) {
	
	if (elm.value == "" || elm.value == "null") {
		return false;
	}
	return true;
}

function isInteger(intVal) {

	//var rx = /(^-*\d+$)|(^-*\d+\.\d+$)/
	var rx = /(^-*\.\d+$)|(^-*\d+$)|(^-*\d+\.\d+$)/
	return rx.test(intVal)
	
}

function windowopen(url) {
	
	var width = 975;
 	var height = 768;
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));
    var windowFeatures = "width=" + width + ",height=" + height + ",scrollbars,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
    window.open(url, "window", windowFeatures);

}

function isDate(str) {

	//var dateformat = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
	var dateformat = /^\d{1,2}(\/)\d{1,2}\1\d{4}$/
	return dateformat.test(str) //returns true or false depending on userinput
	
}

function dateLessThan(sd) {
	
	var dueDate = sd.split("/");
	var today = new Date();
	
	var dm = dueDate[0] - 1;
	var dd = dueDate[1];
	var dy = dueDate[2];
	
	var due = new Date(dy, dm, dd, 0, 0, 0, 0);
	
	today.setHours(0);
	today.setMinutes(0);
	today.setSeconds(0);
	today.setMilliseconds(0);
	
	return due >= today;

}

function dateLessThan2(sd, ed) {

	var stDt = sd.split("/");
	var eDt = ed.split("/");
	
	var dm = stDt[0] - 1;
	var dd = stDt[1];
	var dy = stDt[2];
	
	var s = new Date(dy, dm, dd, 0, 0, 0, 0);
	
	dm = eDt[0] - 1;
	dd = eDt[1];
	dy = eDt[2];
	
	var e = new Date(dy, dm, dd, 0, 0, 0, 0);
	
	return e > s;
	
}

function isTime(intVal) {

	var intTime = intVal.split(".");
	var strTime = intVal.toString();
	
	if(strTime.indexOf(".") == -1) {
		return true;
	}
	
	if(intTime[1] == 5 || intTime[1] == 0) {
		return true;
	}
	
	return intTime[1] % 25 == 0;

}

function isPhone(str) {

	var valid = "0123456789-"
	var temp;
	
	if(str.length != 12) {
	
		return false;
		
	}
	
	for (var i=0; i < str.length; i++) {
		temp = "" + str.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
		
			return false;
		
		}
		
	}
	
	return true;
}

function isEmail(strVal) {

  var str = new String(strVal);
  var isOK = true;
  rExp = /[!\"£$%\^&*()-+=<>,\'#?\\|¬`\/\[\]]/
  
  if( rExp.test(str) )
    isOK = false;
  if( str.indexOf('.') == -1 || str.indexOf('@') == -1 )
    isOK = false;
  if( str.slice(str.lastIndexOf('.')+1,str.length).length < 2 )
    isOK = false;
  if( str.slice(0,str.indexOf('@')).length < 1 )
    isOK = false;
  if( str.slice(str.indexOf('@')+1,str.lastIndexOf('.')).length < 1 )
    isOK = false;

  return isOK;
}

function limitText(field, limitNum) {
	
	if (field.value.length <= limitNum) {
		return true;
	}
	else {
		alert("Error. You have reached the allowable character limit. Please limit text in this box to 700 characters");
		field.value = field.value.substring(0, limitNum);
		return false;
	}
}

function showHide(id) {
	var obj = document.getElementById(id);

	var status = obj.className;

	if (status == 'hide') {

		obj.className = 'show';

	}
	
	else {
		
		obj.className = 'hide';
	}
}

function showID(id) {
	var obj = document.getElementById(id);

	obj.className = 'show';

}

function hideID(id) {
	var obj = document.getElementById(id);

	obj.className = 'hide';

}

function showHideDisplay(id) {

	var obj = document.getElementById(id);
	
	var status = obj.style.display;

	if (status == '') {

		obj.style.display = 'none';
	}
	
	else {
		
		obj.style.display = '';
	}

}

function toggle(id) {
	var ele = document.getElementById(id);
	
	if(ele.style.display == "block") {
    		ele.style.display = "none";
  	}
	else {
		ele.style.display = "block";
	}
}