
function checkBrowser(){
  this.ver=navigator.appVersion;
  this.dom=document.getElementById?1:0;
  this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;
  this.ie4=(document.all && !this.dom)?1:0;
  this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0;
  this.ns4=(document.layers && !this.dom)?1:0;
  this.bw=(this.ie5 || this.ie4 || this.ns4 || this.ns5);
  return this;
}
bw=new checkBrowser();

//————————————————————————————————————————————————————————————————————————————————————————————————————————

function makeObj(obj,nest){
    nest=(!nest) ? '':'document.'+nest+'.';
  this.el=bw.dom?document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?eval(nest+'document.'+obj):0;
  if(this.el){
    this.isObject = true;
    this.css=bw.dom?document.getElementById(obj).style:bw.ie4?document.all[obj].style:bw.ns4?eval(nest+'document.'+obj):0;
    this.scrollHeight=bw.ns4?this.css.document.height:this.el.offsetHeight;
    this.obj = obj + "Object";
    eval(this.obj + "=this");
    return this;
  } else {
    return this.isObject = false;
  }
}

//————————————————————————————————————————————————————————————————————————————————————————————————————————

function NewWindow(mypage, myname, w, h, scroll) 
{
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;

  winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable';
  win = window.open(mypage, myname, winprops);
  
  if (parseInt(navigator.appVersion) >= 4)
  {
    win.window.focus();
  }
}

//————————————————————————————————————————————————————————————————————————————————————————————————————————

function goToQuickLink(oSelect)
{
  if (oSelect.options[oSelect.selectedIndex].value != "")
  {
    window.location.href = oSelect.options[oSelect.selectedIndex].value;
  }
}

//————————————————————————————————————————————————————————————————————————————————————————————————————————

function showHide(arrShow, arrHide)
{
  for (i=0; i < arrShow.length; i++)
  {
    oEl=new makeObj(arrShow[i]);
  
    if (oEl.isObject){
      oEl.css.display='inline';
    }
  }

  for (i=0; i < arrHide.length; i++)
  {
    oEl=new makeObj(arrHide[i]);
  
    if (oEl.isObject){
      oEl.css.display='none';
    }
  }
}

//————————————————————————————————————————————————————————————————————————————————————————————————————————

function AdesValidater(arrMandatory,form)
{
	if (form)
	{
		var oForm = document.forms[form];
	}
	else
	{
		var oForm = document.forms[0];
	}

	var oElement;
	var ok = false;

	for (i=0; i < arrMandatory.length; i++)
	{
		oElement = oForm.elements[arrMandatory[i]];

		if (oElement)
		{
			if (oElement.value == "") {
				alert("You have not completed a mandatory field");
				if (oElement.type != "hidden" && oElement.focus) oElement.focus();
				ok = false;
			} else if (oElement.type == "checkbox") {
				if (oElement.checked == false) {
					alert("You have not completed a mandatory field");
					if(oElement.focus) oElement.focus();
					ok = false;
				}			
			} else if (typeof(checkRadios(arrMandatory[i])) == "object"){
				var radiogroup = checkRadios(arrMandatory[i]);
				var itemchecked = false;
				for(var j = 0 ; j < radiogroup.length ; ++j) {
					if(radiogroup[j].checked && itemchecked == false) {
						itemchecked = true;
						ok = true;
					}
				}
				if(!itemchecked) { 
					alert("You have not completed a mandatory field");
					if(radiogroup[0].focus) radiogroup[0].focus();
					ok = false;
				}
			//} else if (oElement.type == "select-one" || oElement.type == "select-multiple"){
				//select list
			} else if (oElement.name.toLowerCase() == "email" || oElement.name.toLowerCase() == "e-mail" || oElement.name.toLowerCase() == "email_address" || oElement.name.toLowerCase() == "e-mail_address" || oElement.name.toLowerCase() == "work_e-mail"){
				var goodEmail = oElement.value.match(/\b((\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
				if (goodEmail) {
					ok = true;
				} else {
					alert("Invalid email address.");
					if(oElement.focus) oElement.focus();
					ok = false;
				}
			} else if (oElement.name.toLowerCase() == "website"){
				if (oElement.value == "" || oElement.value == "http://") {
					alert("Please enter a website address");
					if(oElement.focus) oElement.focus();
					ok = false;
				}
			} else if (oElement.name.toLowerCase() == "cpassword"){
				if (oForm.elements["password"].value != oForm.elements["cpassword"].value){
					alert("Password mismatch, please re-enter.");
					if(oElement.focus) oElement.focus();
					ok = false;
				}
			
			// add custom form valitation here

				// Password restrictions
				} else if (oElement.name.toLowerCase() == "password" || oElement.name.toLowerCase() == "p" || oElement.name.toLowerCase() == "pwd"){
					
					if (oElement.value.length > 4) {
						ok = true;
					} else {
						alert("Please enter a minimum of 5 characters for the password");
						if(oElement.focus) oElement.focus();
						ok = false;
					}
				// end Password restrictions
				
				// Proctor & Gamble Managing GIO
				} else if (oElement.name.toLowerCase() == "gmc_number"){
					var goodGMC = oElement.value.match(/^\d{7}/);
					if (goodGMC) {
						ok = true;
					} else {
						alert("Invalid GMC Number.");
						if(oElement.focus) oElement.focus();
						ok = false;
					}
				// end Proctor & Gamble Managing GIO

				// Phone numbers
				} else if (oElement.name.toLowerCase() == "telephone" || oElement.name.toLowerCase() == "phone" || oElement.name.toLowerCase() == "tel" || oElement.name.toLowerCase() == "contact_number"){
					
					if (checkInternationalPhone(oElement.value)) {
						ok = true;
					} else {
						alert("Please enter a valid Phone Number");
						if(oElement.focus) oElement.focus();
						ok = false;
					}
				// end Phone numbers

			// end custom form validation section
			
			} else if (oElement.value != "") {
				ok = true;
			}

			if (ok == false){
				return false;
			}
		}
		else
		{
			alert("invalid element name");
			return false;
		}
	}

	if (confirm("All mandatory fields are complete.\n\nSubmit now?")) {
		return true;
	} else {
		return false;
	}
}

//—————————————————————————————————————

function checkRadios(radioGroupName) {
	var el = document.forms[0].elements;
	for(var i = 0 ; i < el.length ; ++i) {
		if(el[i].type == "radio" && el[i].name == radioGroupName) {
			return el[el[i].name]; // get the whole set of radio buttons.
		} else {
		}
   }
}

//—————————————————————————————————————

// Phone Numbers \\

var digits = "0123456789";
var phoneNumberDelimiters = "()- "; // non-digit characters which are allowed in phone numbers
var validWorldPhoneChars = phoneNumberDelimiters + "+"; // characters which are allowed in international phone numbers (a leading + is OK)
var minDigitsInIPhoneNumber = 10; // Minimum no of digits in an international phone no.

function isInteger(s)
{   
	var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone)
{
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

//————————————————————————————————————————————————————————————————————————————————————————————————————————
