<!-- Begining

// -----------------------------------------------------------------------------
// Script to show/hide divisions on page.

var ie4 = (document.all) ? true : false;
var ns4 = (document.layers) ? true : false;
var ns6 = (document.getElementById && !document.all) ? true : false;

function hideBlock(ident) {
if (ie4) {document.all[ident].style.display = "none";}
if (ns4) {document.layers[ident].visibility = "hide";}
if (ns6) {document.getElementById([ident]).style.display = "none";}
}

function showBlock(ident) {
if (ie4) {document.all[ident].style.display = "block";}
if (ns4) {document.layers[ident].visibility = "show";}
if (ns6) {document.getElementById([ident]).style.display = "block";}
}

// Script to show/hide divisions on LEFT MENU page.
function SwitchMenu(obj){
	if(document.getElementById){
	var el = document.getElementById(obj);
	var ar = document.getElementById("cont").getElementsByTagName("SPAN");
		if(el.style.display == "none"){
			for (var i=0; i<ar.length; i++){
				ar[i].style.display = "none";
			}
			el.style.display = "block";
		}else{
			el.style.display = "none";
		}
	}
}
function ChangeClass(menu, newClass) { 
	 if (document.getElementById) { 
	 	document.getElementById(menu).className = newClass;
	 } 
} 
// -----------------------------------------------------------------------------
// Functions used by Contact Form
// -----------------------------------------------------------------------------

// number of days cookie lives for
var num_days = 180;

function ged(noDays){
    var today = new Date();
    var expr = new Date(today.getTime() + noDays*24*60*60*1000);
    return  expr.toGMTString();
}
// ------------------------------------------------------------------------
// Javascript encode/decode for Unicode to UTF-8
// ------------------------------------------------------------------------
function utf8_encode(string)
{
	string = string.replace(/\r\n/g,"\n");
	var utftext = "";

	for (var n = 0; n < string.length; n++) {

		var c = string.charCodeAt(n);

		if (c < 128) {
			utftext += String.fromCharCode(c);
		}
		else if((c > 127) && (c < 2048)) {
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		}
		else {
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}

	}

	return utftext;
}

function utf8_decode(utftext)
{
	var string = "";
	var i = 0;
	var c = c1 = c2 = 0;

	while ( i < utftext.length ) {

		c = utftext.charCodeAt(i);

		if (c < 128) {
			string += String.fromCharCode(c);
			i++;
		}
		else if((c > 191) && (c < 224)) {
			c2 = utftext.charCodeAt(i+1);
			string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
			i += 2;
		}
		else {
			c2 = utftext.charCodeAt(i+1);
			c3 = utftext.charCodeAt(i+2);
			string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
			i += 3;
		}

	}

	return string;
}

