//////// DONE ON EVERY PAGE ////////

//Detect IE5.5+
version=0
if (navigator.appVersion.indexOf("MSIE")!=-1){
temp=navigator.appVersion.split("MSIE")
version=parseFloat(temp[1])
}
if (version>=5.5) {
browser = "IE";
} else {
browser = "nIE";
}

//////// FUNCTIONS ////////

function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=780,height=580,left = 20,top = 20');");
}

function changeDisplay(obj,dis) {
	document.getElementById(obj).style.display = dis;
}

function hide(obj) {
	document.getElementById(obj).style.display = 'none';
}

function show(obj) {
	document.getElementById(obj).style.display = 'block';
}

// whitespace characters
var whitespace = " \t\n\r";
// Check whether string s is empty.
function isEmpty(s) {
  return ((s == null) || (s.length == 0))
}
// Check if the string is only whitespace
function isWhitespace (s) {
  var i;
  // Is s empty?
  if (isEmpty(s)) return true;
  // Search through string's characters one by one
  // until we find a non-whitespace character.
  // When we do, return false; if we don't, return true.
  for (i = 0; i < s.length; i++) {
    // Check that current character isn't whitespace.
    var c = s.charAt(i);
    if (whitespace.indexOf(c) == -1) return false;
    }
  // All characters are whitespace.
  return true;
}

function toggleFields(id) {
  var z,inputs;
  z = document.getElementById(id);
  inputs = z.getElementsByTagName('input');
  textareas = z.getElementsByTagName('textarea');
  imgs = z.getElementsByTagName('img');
  selects = z.getElementsByTagName('select');
  //toggle input disability
  for(i=0;i<inputs.length;i++) {
    if (inputs[i].type == 'checkbox') continue;
    if (inputs[i].disabled == true) {
      inputs[i].disabled = false;
      inputs[i].style.backgroundColor = "#ffffff";
    } else {
      inputs[i].disabled = true;
      inputs[i].style.backgroundColor = "#cccccc";
    }
  }
  //toggle text area disability
  for(i=0;i<textareas.length;i++) {
    if (textareas[i].disabled == true) {
      textareas[i].disabled = false;
      textareas[i].style.backgroundColor = "#ffffff";
    } else {
      textareas[i].disabled = true;
      textareas[i].style.backgroundColor = "#cccccc";
    }
  }
  //toggle select disability
  for(i=0;i<selects.length;i++) {
    if (selects[i].disabled == true) {
      selects[i].disabled = false;
      selects[i].style.backgroundColor = "#ffffff";
    } else {
      selects[i].disabled = true;
      selects[i].style.backgroundColor = "#cccccc";
    }
  }
  //toggle images
  for(i=0;i<imgs.length;i++) {
    if (imgs[i].style.display == 'none') {
      imgs[i].style.display = 'inline';
    } else {
      imgs[i].style.display = 'none';
    }
  }
  //change the text color of the targetted container
  if (inputs[1].disabled == true) {
    z.style.color = "#cccccc";
  } else {
    z.style.color = "#000000";
  }
}

function enableFields(id) {
  var z,inputs;
  z = document.getElementById(id);
  inputs = z.getElementsByTagName('input');
  textareas = z.getElementsByTagName('textarea');
  imgs = z.getElementsByTagName('img');
  selects = z.getElementsByTagName('select');
  //turn on inputs
  for(i=0;i<inputs.length;i++) {
    //if (inputs[i].type == 'checkbox') continue;
    inputs[i].disabled = false;
    inputs[i].style.backgroundColor = "#ffffff";
  }
  //turn on text areas
  for(i=0;i<textareas.length;i++) {
    textareas[i].disabled = false;
    textareas[i].style.backgroundColor = "#ffffff";
  }
  //turn on selects
  for(i=0;i<selects.length;i++) {
    selects[i].disabled = false;
    selects[i].style.backgroundColor = "#ffffff";
  }
  //turn on images
  for(i=0;i<imgs.length;i++) {
    imgs[i].style.display = 'inline';
  }
  //change the text color of the targetted container
  z.style.color = "#000000";
}

function printConfirm() {
	print();
	//var c = confirm("If your application printed properly, click OK to send the data to SECB.  If there was a printing problem, please click Cancel.");
	//if (c) {
	//	document.passData.submit();
	//}		
}

