/*
javascript functions for Hiser


*/




window.onload=loadUP;


function loadUP(){
	AttachMouseOver();	// For Top level Menus
	resizeIframe();
	FocusInput();

	if(document.images) {
		test = new Image();
		test.src = "/remote/jstest.asp";
	}
	/*@cc_on
		SetUpSelects();
	@*/

}


function checkSearch(qValue){
	if (strTrim(qValue)==''){
		alert("Please enter some words to search on")
		return false;
	}else{return true;}
}


// Cross Browser DOM Functions used to create objects and get top / left coords
// copyright Stephen Chapman, 4th Jan 2005
// you may copy this code but please keep the copyright notice as well
var aDOM = 0, ieDOM = 0, nsDOM = 0
var stdDOM = document.getElementById;
if (stdDOM) aDOM = 1; else {
  ieDOM = document.all;
  if (ieDOM) aDOM = 1; else {
    var nsDOM = (
      (navigator.appName.indexOf('Netscape') != -1)
      && (parseInt(navigator.appVersion) ==4));
    if (nsDOM) aDOM = 1;
  }
}
function xDOM(objectId, withStyle) {
var st = ''; if (withStyle) st = '.style';
if (stdDOM) return (eval('document.getElementById(objectId)'+st));
if (ieDOM) return (eval('document.all[objectId]'+st));
if (nsDOM) return (document.layers[objectId]);
}
// Object Functions
// copyright Stephen Chapman, 4th Jan 2005
// you may copy these functions but please keep the copyright notice as well
function objLeft(objectID) {var obj = xDOM(objectID,0);var objs = xDOM(objectID,1);  if(objs.left) return objs.left; if (objs.pixelLeft) return objs.pixelLeft; if (obj.offsetLeft) return obj.offsetLeft; return 0;}
function objTop(objectID) {var obj = xDOM(objectID,0);var objs = xDOM(objectID,1);  if(objs.top) return objs.top; if (objs.pixelTop) return objs.pixelTop; if (obj.offsetTop) return obj.offsetTop; return 0;}


//**** MAIN DHTML NAVIGATION

// Variables
var intNegativeIndent = "-250px" // Used when the sub-menu is shown to the left of the parent 
var intIndent = "41px"  // Indent when shown to the right
if(!document.all){
	intIndent = "31px" // Change for non-IE browsers
}
	
// Variables to store active items
var activeTopMenu = ""
var activeParent = ""
var activeSubMenu = ""

function ShowMenu(menuID,strType,trigObj){
	var screenWidth = getBrowserWidth()
	if(xDOM("SearchSelect",0)){xDOM("SearchSelect",1).visibility = 'hidden'}
	if (strType=="top"){
		if (activeTopMenu!=""){
			HideMenu(activeTopMenu)
		}
		activeTopMenu = menuID
		var menuObj = xDOM(menuID,0);
		if((objLeft(trigObj) + 280) > screenWidth){
			menuObj.style.left = screenWidth - 260 + "px";	
		}else{
			menuObj.style.left = objLeft(trigObj) + "px";	
		}		
		menuObj.style.zIndex=2000;
		activeParent = trigObj
	}else{
		if (activeSubMenu!=""){
			HideMenu(activeSubMenu)
		}
		
		var menuSubObj = xDOM(menuID,0);
		var topIndent = objTop(trigObj) + 14 + "px";
		menuSubObj.style.zIndex=2100;
		if((objLeft(activeParent) + 460) > screenWidth){
			menuSubObj.style.left = intNegativeIndent;
			menuSubObj.style.top = topIndent;
		}else{
			menuSubObj.style.left = intIndent;
			menuSubObj.style.top = topIndent;
		}
		activeSubMenu = menuID
	}
}
function HideMenu(menuID){
	var menuObj = xDOM(menuID,0);
	menuObj.style.left = "-999em";	
	
}
function getBrowserWidth() {
	if (document.all){
		var intScreenWidth = document.body.clientWidth
	}else{
		var intScreenWidth = window.innerWidth
		
	}
	return intScreenWidth
}