// ------------------------------------------------------------------------
function writeCookie(){
// Test to see if form contains anything.
	if ((document.forms.ContactDetailsForm.username.value == "")|| 
		(document.forms.ContactDetailsForm.email.value == "")|| 
		(document.forms.ContactDetailsForm.city.value == "")|| 
		(document.forms.ContactDetailsForm.country.value == "")){
			window.alert("Please enter all of the required information.");
			return false;
	}

	// Collect information from form.
	var contactName = encodeURIComponent(document.forms.ContactDetailsForm.username.value);
	var contactEmail = encodeURIComponent(document.forms.ContactDetailsForm.email.value);
	var contactCompany = encodeURIComponent(document.forms.ContactDetailsForm.company.value);
	var contactCity = encodeURIComponent(document.forms.ContactDetailsForm.city.value);
	var contactCountry = encodeURIComponent(document.forms.ContactDetailsForm.country.value);
	var contactPhone = encodeURIComponent(document.forms.ContactDetailsForm.phone.value);
	var contactWebSite = encodeURIComponent(document.forms.ContactDetailsForm.web.value);
	var contactDistributor = encodeURIComponent(document.forms.ContactDetailsForm.distributor.value);
	var contactOtherCompany = encodeURIComponent(document.forms.ContactDetailsForm.othercompany.value);

	// Do tests on formMailList checkbox
	if (document.forms.ContactDetailsForm.maillist.checked){
		var contactMailList = "Yes";
	} else {
		var contactMailList = "No";
	}
	
	//Construct string.
	var cookieString = "CogentCookie7=" + ("ContactName=" + contactName 
							+ "&ContactEmail=" + contactEmail
							+ "&ContactMailList=" + contactMailList
							+ "&ContactCompany=" + contactCompany
							+ "&ContactCity=" + contactCity
							+ "&ContactCountry=" + contactCountry
							+ "&ContactPhone=" + contactPhone
							+ "&ContactWebSite=" + contactWebSite
							+ "&ContactDistributor=" + contactDistributor
							+ "&ContactOtherCompany=" + contactOtherCompany);

	// Add expiration date if save Cookie is requested.
	
	if (document.forms.ContactDetailsForm.savecookie.checked){
		cookieString = (cookieString + ";expires=" + ged(num_days));
		document.cookie = cookieString;
		verifyCookieContact();
		// window.location = ("Download_Software.html");
		return;
	} else {
		// Just write session cookie
		document.cookie = cookieString;
		verifyCookieContact();
		// window.location = ("Download_Software.html");
		return;
	}
}
// ------------------------------------------------------
// Functions used by the Contact page
// Differs from verifyCookie function in that it doesn't reload page
// if cookie is not written due to browser blocking cookies.
// ------------------------------------------------------
function verifyCookieContact(){
	// Checks to see if the CogentCookie7 is present.
	// Redirects to Contact Form if necessary.
	
	var cookieName = "CogentCookie7";
    var start = document.cookie.indexOf(cookieName);
    if (start == -1){ 
		// If cookie doesn't exist then do nothing and continue to display the page.
		return;
    } else {
		// If cookie is found, then redirect to Download and exit.
		window.location = ("Download_Software.html");
		return;
	}
}
// ------------------------------------------------------
// Functions used by the Download_Software page
// Used when Download_Software page is loaded to check to see if cookie
// is present.  If it is, then just display the page, if not, then
// redirect to contact form.
// Only really used for when people link directly to Download_Software page.
// ------------------------------------------------------
function verifyCookieDownload(){
	// Checks to see if the CogentCookie7 is present.
	// Redirects to Contact Form if necessary.
	
	var cookieName = "CogentCookie7";
    var start = document.cookie.indexOf(cookieName);
    if (start == -1){ 
		// If cookie doesn't exist then redirect to Contact and exit.
		window.location = ("Contact_Form.html"); 
		return;
    } else {
		// If cookie is found, then do nothing and continue to dislay the page.
		return;
	}
}
// ------------------------------------------------------
// Functions used by the Download page
// ------------------------------------------------------
function verifyCookie(){
	// Checks to see if the CogentCookie7 is present.
	// Redirects to Contact Form if necessary.
	
	var cookieName = "CogentCookie7";
    var start = document.cookie.indexOf(cookieName);
    if (start == -1){ 
		// If cookie doesn't exist then redirect to Contact and exit.
		window.location = ("Contact_Form.html"); 
		return;
    } else {
		// If cookie is found, then redirect to Download and exit.
		window.location = ("Download_Software.html");
		return;
	}
}
// ------------------------------------------------------------------------
function readTheCookie(cookie_info){
	
	// Read the Cogent Cookie Details.
	var cookieName = "CogentCookie7";
	
	// Find the start of the Cogent Cookie.
	var firstChar = document.cookie.indexOf(cookieName);
	var lastChar;
	var NN2Hack = firstChar + cookieName.length;
	// If you found the cookie ...
	if((firstChar != -1) && (document.cookie.charAt(NN2Hack) == '=')) {
		// skip 'name' and '='.
		firstChar += cookieName.length + 1;
		// Find the end of the value string (i.e. the next ';').
	    lastChar = document.cookie.indexOf(';', firstChar); 
		// If reach the end, then this means our cookie was the only one 
		// and assume lastChar = length of cookie.
			if(lastChar == -1) {
				lastChar = document.cookie.length;
			}
		// Extract the contents of the cookie.
		var the_values = unescape(document.cookie.substring(firstChar, lastChar));
		// Parse the cookie string.
		// split and copy each name=value pair into an array (list)
		var separated_values = the_values.split("&");
		// Loop through the list of name=values and load up the associate array
		var property_value = "";
			for (var loop = 0; loop < separated_values.length; loop++){
				property_value = separated_values[loop];
				var broken_info = property_value.split("=");
				var the_property = broken_info[0];
				var the_value = broken_info[1];
				cookie_info[the_property] = decodeURIComponent(the_value);
			}
		
	} else { // If cookie is not found, just exit
		return;
	}
}

