//SEARCH BAR CLEAR FUNTION
function clear_text(object) {
	if (object.value=="Search") {
		object.value = "";
		object.className='search';
		object.focus();
	}
}



//SET ALL EXTERNAL LINKS TO OPEN IN A BLANK WINDOW AND HAVE THE icon-offsite CSS CLASS
this.blankwin = function(){
	var hostname = window.location.hostname;
	hostname = hostname.replace("www.","").toLowerCase();
	var a = document.getElementsByTagName("a");	
	this.check = function(obj){
		var href = obj.href.toLowerCase();
		return (href.indexOf("http://")!=-1 && href.indexOf(hostname)==-1) ? true : false;				
	};
	this.set = function(obj){
		obj.target = "_blank";
		obj.className = "icon-offsite";
	};	
	for (var i=0;i<a.length;i++){
		if(check(a[i])) {
			set(a[i]);
		}
	};		
};
//script initiates on page load. 
this.addEvent = function(obj,type,fn){
	if(obj.attachEvent){
		obj['e'+type+fn] = fn;
		obj[type+fn] = function(){obj['e'+type+fn](window.event );}
		obj.attachEvent('on'+type, obj[type+fn]);
	} else {
		obj.addEventListener(type,fn,false);
	};
};
addEvent(window,"load",blankwin);



// CALCULATE THE PATH
pathis = location.pathname;
if (pathis.lastIndexOf(".") > 1) {
	pathis = pathis.substring(0, pathis.lastIndexOf("."));
}
path_array = pathis.split("/");



//SET THE FLASH PARAMS: EXECUTED FROM SWF OBJECT
//set the embedsection param in the swfobject
function setembedsection() {
	if (path_array.length >= 2) {
		return path_array[1];
	} else {
		return '';
	}
}
//set the embedmode param in the swfobject
function setembedmode() {
	//alert("1:"+path_array[1]);
	//alert("2:"+path_array[2]);
	if ((path_array[1] == "where-we-work" || path_array[1] == "how-we-work" || path_array[1] == "client-stories") && (!path_array[2] || path_array[2] == "index")) {
		$('#banner').css('height', "531px");
		return "full";
	} else {
		return "nav";	
	}
}



//SET THE HTML SUB NAV TO IT'S ACTIVE STATE
//get the current path minus the 
var currentpath = window.location.pathname;
//var currentpage = sPath.substring(sPath.lastIndexOf('//') + 1);
//loop thru the anchors in the subnav div
$(document).ready(function() {
	$('div#subnav a').each(function(i) {
		//parse the currrent path to get the second level
		linkis = $(this).attr("href")
		if (linkis.lastIndexOf(".") > 1) {
			linkis = linkis.substring(0, linkis.lastIndexOf("."));
		}
		link_array = linkis.split("/");

		//if the anchor matches the current page
		if ($(this).attr("href") == currentpath || link_array[2] == path_array[2]) {
			//apply the active calss to the anchor
			//alert("match found");
			$(this).addClass("active");
		}
	});
});



//CONTACT DROP DOWN FUNCTIONALTY
// show the regional contact information via AJAX
// "region" specifies the region from the drop-down box
function showRegionalContact(region) {
	if (region) {
		$('#contactfield').load('/php/contact-data-get.php?region=' + escape(region));
		$('#contactfield').show();
	} else {
		$('#contactfield').hide();	
	}
}

// register the region select handler on load
// and decide whether to show regional contact info based on the cookie
$(document).ready(function() {
	
	$('#contact-regions-form select').change(function() {
		// show the regional contact and set the cookie
		var region = $(this).val();
		showRegionalContact(region);
		$.cookie('regional-contact', region, { path: '/' });
	});
	
	// has the regional contact already been set
	var region = $.cookie('regional-contact');
	if (region) {
		// show the region and populate the select box
		showRegionalContact(region);
		$('#contact-regions-form select').val(region);
	}
});



//FORM VALIDATION
function check_required(myForm) {
	var requiredFields = myForm._required.value.split("|");
	var errorString = '';
	for (var i=0; i<requiredFields.length; i++) {
		var parts = requiredFields[i].split(",");
		var field = parts[0];
		var title = parts[1];
		var requiredType = parts[2];
		var myElement = myForm[field];
		var isNull = false;
		/*
		TYPES check for
			text				//the presence of any text
			text-email		//valid email address format
			text-int			//number
			text-phone		//valid phone: numbers()+-.
			radio				//radio
			checkbox			//checkbox
			select-single	//
			select-multi	//
		*/
		//validating: confirm that text is a string
		if (requiredType == "text") {
			//alert(myElement.name);
			if ((myElement.value == null || myElement.value.search(/\w/) == -1) && errorString.indexOf(title) == -1) {
				isNull = true;
			}
		//validating: if the pref is set, then check it's value and then validate the form field that is the value
		} else if (requiredType == "pref") {
			//alert(myElement.name);
			var buttonCount = myElement.length;
			//alert(myElement[0].name);
			isNull = true;
			for (k = 0; k < buttonCount; k++) {
				//alert("loop");
				if (myElement[k].checked) {
					isNull = false;
					prefVal = myElement[k].value;
					//alert(prefVal);
					if (myForm[prefVal].value == null || myForm[prefVal].value.search(/\w/) == -1) {
						title = prefVal;
						isNull = true;
					}

				}
			}

		//validating: confirm that text is a valid email format
		} else if (requiredType == "text-email") {
			//alert(myElement.name);
			var str = myElement.value;
			var at="@";
			var dot=".";
			var lstr=str.length;
			if ((myElement.value == null || myElement.value.search(/\w/) == -1) && errorString.indexOf(title) == -1) {
				isNull = true;
			}					
			if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
				isNull = true;
			}
			if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
				 isNull = true;
			}
		//validating: confirm that text is a valid email format
		} else if (requiredType == "text-int") {
			var str = myElement.value;
			 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;
			 }		
		//validating: confirm that text is a radio button or check box is selected
		} else if (requiredType == "radio" || requiredType == "checkbox") {
			var buttonCount = myElement.length;
			//alert(myElement[0].name);
			isNull = true;
			for (j = 0; j < buttonCount; j++) {
				if (myElement[j].checked) {
					isNull = false;
				}
			}
		//validating: confirm that a option is selected
		} else if (requiredType == "select-single" || requiredType == "select-multiple") {
			//alert(myElement.options[myElement.selectedIndex].value);
			if ((myElement.options[myElement.selectedIndex].value == null || myElement.options[myElement.selectedIndex].value == '') && errorString.indexOf(title) == -1) {
				isNull = true;
			}
		}
		
		//alert(field+' is invalid: '+isNull);
		//SET ERROR
		if (isNull) {
			errorString += title + ", ";
			if (document.getElementById('label_'+field)) { 
				document.getElementById('label_'+field).className="formlabelerror";
			}
			myElement.className="formfielderror";
		} else {
			if (document.getElementById('label_'+field)) {
				document.getElementById('label_'+field).className="formlabelvalid";
			}
			myElement.className="formfieldvalid";
		}

	}
	if (errorString != '') {
		errorString = errorString.slice(0,errorString.length-2);
		window.alert("Please fill in the following required fields before submitting this form:\n\n"+errorString)
		return false;
	}
	else {
		return true;
	}
}