function HideSub(){
	if (activeSubMenu!=""){
		HideMenu(activeSubMenu)
		activeSubMenu = ""
	}		
}

function hideAllMenus(){
	if (activeSubMenu!=""){
		HideMenu(activeSubMenu)
		activeSubMenu = ""
	}			
	if (activeTopMenu!=""){
		HideMenu(activeTopMenu)
		activeTopMenu = ""
		activeParent = ""
	}	
	if(xDOM("SearchSelect",0)){xDOM("SearchSelect",1).visibility = 'visible'}
}


function AttachMouseOver(){
	//This attaches a HideSub mouseover for all first level items that dont have an event.
	var menuAnchors = document.getElementById("nav").getElementsByTagName("A");
	for (var i=0; i<menuAnchors.length; i++) {	
		if (menuAnchors[i].onmouseover!=undefined){
		}else if(menuAnchors[i].className=="topLevel"){
			menuAnchors[i].onmouseover = hideAllMenus;
		}else if(menuAnchors[i].className!="subItem"){
			menuAnchors[i].onmouseover = HideSub;
			//alert("item Numnber" & i)
		}
	}
}



// Left Sub Nav
function montre(id) {
var d = document.getElementById(id);
	for (var i = 1; i<=1000; i++) {
		if (document.getElementById('smenu'+i)) {document.getElementById('smenu'+i).style.display='none';}
	}
if (d) {d.style.display='block';}
}



//Glossary Items
var IE = document.all?true:false

//Add the event
document.onclick = SetXY


var tempX = 0
var tempY = 0

function WriteGlossary(intID){
	if (intID!=''){		
		var objGlossary = xDOM("glossary", 0)
		var objIFrame = xDOM("glossaryFrame", 0)
		objIFrame.src = "/glossary.asp?id=" + intID
		if(tempX == 0)tempX = 150;
		if(tempY == 0)tempY = 150;
		objGlossary.style.left = tempX + "px";
		objGlossary.style.top = tempY + 10 + "px";
		objGlossary.style.display = "block";
		if(IE){correctPNG()};// Correct the PNG display problems
	}

}
function CloseGlossary(){
	if(xDOM("glossary", 0)){
	var objGlossary = xDOM("glossary", 0)
	objGlossary.style.display = "none";
	}
}
function SetXY(e){
	if (IE) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft
		tempY = event.clientY + document.body.scrollTop
	} else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX
		tempY = e.pageY
	}  	
	CloseGlossary();
}


function correctPNG(){
   for(var i=0; i<document.images.length; i++){
	  var img = document.images[i]
	  var imgName = img.src.toUpperCase()
	  if (imgName.substring(imgName.length-3, imgName.length) == "PNG"){
		 var imgID = (img.id) ? "id='" + img.id + "' " : ""
		 var imgClass = (img.className) ? "class='" + img.className + "' " : ""
		 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
		 var imgStyle = "display:inline-block;" + img.style.cssText 
		 if (img.align == "left") imgStyle = "float:left;" + imgStyle
		 if (img.align == "right") imgStyle = "float:right;" + imgStyle
		 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle		
		 var strNewHTML = "<span " + imgID + imgClass + imgTitle
		 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
	     + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		 + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
		 img.outerHTML = strNewHTML
		 i = i-1
	 }
  }
}



function CheckDependants(strAssetList, objSelectedCheckBox , blnOnLoad){
	var arrAssets = strAssetList.split(",")
	var l = arrAssets.length	
	if (!objSelectedCheckBox.checked || blnOnLoad){ // Disable them
		for (x = 0; x < l; x++){
			disableItem(arrAssets[x])
		}		
	}else{
		for (x = 0; x < l; x++){ // Re-enable them
			enableItem(arrAssets[x])
		}
	}
}


function CheckExclusives(strAssetList, objSelectedCheckBox , blnOnLoad){
	var arrAssets = strAssetList.split(",")
	var l = arrAssets.length	
	if (!objSelectedCheckBox.checked || blnOnLoad){ // Disable them
		for (x = 0; x < l; x++){
			enableItem(arrAssets[x])
			
		}		
	}else{
		for (x = 0; x < l; x++){ // Re-enable them
			disableItem(arrAssets[x])
		}
	}	
}