// ------------------------------------------------------------------------
function setContactDetailsForm(){

	// Checks to see if the CogentCookie7 is present.
	// If it is there then copy details into form.
	var cookieName = "CogentCookie7";
    var start = document.cookie.indexOf(cookieName);
    if (start == -1){ 
		// If cookie doesn't exist then just exit function.
		return;
    } else {
		// If cookie is found, then read cookie and fill in form.
			
		var cookie_details = new Array();
		readTheCookie(cookie_details);
		// Test to see if MailList is "Yes".
		if (cookie_details["ContactMailList"] == "Yes"){
			document.forms.ContactDetailsForm.maillist.checked = true;
		}
		// read the other data from the associative array.
		document.forms.ContactDetailsForm.username.value = cookie_details["ContactName"];
		document.forms.ContactDetailsForm.email.value = cookie_details["ContactEmail"];
		document.forms.ContactDetailsForm.phone.value = cookie_details["ContactPhone"];
		document.forms.ContactDetailsForm.company.value = cookie_details["ContactCompany"];
		document.forms.ContactDetailsForm.city.value = cookie_details["ContactCity"];
		document.forms.ContactDetailsForm.country.value = cookie_details["ContactCountry"];
		checkDistrib("country");
		document.forms.ContactDetailsForm.web.value = cookie_details["ContactWebSite"];
		document.forms.ContactDetailsForm.distributor.value = cookie_details["ContactDistributor"];
		document.forms.ContactDetailsForm.othercompany.value = cookie_details["ContactOtherCompany"];
	}
}
// ------------------------------------------------------------------------
function checkDistrib(calledFrom){
	// When user selects distrib, check to see if the choose 'Other company'
	// and then make entry field visible.

	if (calledFrom == "country") {
	    try {
		var distribCombo = document.forms['ContactDetailsForm'].distributor;
		var countryCombo = document.forms['ContactDetailsForm'].country;
		var option = countryCombo.options[countryCombo.selectedIndex];
		SetDropDown (distribCombo, option.value);
	    }
	    catch (e) {
		window.alert (e);
	    }
	}
	else if (calledFrom == "reset") {
	    var distribCombo = document.forms['ContactDetailsForm'].distributor;
	    SetDropDown (distribCombo, "reset");
	}

	var distribChoice = document.forms['ContactDetailsForm'].distributor.value;

	if (distribChoice == "Other company" && (calledFrom == "normal" || calledFrom == "country") ) {
	    // window.alert('You picked ' + distribChoice);
	    showBlock('Other_Company');
	    return;
	} else {
	    hideBlock('Other_Company');
	    return;	
	}
}

// ------------------------------------------------------------------------
function changeFileSize(formName){
	// Changes the number in the File Size field depending
	// on which OS is selected.
	
	var OS = document.forms[formName].OS.value;
	
	if (OS == "Linux"){ 
		// Copy LinSize into FileSize field.
		document.forms[formName].FileSize.value = document.forms[formName].LinSize.value;
		document.forms[formName].FileVer.value = document.forms[formName].LinVer.value;
    } else if (OS == "QNX6"){
		// Copy Q6Size into FileSize field.
		document.forms[formName].FileSize.value = document.forms[formName].Q6Size.value;
		document.forms[formName].FileVer.value = document.forms[formName].Q6Ver.value;
	} else if (OS == "QNX4"){
		// Copy Q4Size into FileSize field.
		document.forms[formName].FileSize.value = document.forms[formName].Q4Size.value;
		document.forms[formName].FileVer.value = document.forms[formName].Q4Ver.value;
	} else if (OS == "Windows"){
		// Copy WinSize into FileSize field.
		document.forms[formName].FileSize.value = document.forms[formName].WinSize.value;
		document.forms[formName].FileVer.value = document.forms[formName].WinVer.value;
	}

}
// ------------------------------------------------------------------------
// -----------     Scripts used for IFRAMES implementation      -----------
// ------------------------------------------------------------------------