function checkRadios() {
	var i,trig,grp,c,radioError;
	var missed = new Array();
	args=checkRadios.arguments;
	for (i=0; i<(args.length); i++) {
		trig = 0;
		grp = document.getElementsByName(args[i]);
		for (c=0; c<(grp.length); c++) {
			if (grp[c].checked == true) {
				trig = 1;
				continue;
			}
		}
		if (trig == 0) {
			missed[i] = args[i];
		}
	}
	radioError = "";
	for (i=0; i<(missed.length); i++) {
		if (missed[i] == "Salutation") {
			radioError += "- " + missed[i] + ' - Please select "Mr.", "Mrs.", or "Ms."\n';
		} else {
			radioError += "- " + missed[i] + ' - Please select "Yes" or "No"\n';
		}
	}
	return radioError;
}

function checkCW() {
}

function liveSum() {
	course = new Array("technicalWriting","structuralAnalysis","aStructuralAnalysis","ssbd","scbd","timberBD","masonry","structuralDynamics","smfd");
	t = 0;
	c = 0;
	cwValid = false;
	for (i=0; i<(course.length); i++) {
		thisInput = document.getElementById(course[i]);
		val = parseInt(thisInput.value)
		if (isNaN(val)) val = 0;
		t += val;
		if (val > 0) {
			c += 1;
		}
	}
	if (t < 36 || c < 6) {
		document.getElementById("cwTotal").style.backgroundColor = "red";
		cwValid = false;
	} else {
		document.getElementById("cwTotal").style.backgroundColor = "green";
		cwValid = true;
	}
	document.getElementById("cwTotal").innerHTML = "Total Hours: " + t + "</div><br />Total Course: " + c;
}

function ceLiveSum() {
errorColor = "#cc0000";
for (x=0;x<arguments.length;x++) {
	ttl = 0;
	boxes = document.getElementsByTagName('input');
	for (i=0;i<(boxes.length);i++) {
		if (boxes[i].className == arguments[x] && isEmpty(boxes[i].value) == false) {
			if (parseInt(boxes[i].value) != NaN) {
				ttl += parseInt(boxes[i].value);
			} else {
				boxes[i].value = '';
			}
		}
	}
	document.getElementById('ttl' + arguments[x]).style.color = "#000000";
	  
   if ((arguments[x] == 'A' || arguments[x] == 'E') && ttl > 15) {
     document.getElementById('ttl' + arguments[x]).style.color = errorColor;
     }
   if ((arguments[x] == 'B' || arguments[x] == 'F' || arguments[x] == 'H' || arguments[x] == 'K') && ttl > 5) {
     document.getElementById('ttl' + arguments[x]).style.color = errorColor;
     }
   if (arguments[x] == 'I' && ttl > 6) {
     document.getElementById('ttl' + arguments[x]).style.color = errorColor;
     }
   if (arguments[x] == 'J' && ttl > 4) {
     document.getElementById('ttl' + arguments[x]).style.color = errorColor;
     }
   if ((arguments[x] == 'C' || arguments[x] == 'D') && ttl > 8) {
     document.getElementById('ttl' + arguments[x]).style.color = errorColor;
     }
   document.getElementById('ttl' + arguments[x]).value = ttl;
 }
}

function ceLiveTextCheck(row) {
	f = document.forms['ceForm'];
	if (f.elements['MonthStart' + row].value == '00' || f.elements['YearStart' + row].value == '00' || isWhitespace(f.elements[row + ' Activity'].value) || isWhitespace(f.elements[row + ' Sponsoring Organization'].value)) {
		alert("Please supply a Date, Activity, and Sponsoring Organization for this row");
		f.elements[row + ' Activity'].focus();
	}
}

function ceFormValidate() {
	f = document.forms['ceForm'];
	if (isWhitespace(f.elements['Name'].value)) {
		alert("Please provide your name.");
		f.elements['Name'].focus();
		f.elements['Name'].select();
		return false;
	}
	if (isWhitespace(f.elements['Certification Number'].value) || !f.elements['Certification Number'].value.match("^([0-9]{4}-[0-9]{4})$")) {
		alert("Please provide your Certification Number in this format: \n XXXX-XXXX");
		f.elements['Certification Number'].focus();
		f.elements['Certification Number'].select();
		return false;
	}
	if (!f.elements['Phone'].value.match("^[0-9]*-?[0-9]{3}-[0-9]{3}-[0-9]{4}")) {
		alert("The Phone number field must have at least 10 numbers (Please include your area code) and contain no other characters besides a dash (-). Please enter it in this format:\n N-NNN-NNN-NNNN");
		f.elements['Phone'].focus();
		f.elements['Phone'].select();
		return false;
	}
	if (!f.elements['Email'].value.match("^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$")) {
		alert("This does not appear to be a valid Email address.");
		f.elements['Email'].focus();
		f.elements['Email'].select();
		return false;
	}
}