function disableItem(id){
	var objP = xDOM("p"+ id,0)
	var objCheckbox = xDOM("projpoint_"+ id,0) 
	if(objCheckbox){
		objP.style.color = "#909090"
		objCheckbox.disabled = true	
	}
}
function enableItem(id){
	var objP = xDOM("p"+ id,0)
	var objCheckbox = xDOM("projpoint_"+ id,0) 
	if(objCheckbox){
		objP.style.color = "#505050"
		objCheckbox.disabled = false
	}
}


function ValidateStep3(frmObj){
	var i;
	var formElements = frmObj.elements;
	
	for (i=0; i<formElements.length; i++) {
		if (formElements[i].type == "checkbox") {
			if(formElements[i].checked){
				 return true;
				break;
			}
		}	
	}
	alert("Please select one or more goals.")
	return false;
	
}

function ValidateStep2(frmObj){

	var cnt = -1;
	for (var i=0; i < frmObj.projectDesignProcess.length; i++) {
	   if (frmObj.projectDesignProcess[i].checked) {cnt = i; i = frmObj.projectDesignProcess.length;}
	   }
	if (cnt > -1){
		return true;
	}else{
		alert("Please make a selection.")
		return false;
	}
}


function CheckType(intCount, objSelect){

	var objOther = xDOM(intCount + "_OtherDiv",0)
	var isOther = false
	for (i = 0; i<objSelect.length; i++ ){
		if (objSelect.options[i].selected && objSelect.options[i].value=="Other"){
			isOther = true;
		}
	}
	if(isOther){
		objOther.style.display="block";
	}else{
		objOther.style.display="none";
	}
}









function resizeIframe(){
	var intScreenWidth = getBrowserWidth()	
	//alert(intScreenWidth)
	var objIframe = xDOM("homeFrame",0)
	var intWidth = intScreenWidth - 240
	if (intWidth < 530){
		intWidth = 530;	
	}
	//alert(intWidth)
	if(objIframe){
		objIframe.style.width = intWidth + "px";
	}

	
}





function PopImage(id){
	if(id!=""){
		var strURL = "/pop.asp?id=" + id
		var objPhotoWindow = window.open(strURL,'photo','height=500,width=500,left=10,top=10,directories=0,fullscreen=0,location=0,menubar=0,scrollbars=1,resizable=0,status=0,toolbar=0');
	}
}

// HOME "See our work"
var objImgOld = false
var intTotalcaseStudies = 0
var intCurrCaseStudy = 0

function ChangeCaseStudy(intItem, blnAutoRun, intTotalItems){
	intTotalcaseStudies = intTotalItems;
	intCurrCaseStudy = intItem
	var objImg = xDOM("Icon" + intItem, 0)
	objImg.src = "/css/images/OurWorkOn.gif"
	//reset the previous one
	if(objImgOld!=false){objImgOld.src = "/css/images/OurWorkOff.gif"}
	// assign the current one for next time
	objImgOld = objImg
	
	
	if(arrCaseStudy[intItem].itmImg != ''){
		xDOM("WorkImage", 0).src = arrCaseStudy[intItem].itmImg
	}else{
		xDOM("WorkImage", 0).src = "/images/spacer.gif"
		
	}
	xDOM("WorkTitle", 0).innerHTML = arrCaseStudy[intItem].itmTitle
	xDOM("WorkContent", 0).innerHTML = unescape(arrCaseStudy[intItem].itmText)

	var strLinkList = '<ul>'
	if(arrCaseStudy[intItem].itmExtURL != ''){strLinkList = strLinkList + '<li><a href="' + arrCaseStudy[intItem].itmExtURL + '">View the site</a></li>'}
	if(arrCaseStudy[intItem].itmURL != ''){strLinkList = strLinkList + '<li><a href="' + arrCaseStudy[intItem].itmURL + '">Full article</a></li>'}
	if(MoreLinkURL != ''){strLinkList = strLinkList + '<li><a href="' + MoreLinkURL + '">See more of our work</a></li>'}
	strLinkList = strLinkList + '</ul>'
	xDOM("WorkList", 0).innerHTML = strLinkList
	
	if(blnAutoRun){
		setTimeout("RotateCaseStudy()",15000)
	}
}