function buildQueryString(theFormName)
{
 theForm = document.forms[theFormName];
 var qs = '';
 for (e=0;e<theForm.elements.length;e++)
 {
  if (theForm.elements[e].name!='')
  {
   qs+=(qs=='')?'?':'&';
   qs+=theForm.elements[e].name+'='+escape(theForm.elements[e].value);
  }
 }
 // This time stamp helps get around IE caching the page which 
 // leads to random downloads not getting logged.
 qs += "&now=" + (new Date()).getTime();  
 return qs;
}

// ------------------------------------------------------------------------
function createTempIframe (framename)
{
	var tempIFrame;
	
	// create the IFrame and return a reference to the
	// object so we can use it later.
	try {
		tempIFrame=document.createElement('iframe');
		tempIFrame.setAttribute('id',framename);
		tempIFrame.style.border='0px';
		tempIFrame.style.width='0px';
		tempIFrame.style.height='0px';
		tempIFrame = document.body.appendChild(tempIFrame);
			
		// if (document.frames) {
			// this is for IE5 Mac, because it will only
			// allow access to the document object
			// of the IFrame if we access it through
			// the document.frames array
			// tempIFrame = document.frames[framename];
		// }
	} catch(exception) {
		// This is for IE5 PC, which does not allow dynamic creation
		// and manipulation of an iframe object. Instead, we'll fake
		// it up by creating our own objects.
		iframeHTML='<iframe id="'+framename+'" style="';
		iframeHTML+='border:0px;';
		iframeHTML+='width:0px;';
		iframeHTML+='height:0px;';
		iframeHTML+='"><\/iframe>';
		document.body.innerHTML+=iframeHTML;
		  //tempIFrame = new Object();
		  //tempIFrame.document = new Object();
		  //tempIFrame.document.location = new Object();
		  //tempIFrame.document.location.iframe = document.getElementById(framename);
		  //tempIFrame.document.location.replace = function(location) {
		  // this.iframe.src = location;
		  //}
		  tempIFrame = document.getElementById(framename);
	}
	return (tempIFrame);
}
// ------------------------------------------------------------------------
var IFrameObj; // our IFrame object
var IFrameObj2; // another IFrame object
// ------------------------------------------------------------------------
function callToServer(theFormName) {
	
	// Copy cookie details into the hidden fields within the download form.
	copyCookieDetails(theFormName);

	if (!document.createElement)
		return true;
	var URL = 'http://developers.cogentrts.com/cgi-bin/software.g' + buildQueryString(theFormName) + '&DownloadSite=OPC DataHub';

	if (!IFrameObj)
		IFrameObj = createTempIframe('RSIFrame');
	if (!IFrameObj2)
		IFrameObj2 = createTempIframe('RSIFrame2');
	
	if (navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument) {
		// we have to give NS6 a fraction of a second
		// to recognize the new IFrame
		setTimeout('callToServer("'+theFormName+'")',10);
		return false;
	}
	
	// Make the redirection call to server 
	// with argument string appended
	
	// IFrameObj.src = URL;
	// IFrameObj2.src = getDownloadName(theFormName);
	IFrameObj.setAttribute("src", URL);
	IFrameObj2.setAttribute("src", getDownloadName(theFormName));
	
	return false;
}

