/*
----------------------------------------------------------
	North West Vision and Media main JavaScript file.
	This file is included in all pages
	FileName:    generic.js
	Version:     1.00				09.01/2004
	Copyright Scion Solutions Ltd		www.scion.co.uk
----------------------------------------------------------
*/

/* Browser detection */
var NS = (document.layers) ? 1 : 0;
var IE = (document.all) ? 1 : 0;
var NEW = (document.getElementById) ? 1 : 0;
var IE4 = (IE && !NEW) ? 1 : 0;
var IE5 = (IE && NEW) ? 1 : 0;
var MAC = (navigator.appVersion.indexOf("Mac") != -1) ? 1 : 0;
var Opera = (navigator.userAgent.indexOf("Opera") != -1) ? 1 : 0;
var Firefox = (navigator.userAgent.indexOf("Firefox") != -1) ? 1 : 0;
var NS6 = (NEW && !IE && !Opera & !Firefox) ? 1 : 0;
var NS60 = ((NS6) && (navigator.userAgent.indexOf("6\/6\.0") != -1)) ? 1 : 0;
//alert('NS:' + NS + '\nIE:' + IE + '\nNEW:' + NEW + '\nIE4:' + IE4 + '\nIE5:' + IE5 + '\nNS6:' + NS6 + '\nNS60:' + NS60 + '\nMAC:' + MAC + '\nOpera: ' + Opera + 'navigator.userAgent:' + navigator.userAgent);

window.name = 'nwvMain'; // Generic window name

/* Element identification. Makes allowances for earlier browser that do not support getElementById */
function getElementById(id, windowName) {
	if (IE4 || IE5) {
		return windowName ? windowName.document.all[id] : document.all[id];
	} else if (NS6 || IE5 || IE55) {
		return windowName ? windowName.document.getElementById(id) : document.getElementById(id);
	}
}

/* Loops through each of the stylesheets and then through each of the styles
	until the specified (by the array name) style object is returned as an array of arrays  */
function getStyleClass(className) {
	if (IE4 || IE5) {
		for (var s = 0; s < document.styleSheets.length; s++) {
			for (var r = 0; r < document.styleSheets[s].rules.length; r++) {
				if (document.styleSheets[s].rules[r].selectorText == '.' + className) {
          			return document.styleSheets[s].rules[r];
				}
			}
		}
	} else if (NEW) {
		for (var s = 0; s < document.styleSheets.length; s++) {
			for (var r = 0; r < document.styleSheets[s].cssRules.length; r++) {
				if (document.styleSheets[s].cssRules[r].selectorText == '.' + className) {
					return document.styleSheets[s].cssRules[r];
				}
			}
		}
	}
  return null;
}

/* Sets the height of the left hand menu based on the height of the central content area */
function setCentralContentHeight() {
	if (NS6) {
		if (getElementById('content').offsetHeight > getElementById('menu').offsetHeight) {
			getElementById('centralContent').style.height = getElementById('content').offsetHeight+0 + 'px';
		} else {
			getElementById('centralContent').style.height = getElementById('menu').offsetHeight+2 + 'px';
		}
	}
}

/* NN6/7/Mozilla doesn't like the float and so we have to set some special paramaters for them */
function setContentItemParameters() {
	if (NS6) {
		getStyleClass('fpText').style.position = 'absolute'
		getStyleClass('fpText').style.left = '6px'
		getStyleClass('fpText').style.top = '-10px'
	}
}

/* Initialises functions on page load */
function init() {
	setCentralContentHeight();
	setContentItemParameters();
}

/* Checks the length of the search phrase in the search box */
function checkSearch(srchFld) {
	if (srchFld.length < 2) {
		alert('You must enter a search phrase of two characters or more');
		return false;
	} else {
		return true;
	}
}

/* Clears the prompt text when a user focuses. Re-enters it if the search box is left empty */
function clearSearchText(fld, fldFocus, fldValue) {
	if (fld.value == fldValue && fldFocus == 1) {
		fld.value = '';
	} else if (fld.value == '' && fldFocus == 0) {
		fld.value = fldValue;
	}
}

/* Clears the prompt text when a user focuses. Re-enters it if the email box is left empty */
function clearEmailText(fld, fldFocus) {
	if (fld.value == 'Enter email here' && fldFocus == 1) {
		fld.value = '';
	} else if (fld.value == '' && fldFocus == 0) {
		fld.value = 'Enter email here';
	}
}

// Strips out non number characters
function checkForTelephone(fld) {
	var invalid = false, string = fld.value;
	//Stripping out any invalid characters.
	for (var i=0, output='', valid="0123456789"; i<string.length; i++) {
		if (valid.indexOf(string.charAt(i)) != -1) {
			output += string.charAt(i);
		} else if (string.charAt(i) != ' ') {
			invalid = true;
		}
	}
	fld.value = output
	if (invalid == true) {
		return false;
	} else {
		return true;
	}
}

