// JavaScript Document

// validate email
// how to use: onsubmit="return validateEmail(this)"
function validateEmail(form) {
	emailREGEX = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
//	phoneRe = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/
	for(var e=0; e < form.elements.length; e++){
		var el = form.elements[e];
		if(el.name=='email-field') {
			if (!emailREGEX.test(el.value)) {
				alert('Please enter a valid E-mail address!');
				el.focus();
				return false;
			} // end if
		} // end if
	} // end for
} // end validateEmail



// place label inside input form
function initOverLabels () {
  if (!document.getElementById) return;  	

  var labels, id, field;

  // Set focus and blur handlers to hide and show 
  // LABELs with 'overlabel' class names.
  labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {
	
    if (labels[i].className == 'overlabel') {

      // Skip labels that do not have a named association
      // with another field.
      id = labels[i].htmlFor || labels[i].getAttribute('for');
      if (!id || !(field = document.getElementById(id))) {
        continue;
      }

      // Change the applied class to hover the label 
      // over the form field.
      labels[i].className = 'overlabel-apply';

      // Hide any fields having an initial value.
      if (field.value !== '') {
        hideLabel(field.getAttribute('id'), true);
      }

      // Set handlers to show and hide labels.
      field.onfocus = function () {
        hideLabel(this.getAttribute('id'), true);
      };
      field.onblur = function () {
        if (this.value === '') {
          hideLabel(this.getAttribute('id'), false);
        }
      };

      // Handle clicks to LABEL elements (for Safari).
      labels[i].onclick = function () {
        var id, field;
        id = this.getAttribute('for');
        if (id && (field = document.getElementById(id))) {
          field.focus();
        }
      };

    }
  }
};



function hideLabel (field_id, hide) {
  var field_for;
  var labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {
    field_for = labels[i].htmlFor || labels[i].getAttribute('for');
    if (field_for == field_id) {
//      labels[i].style.textIndent = (hide) ? '-1000px' : '0px';
      labels[i].style.display = (hide) ? 'none' : 'inline';
      return true;
    } // end if
  } // end for
}; // end function hideLabel



// function to check if form fields have been filled
function checkForm(form) {
	for (var e = 0; e < form.elements.length; e++) {
	var el = form.elements[e];
		if (el.value == '') {
			alert('A valid Username and Password is required!');
			el.focus();
			return false;
		} // end if
	} // end for
}; // end fucntion checkLogin


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

// function to check if form fields have been filled
function checkSearch(form) {
	for (var e = 0; e < form.elements.length; e++) {
	var el = form.elements[e];
	if(el.name == 'searchbox'){
		if (trim(el.value) == '') {
			alert('Keyword(s) is required!');
			el.focus();
			return false;
		} // end if
	} // end if
	} // end for
}; // end fucntion checkLogin


window.onload = function () {
  setTimeout(initOverLabels, 50);
};



