// Open a new window
//
function newWindow(url,windowname,properties) {
  mywin=window.open(url,windowname,properties)
}

// Add page to user's favorite menu
//
function addFavorite() {
  if (window.external) {
    external.AddFavorite(location.href, document.title)
  }
  else {
    alert("Sorry, your browser doesn't support this feature." + 
    "\nPlease use the bookmark feature of your browser to save the location of this page.");
  }
}

//
// Function maps current page folder name to array and highlights menu bar section name
function showSection() {
	var d,url,domain,found,element,folder;
	var i,j,x;
	var localFolderPrefix="/cramm/";
	var menuHighlight="#990000"
	//
	// Array used for relating folder names to folder names
	var replaceList = new Array (
		[ "capabilities", "meCapabilities" ],
		[ "contact", "meContact" ],
		[ "downloads", "meDownloads" ],
		[ "events", "meEvents" ],
		[ "overview", "meOverview" ],
		[ "support", "meSupport" ]
	);
	//
	// Initialise variables
	d=document;
	url=unescape(d.location.href);
	//
	// Try and identify domain name by searching URL for local folder name
	x=url.indexOf(localFolderPrefix);
	if (x != -1) {
		domain=url.substr(0,x+localFolderPrefix.length);
	}
	else {
		domain=d.location.protocol + "//" + d.location.hostname + "/";
	}
	//
	// Remove domain name from URL
	url=url.substr(domain.length);
	//
	// Determine whether there is at least one folder name in URL
	found=false;
	x=url.indexOf("/");
	if (x != -1) {
		folder=url.substr(0,x);
		//
		// Search folder name in array and map to menu section name
		for (j=0; j<replaceList.length; j++) {
			if (folder==replaceList[j][0]) {
				folder=replaceList[j][1];
				found=true;
				break;
			}
		}
	}
	//
	// If folder found, change CSS colour vlaue to highlight it on menu bar
	if (found) {
		document.getElementById(folder).style.color = menuHighlight;
	}
	else {
	}
}

// Check correct completion of web form fields
//
function checkForm() {
  if (document.webForm.firstName.value == "") {
    alert("Please enter your first name or an initial");
    return(false);
  }
  if (document.webForm.surname.value == "") {
    alert("Please enter your surname");
    return(false);
  }
  if (document.webForm.jobTitle.value == "") {
    alert("Please enter your job title");
    return(false);
  }
  if (document.webForm.organisation.value == "") {
    alert("Please enter the name of your organisation");
    return(false);
  }
  if (document.webForm.noPost.checked == false && (document.webForm.address1.value == "" || document.webForm.postCode.value == "" || document.webForm.country.value == "")) {
    alert("Please enter your postal address including the postcode and country");
    return(false);
  }
  if (document.webForm.noEmail.checked == false && document.webForm.email.value == "") {
    alert("Please enter your email address");
    return(false);
  }
  if (document.webForm.officeTel.value == "") {
    alert("Please enter your office telephone number so that we can reply to you if we experience problems using your preferred method(s) of communication");
    return(false);
  }
  
  var comments = (document.webForm.comments.value).toLowerCase();
  if (comments == "") {
    alert("Please tell us how we can help you");
    return(false);
  }  
  if ((comments.indexOf("href") != -1) || (comments.indexOf("http") != -1)) {
		alert("You can't include email addresses or URLs in comments");
		return(false); 
	}
	var otherSource = (document.webForm.otherSource.value).toLowerCase();
	if ((otherSource.indexOf("href") != -1) || (otherSource.indexOf("http")) != -1) {
		alert("You can't include email addresses or URLs in that field");
		return(false);
	}
}

// Check correct completion of event registration form fields
//
function checkEvent() {
  if (document.webForm.firstName.value == "") {
    alert("Please enter your first name or an initial");
    return(false);
  }
  if (document.webForm.surname.value == "") {
    alert("Please enter your surname");
    return(false);
  }
  if (document.webForm.jobTitle.value == "") {
    alert("Please enter your job title");
    return(false);
  }
  if (document.webForm.organisation.value == "") {
    alert("Please enter the name of your organisation");
    return(false);
  }
  if (document.webForm.address1.value == "" || document.webForm.postCode.value == "" || document.webForm.country.value == "") {
    alert("Please enter your postal address including the postcode and country");
    return(false);
  }
  if (document.webForm.email.value == "") {
    alert("Please enter your email address");
    return(false);
  }
  if (document.webForm.officeTel.value == "") {
    alert("Please enter your office telephone number");
    return(false);
  }
	  if (document.webForm.source.selectedIndex == 0) {
    alert("Please tell us how you heard about this Insight event");
    return(false);
  }
}

// Alert user if both communication menthods declined 
//
function checkNoMktg() {
  if (document.webForm.noPost.checked && document.webForm.noEmail.checked && (document.webForm.trainingInfo.checked || document.webForm.otherInfo.checked)) {
    alert ("We won't be able to keep you updated on the topics you've selected if we can't use your post or email address");
  }
}