// ------------------------------------------------------------------------
function getDownloadName(theFormName){
	
	// Initiate the file download based on product and OS selected.
	var ProductName = document.forms[theFormName].name;
	var OS = document.forms[theFormName].OS.value;	
	
	// Use the location.href method to redirect the browser to the download file.

	if ((ProductName == "OPC_DataHub") && (OS == "Windows")) {
		downloadPath = "http://www.cogent.ca/Download/Windows/OPCDataHub-6.4.12-100305-Windows.exe";
		return (downloadPath);
	} 
	else if ((ProductName == "OPC_DataHub_NT") && (OS == "Windows")) {
		downloadPath = "http://www.cogent.ca/Download/Windows/OPCDataHubNT-6.2.2-061116-Windows.exe";
		return (downloadPath);
	}
	else if ((ProductName == "OPC_DataHub_Beta") && (OS == "Windows")) {
		downloadPath = "http://www.cogent.ca/Download/Windows/OPCDataHubBeta-6.3.24-070728.RC1-Windows.exe";
		return (downloadPath);
	}
	else if ((ProductName == "OPC_DataHub_NT_Beta") && (OS == "Windows")) {
		downloadPath = "http://www.cogent.ca/Download/Windows/OPCDataHubBetaNT-6.3.23-070605-Windows.exe";
		return (downloadPath);
	}
	else if ((ProductName == "DataHub") && (OS == "Linux")) {
		downloadPath = "http://www.cogent.ca/Download/Linux/DataHub-6.4.11-100201-Linux.sh.gz";
		return (downloadPath);
	}
	else if ((ProductName == "DataHub") && (OS == "QNX6")) {
		downloadPath = "http://developers.cogentrts.com/Download/QNX6/DataHub-6.4.11-bld100201-x86-Cogent.qpr";
		return (downloadPath);
	}
	else if ((ProductName == "DataHub") && (OS == "QNX4")) {
		downloadPath = "http://www.cogent.ca/Download/QNX4/DataHub-6.4.11-100201-QNX4.sh.gz";
		return (downloadPath);
	}
	else if ((ProductName == "DataHub_Beta") && (OS == "Linux")) {
		downloadPath = "http://www.cogent.ca/Download/Linux/DataHubBeta-6.3.19-070502-Linux.sh.gz";
		return (downloadPath);
	}
	else if ((ProductName == "DataHub_Beta") && (OS == "QNX6")) {
		downloadPath = "http://developers.cogentrts.com/Download/QNX6/DataHubBeta-6.3.19-bld70502-x86-Cogent.qpr";
		return (downloadPath);
	}
	else if ((ProductName == "DataHub_Beta") && (OS == "QNX4")) {
		downloadPath = "http://www.cogent.ca/Download/QNX4/DataHubBeta-6.3.19-070502-QNX4.sh.gz";
		return (downloadPath);
	}
	else if ((ProductName == "DataHubAPI") && (OS == "Windows")) {
		downloadPath = "http://www.cogent.ca/Download/Windows/DataHubAPI-6.4.12-100226-Windows.exe";
		return (downloadPath);
	}
	else if ((ProductName == "DataHubAPI_Beta") && (OS == "Windows")) {
		downloadPath = "http://www.cogent.ca/Download/Windows/DataHubAPIBeta-6.3.10-070122-Windows.exe";
		return (downloadPath);
	}
	else if ((ProductName == "CogentAPI") && (OS == "Linux")) {
		downloadPath = "http://www.cogent.ca/Download/Linux/CogentAPI-6.4.11-100201-Linux.sh.gz";
		return (downloadPath);
	}
	else if ((ProductName == "CogentAPI") && (OS == "QNX6")) {
		downloadPath = "http://developers.cogentrts.com/Download/QNX6/CogentAPI_x86-6.4.11-bld100201-x86-Cogent.qpr";
		return (downloadPath);
	}
	else if ((ProductName == "CogentAPI") && (OS == "QNX4")) {
		downloadPath = "http://www.cogent.ca/Download/QNX4/CogentAPI-6.4.11-100201-QNX4.sh.gz";
		return (downloadPath);
	}
	else if ((ProductName == "SRR") && (OS == "Linux")) {
		downloadPath = "http://www.cogent.ca/Download/Linux/srr-1.4.43.tgz";
		return (downloadPath);
	}
	else if ((ProductName == "ToolSuite") && (OS == "QNX4")) {
		downloadPath = "http://www.cogent.ca/Download/QNX4/ToolSuite-6.4.11-100201-QNX4.sh.gz"; 
		return (downloadPath); 
	}
	else if ((ProductName == "ToolSuite") && (OS == "QNX6")) {
		downloadPath = "http://developers.cogentrts.com/Download/QNX6/ToolSuite-6.4.11-bld100201-x86-Cogent.qpr"; 
		return (downloadPath); 
	}
	else if ((ProductName == "ToolSuite") && (OS == "Linux")) {
		downloadPath = "http://www.cogent.ca/Download/Linux/ToolSuite-6.4.11-100201-Linux.sh.gz"; 
		return (downloadPath); 
	}
	else if ((ProductName == "ToolSuite_Beta") && (OS == "QNX4")) {
		downloadPath = "http://www.cogent.ca/Download/QNX4/ToolSuiteBeta-6.3.19-070502-QNX4.sh.gz"; 
		return (downloadPath); 
	}
	else if ((ProductName == "ToolSuite_Beta") && (OS == "QNX6")) {
		downloadPath = "http://developers.cogentrts.com/Download/QNX6/ToolSuiteBeta-6.3.19-bld070502-x86-Cogent.qpr"; 
		return (downloadPath); 
	}
	else if ((ProductName == "ToolSuite_Beta") && (OS == "Linux")) {
		downloadPath = "http://www.cogent.ca/Download/Linux/ToolSuiteBeta-6.3.19-070502-Linux.sh.gz"; 
		return (downloadPath); 
	}
}
// ------------------------------------------------------------------------
function copyCookieDetails(theFormName){
	// Copy cookie details into the hidden fields within the download form.
	var cookie_details = new Array();
	readTheCookie(cookie_details);
	document.forms[theFormName].ContactName.value = (cookie_details["ContactName"]);
	document.forms[theFormName].ContactEmail.value = (cookie_details["ContactEmail"]);
	document.forms[theFormName].ContactPhone.value = (cookie_details["ContactPhone"]);
	document.forms[theFormName].ContactCompany.value = (cookie_details["ContactCompany"]);
	document.forms[theFormName].ContactMailList.value = (cookie_details["ContactMailList"]);
	document.forms[theFormName].ContactCity.value = (cookie_details["ContactCity"]);
	document.forms[theFormName].ContactCountry.value = (cookie_details["ContactCountry"]);
	document.forms[theFormName].ContactWebSite.value = (cookie_details["ContactWebSite"]);
	document.forms[theFormName].ContactDistributor.value = (cookie_details["ContactDistributor"]);
	document.forms[theFormName].ContactOtherCompany.value = (cookie_details["ContactOtherCompany"]);
}