function RotateCaseStudy(){
	if((intCurrCaseStudy + 1) > intTotalcaseStudies){
		ChangeCaseStudy(0, true, intTotalcaseStudies)
	}else{
		ChangeCaseStudy(intCurrCaseStudy + 1, true, intTotalcaseStudies)
	}
}




function FocusInput(){
	
	var inputs=document.getElementsByTagName('input');
	var intText = 0
	for (i=0;i<inputs.length;i++){
		if(inputs[i].type=="text"){intText++}
	}
	//alert(intText)
	if(intText > 1){
		intItemFocus = 2
	}else{
		intItemFocus = 1
	}
	
	var intSecondText = 0
	for (i=0;i<inputs.length;i++){
		if(inputs[i].type=="text"){
			intSecondText++
			if(intSecondText==intItemFocus){
				inputs[i].focus()
			}
		}
		
	}	
}

Array.prototype.attSearch =
function(att,value)
{
    var ret = new Array();
 
    for(var i =0 ; i < this.length; i++)
    {
        if(this[i].att == value)
            ret.push(this[i]);
    }
 
    return ret;
}

// DYNAMIC FORM STUFF




function setDynaList(arrDL){

 var oList1 = document.forms[arrDL[2]].elements[arrDL[1]];
 var oList2 = document.forms[arrDL[4]].elements[arrDL[3]];
 var arrList = arrDL[5];
 
 clearDynaList(oList2);
 
 if (oList1.selectedIndex == -1){
  oList1.selectedIndex = 0;
 }

 populateDynaList(oList2, oList1[oList1.selectedIndex].value, arrList);
 return true;
}
 
function clearDynaList(oList){

 for (var i = oList.options.length; i >= 0; i--){
  oList.options[i] = null;
 }
 
 oList.selectedIndex = -1;
}

function populateDynaList(oList, nIndex, aArray){
oList[oList.length]= new Option("Please Select");
 for (var i = 0; i < aArray.length; i= i + 3){
  if (aArray[i] == nIndex){
   oList.options[oList.options.length] = new Option(aArray[i + 1], aArray[i + 2]);
  }
  //oList.size=oList.length //You need to comment out this line of the function if you use this mod
 }

 if (oList.options.length == 1){
  oList.options[oList.options.length] = new Option(":: No SubCategories Available ::");
 }
 oList.selectedIndex = 0; 
}



// Form Validation Stuff

function ValidateReg1(objForm){
	var strAlert = "";
	strAlert = "______________________________________\n\nPlease\n\n";
	var isAlert = false;	
	
	var strFirstName = strTrim(objForm.FirstName.value);
	var strLastName = strTrim(objForm.LastName.value);	
	var strEmail = strTrim(objForm.Email.value);
	var strInstance = objForm.instanceID.options[objForm.instanceID.selectedIndex].value;
	var intAttendees = parseInt(strTrim(objForm.number.value));
	
	if (strFirstName==""){
		strAlert += "- Enter your First Name\n";
		isAlert = true;
	}
	if (strLastName==""){
		strAlert += "- Enter your Last Name\n";
		isAlert = true;
	}	
	if (!ValidEmail(strEmail)){
		strAlert += "- Enter a valid email address\n";
		isAlert = true;
	}
	if(strInstance=="Please Select"){
		strAlert += "- Select the course location you would like to attend\n";
		isAlert = true;		
	}
	if(intAttendees < 1 || intAttendees > 15 || isNaN(intAttendees)){
		strAlert += "- Enter the number of attendees (1-15)\n";
		isAlert = true;			
	}
	if (isAlert == true){
		strAlert += "\n\n______________________________________";
		alert (strAlert);
		return false;
	}else{
		return true;
	}
}