// function to check if required fields have been filled
// how to use: onsubmit="return checkForm(this)"
function checkForm2(form) {
for(var e=0; e < form.elements.length; e++){
var el = form.elements[e];
var elvalue = el.value;
var fromvar = document.getElementById('from');
	switch(fromvar.value) {

		case "distributors": {
			if (el.name == 'name-field') {
				if(el.value == '') {
					alertDisplay(el);
					return false;
				} // end if
			} // end if

			if (el.name == 'company-field') {
				if(el.value == '') {
					alertDisplay(el);
					return false;
				} // end if
			} // end if

			if (el.name == 'message-field') {
				if(el.value == '') {
					alertDisplay(el);
					return false;
				} // end if
			} // end if

			// email validation
			if (el.name == 'email-field') {
				if(el.value == '') {
					alertDisplay(el);
					return false;
				} else { // end if
					emailREGEX = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
					if (!emailREGEX.test(el.value)) {
						alert('Please enter a valid E-mail address!');
						el.focus();
						return false;
					} // end if
				} // end else
			} // end if
			
		break;
		} // end case distributors
	
	
		case "contact": {
			if (el.name == 'name-field') {
				if(el.value == '') {
					alertDisplay(el);
					return false;
				} // end if
			} // end if

			if (el.name == 'message-field') {
				if(el.value == '') {
					alertDisplay(el);
					return false;
				} // end if
			} // end if

			// email validation
			if (el.name == 'email-field') {
				if(el.value == '') {
					alertDisplay(el);
					return false;
				} else { // end if
					emailREGEX = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
					if (!emailREGEX.test(el.value)) {
						alert('Please enter a valid E-mail address!');
						el.focus();
						return false;
					} // end if
				} // end else
			} // end if
			
		break;
		} // end case contact
	
	
		case "download": {
		
			// name validation
			if (el.name == 'name-field') {
				if(el.value == '') {
					alertDisplay(el);
					return false;
				} // end if
			} // end if
			
			
			// email validation
			if (el.name == 'email-field') {
				if(el.value == '') {
					alertDisplay(el);
					return false;
				} else { // end if
					emailREGEX = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
					if (!emailREGEX.test(el.value)) {
						alert('Please enter a valid E-mail address!');
						el.focus();
						return false;
					} // end if
				} // end else
			} // end if
			
			
			// country validation
			if(el.name == 'country-field') {
				if(el.options.selectedIndex==0){
					alertDisplay(el);
					return false;
				} // end if
			} // end if
			
		break;
		} // end case preferences
	} // end switch

} // end for
} // end checkArticleForm

// function to display alert
function alertDisplay(frmfield) {
	var str = frmfield.name.toUpperCase();
	alert(str.split('-',1)+' is required!');
	frmfield.focus();
} // end function alertDisplay

// check news archive section form
function checkNewsArchive(form) {
	var flag = 0;
	for(var e=0; e < form.elements.length; e++){
	var el = form.elements[e];
		
		// check search
		if(el.name == "search-field") { if(trim(el.value) == "") flag++; }
	
		// check category
		if(el.name == "category-field") { if(el.options.selectedIndex == 0) flag++; }
		
		// check date
		if(el.name == "date-field") { if(el.options.selectedIndex == 0) flag++; }
		
		// check language
		if(el.name == "language-field") { if(el.options.selectedIndex == 0) flag++; }
		
		// check author
		if(el.name == "author-field") { if(el.options.selectedIndex == 0) flag++; }
		
	} // end for
	
	if(flag>4){
		alert('You must at least type a SEARCH TEXT or SELECT AN OPTION before searching the News Archive!');
		return false;
	} // end if flag
} // end function checkNewsArchive

// two selects related
function dynamicSelect(id1, id2) {
	// Browser and feature tests to see if there is enough W3C DOM support
	var agt = navigator.userAgent.toLowerCase();
	var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	var is_mac = (agt.indexOf("mac") != -1);
	if (!(is_ie && is_mac) && document.getElementById && document.getElementsByTagName) {
		// Obtain references to both select boxes
		var sel1 = document.getElementById(id1);
		var sel2 = document.getElementById(id2);
		// Clone the dynamic select box
		var clone = sel2.cloneNode(true);
		// Obtain references to all cloned options 
		var clonedOptions = clone.getElementsByTagName("option");
		// Onload init: call a generic function to display the related options in the dynamic select box
		refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
		// Onchange of the main select box: call a generic function to display the related options in the dynamic select box
		sel1.onchange = function() {
			refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
		};
	}
}
function refreshDynamicSelectOptions(sel1, sel2, clonedOptions) {
	// Delete all options of the dynamic select box
	while (sel2.options.length) {
		sel2.remove(0);
	}
	// Create regular expression objects for "select" and the value of the selected option of the main select box as class names
	var pattern1 = /( |^)(select)( |$)/;
	var pattern2 = new RegExp("( |^)(" + sel1.options[sel1.selectedIndex].value + ")( |$)");
	// Iterate through all cloned options
	for (var i = 0; i < clonedOptions.length; i++) {
		// If the classname of a cloned option either equals "select" or equals the value of the selected option of the main select box
		if (clonedOptions[i].className.match(pattern1) || clonedOptions[i].className.match(pattern2)) {
			// Clone the option from the hidden option pool and append it to the dynamic select box
			sel2.appendChild(clonedOptions[i].cloneNode(true));
		}
	}
}