// ------------------------------------------------------------------------
function resetIframe(){
	
	// URL = 'Blank_Iframe.html';
	document.getElementById('ackFrame').setAttribute("src", "about:blank");
	return true;
}
// ------------------------------------------------------------------------
function checkMail(mailType){
	
	// Clear notification Iframe.
	// resetIframe();
	
	// Copy recipient email address from form.
	var recipient = document.getElementById('emailTest').emailAddress.value;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(recipient)) {
		if (mailType == "email") {
			sendEmail();
		}
		else {
			sendSMS();
		}
		return false;
	}
	else {
		window.alert('This does not seem to be a valid email address. Please try again.');
		return false;
	}
}

// ------------------------------------------------------------------------
function sendEmail(){
	
	// Copy recipient email address from form.
	var recipient = document.getElementById('emailTest').emailAddress.value;
	// Create url to call gamma script.
	var URL = 'http://developers.cogentrts.com/cgi-bin/demomail.g?email=' + recipient;
	// Make call to gamma script.
	document.getElementById('ackFrame').setAttribute("src", URL);
	return false;
}
// ------------------------------------------------------------------------
function sendSMS(){
	
	// Copy recipient email address from form.
	var recipient = document.getElementById('emailTest').emailAddress.value;
	// Create url to call gamma script.
	var URL = 'http://developers.cogentrts.com/cgi-bin/demoSMS.g?email=' + recipient;
	// Make call to gamma script.
	document.getElementById('ackFrame').setAttribute("src", URL);
	return false;
}

//  End -->