function ValidateReg2(objForm){
	var intTotalAttendees = strTrim(objForm.numAttendees.value);
	var strAlert = "";
	strAlert = "______________________________________\n\nPlease\n\n";
	var isAlert = false;	
	
	for (var i = 1; i <= intTotalAttendees; i++){
			
		var firstname = strTrim(objForm.eval("att_" + i + "_firstname").value);
		var lastname = strTrim(objForm.eval("att_" +i + "_lastname").value);	
		var phone = strTrim(objForm.eval("att_" +i + "_Phone").value);	
		var email = strTrim(objForm.eval("att_" +i + "_Email").value);
		if (firstname==""){
			strAlert += "- Enter attendee " + i + "'s first name\n";
			isAlert = true;
		}
		if (lastname==""){
			strAlert += "- Enter attendee " + i + "'s last name\n";
			isAlert = true;
		}
		if (phone==""){
			strAlert += "- Enter attendee " + i + "'s phone number\n";
			isAlert = true;
		}
		alert(email)
		if (!ValidEmail(email)){
			strAlert += "- Enter a valid email address for attendee " + i + "\n";
			isAlert = true;
		}

		
	}
	if (isAlert == true){
		strAlert += "\n\n______________________________________";
		alert (strAlert);
		return false;
	}else{
		return true;
	}
}

function CheckPaymentMethod(strmethod){
	var objCheque = xDOM("Cheque",0)
	var objCreditCard = xDOM("Creditcard",0)
	if(strmethod=="3"){
		objCheque.style.display="block";
		objCreditCard.style.display="none";
	}else if(strmethod!=""){
		objCreditCard.style.display="block";
		objCheque.style.display="none";
	}else{
		objCheque.style.display="none";
		objCreditCard.style.display="none";
	}
}

function ValidateReg3(objForm){
	var strAlert = "";
	strAlert = "______________________________________\n\nPlease\n\n";
	var isAlert = false;	
	
	var paymentMethod = objForm.paymentMethod.options[objForm.paymentMethod.selectedIndex].value;	
	var intCreditCard = strTrim(objForm.CreditCard.value)
	var intCreditCardName = strTrim(objForm.CreditCardName.value)
	if(paymentMethod=="3"){
		if(confirm("Are you sure you want to place this order.\n\n Click Ok to continue")){
			return true;
		}else{
			return false;
		}
	}else{
		if (intCreditCardName==""){
			strAlert += "- Please enter the name on your Credit Card\n";
			isAlert = true;
		}
		if (!validateCreditCard(intCreditCard)){
			strAlert += "- Please enter a valid Credit Card number\n";
			isAlert = true;
		}		
	}
	if (isAlert == true){
		strAlert += "\n\n______________________________________";
		alert (strAlert);
		return false;
	}else{
		return true;
	}

}

function strTrim(strText) {
	for (i=0; i<strText.length; ++i) {
		if (strText.charAt(i) != ' ') break;
	}
	if (i >= strText.length) return ('');
	if (i > 0) strText = strText.substring(i, strText.length);
	for (j=strText.length-1; j > i; --j) {
		if (strText.charAt(j) != ' ') break;
	}
	if (j < strText.length -1)
		strText = strText.substring(0, j+1);
	return strText;
}
function ValidEmail(email){
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0){
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  if (theStr.indexOf(" ") > 0 )  return false;  
  if (theStr.indexOf("@", index + 1) > 0 ) return false;
  return result;
}

// Credit Card Validation Javascript
// copyright 12th May 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function validateCreditCard(s) {
	var v = "0123456789";
	var w = "";
	for (var i=0; i < s.length; i++) {
	x = s.charAt(i);
	if (v.indexOf(x,0) != -1)
	w += x;
	}
	var j = w.length / 2;
	if (j < 6.5 || j > 8 || j == 7) return false;
	var k = Math.floor(j);
	var m = Math.ceil(j) - k;
	var c = 0;
	for (var i=0; i<k; i++) {
	a = w.charAt(i*2+m) * 2;
	c += a > 9 ? Math.floor(a/10 + a%10) : a;
	}
	for (var i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
	return (c%10 == 0);
}


// Google maps API script

function load() {
	if (GBrowserIsCompatible()) {
	  var map = new GMap2(document.getElementById("map"));
	  map.setCenter(new GLatLng(37.4419, -122.1419), 13);
	}
}


function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