// Checks the email address
function checkForEmail(fld) {
	if (fld.value.search(/[\w\-_]+\@[\w\-_]+\.[\w\-_]+/) == -1) {
		return false;
	} else {
		return true;
	}
}

function checkNewsSubscription(fld) {
	if (!checkForEmail(fld)) {
		alert('Please a valid email address\n');
		return false;
	}
}


/*
	Checks the contact form form values
*/
function checkContactForm(frm) {
	Msg = '';
	for (i = 0; i < frm.mandatory.length; i++) {
		fieldLabelArray = frm.mandatory[i].value.split(/_/);
		fieldLabel = '';
		for (var j in fieldLabelArray) {
			fieldLabelArray[j] = fieldLabelArray[j].substring(0,1).toUpperCase() + fieldLabelArray[j].substring(1, fieldLabelArray[j].length);
			fieldLabel += fieldLabelArray[j] + ' ';
		}

		if (frm[frm.mandatory[i].value][0] && (frm[frm.mandatory[i].value][0].type == 'checkbox' || frm[frm.mandatory[i].value][0].type == 'radio')) {
		  var itemChecked = false;
		  for (k = 0; k < frm[frm.mandatory[i].value].length; k++) {
		    if (frm[frm.mandatory[i].value][k].checked) {
		      itemChecked = true;
		    }
		  }
		  if (itemChecked == false) {
		    Msg += 'Please check at least one box for "' + fieldLabel + '"\n';
		  }
		} else if (frm[frm.mandatory[i].value].value == '') {
			Msg += 'Please enter some text for "' + fieldLabel + '"\n';
		} else if (frm.mandatory[i].value.match(/name/) && frm[frm.mandatory[i].value].value.match(/[^a-zA-Z0-9-' ]+/) > 0) {
			Msg += 'Please enter only letters, space, commas or dashes for "' + fieldLabel + '"\n';
		}
	}
	if (Msg) {
		alert(Msg);
		return false;
	}
}



/* Checks that the values within the contact form are suitable for submission */
function check_contact_form(frm) {
	Msg = '';
	if (frm.name) {
		if (frm.name.value == '') {
			Msg += 'Please enter your first name\n';
		} else if (frm.name.value.search(/[^a-zA-Z0-9-' ]+/) > 0) {
			Msg += 'Please enter only letters, space, commas or dashes for your name\n';
		}
	}
	if (frm.position) {
		if (frm.position.value == '') {
			Msg += 'Please enter your position\n';
		}
	}
	if (frm.prod_working_title) {
		if (frm.prod_working_title.value == '') {
			Msg += 'Please your production/working title\n';
		}
	}
	if (frm.production_company) {
		if (frm.production_company.value == '') {
			if (frm.location_id.value == 'mftvf' || frm.location_id.value == 'raf') {
				Msg += 'Please the name of your organisation\n';
			} else {
				Msg += 'Please enter your production company\n';
			}
		}
	}
	if (frm.address) {
		if (frm.address.value == '') {
			if (frm.location_id.value == 'mftvf' || frm.location_id.value == 'raf') {
				Msg += 'Please enter your trading address\n';
			} else {
				Msg += 'Please enter your address\n';
			}
		}
	}
	if (frm.postcode) {
		if (frm.postcode.value == '') {
			Msg += 'Please enter your postcode\n';
		}
	}
	if (frm.telephone) {
		if (frm.telephone.value == '') {
			Msg += 'Please enter your telephone number\n';
		} else if (!checkForTelephone(frm.telephone)) {
			Msg += 'Please a valid UK telephone number including the area dialling code\n';
		}
	}
	if (frm.email) {
		if (frm.email.value == '') {
			Msg += 'Please enter your email address\n';
		} else if (!checkForEmail(frm.email)) {
			Msg += 'Please a valid email address\n';
		}
	}
	if (frm.production_type) {
		if (frm.production_type.value == '') {
			Msg += 'Please select your production type\n';
		}
	}
	if (frm.enquiry) {
		if (frm.enquiry.value == '') {
			Msg += 'Please enter the details of your enquiry\n';
		}
	}
	if (frm.main_area_of_operation) {
		if (frm.main_area_of_operation.value == '') {
			Msg += 'Please select your main area of operation\n';
		}
	}
	if (frm.hear_about_us) {
		if (frm.hear_about_us.value == '') {
			Msg += 'Please select where you heard about the fund\n';
		}
	}

	if (Msg) {
		alert(Msg);
		return false;
	} else {
		return true;
	}
}

/* Checks that the values within the feedback form are suitable for submission */
function check_feedback_form(frm) {
	Msg = '';
	if (frm.name) {
		if (frm.name.value == '') {
			Msg += 'Please enter your first name\n';
		} else if (frm.name.value.search(/[^a-zA-Z0-9-' ]+/) > 0) {
			Msg += 'Please enter only letters, space, commas or dashes for your name\n';
		}
	}
	if (frm.email) {
		if (frm.email.value == '') {
			Msg += 'Please enter your email address\n';
		} else if (!checkForEmail(frm.email)) {
			Msg += 'Please a valid email address\n';
		}
	}
	if (frm.type_of_enquiry) {
		if (frm.type_of_enquiry.value == '') {
			Msg += 'Please enter the nature of your enquiry\n';
		}
	}
	if (frm.enquiry) {
		if (frm.enquiry.value == '') {
			Msg += 'Please enter the details of your enquiry\n';
		}
	}

	if (Msg) {
		alert(Msg);
		return false;
	} else {
		return true;
	}
}

// opens a popup window based on either default or supplied arguments
var myWin, focusWin, genericWin;
function openWindow(url, winWidth, winHeight, windowName) {
var winWidth = winWidth ? winWidth : Opera ? 666 : 650;
var winHeight = winHeight ? winHeight : Opera ? 570 : 570;
var parms = 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, height=' + winHeight + ',width=' + winWidth + ', left=0, top=0';
    if (windowName) {
    	genericWin = window.open(url, 'myWin', parms);
    } else {
    	focusWin = window.open(url, 'myWin', parms);
    }
    if (focusWin) {
        focusWin.focus();
    }
}

/* Gets the current date and writes it to wherever the scripts is called from */
function getCurrentDate() {
	var mydate=new Date();
	var hours=mydate.getHours();
	var yr=mydate.getFullYear();
	var dy=mydate.getDay();
	var month=mydate.getMonth();
	var daym=mydate.getDate();
	if (daym<10) {
		daym="0"+daym;
	}
	var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
	var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December")

	document.write(dayarray[dy]+ ' ' + daym + ' ' + montharray[month] + ' ' + yr);
}

var tip_box_id;

function findPosX(obj)
{
   var curleft = 0;
   if(obj.offsetParent)
   while(1)
   {
      curleft += obj.offsetLeft;
      if(!obj.offsetParent)
         break;
      obj = obj.offsetParent;
   }
   else if(obj.x)
      curleft += obj.x;
   return curleft;
}

function findPosY(obj)
{
   var curtop = 0;
   if(obj.offsetParent)
   while(1)
   {
      curtop += obj.offsetTop;
      if(!obj.offsetParent)
         break;
      obj = obj.offsetParent;
   }
   else if(obj.y)
      curtop += obj.y;
   return curtop;
}

function DisplayTip(me,id) {
  offX = 85;
  offY = -2;
   var content = toolTipArray[id];
   var tipO = me;
   tip_box_id = document.getElementById("TipBox");
   var x = findPosX(me);
   var y = findPosY(me);
   tip_box_id.style.left = String(parseInt(x + offX) + 'px');
   tip_box_id.style.top = String(parseInt(y + offY) + 'px');
   tip_box_id.innerHTML = content;
   opacity(tip_box_id.id, 0, 100, 1000)
   tipO.onmouseout = HideTip;
} // function DisplayTip()

function HideTip() {
  clearTimeout(opacityTimer);
  document.getElementById(tip_box_id.id).style.display = 'none';
  document.getElementById(tip_box_id.id).style.opacity = 0;
  document.getElementById(tip_box_id.id).style.filter = 'alpha(opacity=0)';
}

var opacityTimer;
function opacity(id, opacStart, opacEnd, millisec) {
  //speed for each frame
  var speed = Math.round(millisec / 20);
  var timer = 0;

  //determine the direction for the blending, if start and end are the same nothing happens
  if(opacStart > opacEnd) {
    if (!(document.getElementById(id).style.opacity == 0
      || document.getElementById(id).style.MozOpacity == 0
      || document.getElementById(id).style.KhtmlOpacity == 0)) {
      for(cnt = opacStart; cnt >= opacEnd; cnt=cnt-5) {
        opacityTimer = setTimeout("changeOpac(" + cnt + ",'" + id + "')",(timer * speed));
        timer++;
      }
    } else {
      document.getElementById(id).style.display = 'none';
    }
  } else if(opacStart < opacEnd) {
    document.getElementById(id).style.display = 'block';

    for(cnt = opacStart; cnt <= opacEnd; cnt=cnt+5) {
      opacityTimer = setTimeout("changeOpac(" + cnt + ",'" + id + "')",(timer * speed));
      timer++;
    }
  }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
  var object = document.getElementById(id).style;
  object.opacity = (opacity / 100);
  object.MozOpacity = (opacity / 100);
  object.KhtmlOpacity = (opacity / 100);
  object.filter = "alpha(opacity=" + opacity + ")";
}