// Eonic Ltd

/*
 * Handlers for automated loading
 */ 

 _LOADERS = Array();

function callAllLoaders() {
	var i, loaderFunc;
	for(i=0;i<_LOADERS.length;i++) {
		loaderFunc = _LOADERS[i];
		if(loaderFunc != callAllLoaders) loaderFunc();
	}
}

function appendLoader(loaderFunc) {
	if(window.onload && window.onload != callAllLoaders)
		_LOADERS[_LOADERS.length] = window.onload;

	window.onload = callAllLoaders;
	_LOADERS[_LOADERS.length] = loaderFunc;
}


function divSwtch(parentContainerId,activeId) {

	parentContainer = document.getElementById(parentContainerId)
	children = parentContainer.getElementsByTagName("div")
	activeId = document.getElementById(activeId)
	
	// hide all the children of the parent container
	for (var i = 0; i < children.length; i++) 
	{
		children[i].style.display = "none";
	}

	// show the activeId
	activeId.style.display = "block";
}

function gallerySwitch(imgName,imgSrc,nWidth,nHeight,cCaption){
	var img = document.images["pic"]
	var cap = document.getElementById("imgCaption")
	
	// Change the img
	img.src = imgSrc;
	img.width = nWidth
	img.height = nHeight
	
	// Change the caption
	// cap.firstChild.nodeValue = cCaption

}

function getAppVersion() {
	appname= navigator.appName;
	appversion = navigator.appVersion;
	majorver = appversion.substring(0, 1);
	if ( (appname == "Netscape") && ( majorver >= 3 ) ) return 1;
	if ( (appname == "Microsoft Internet Explorer") && (majorver >= 4) ) return 1;
	return 0;
}



function loadImageArray() {
		if (getAppVersion()) {
		img = new Array();
		for (i = 0; i < imgsrc.length; i++) {
			img[i] = new Image();
			img[i].src = imgsrc[i];
		}
	}
}

function loadImageArray2() {
		if (getAppVersion()) {
		img2 = new Array();
		for (i = 0; i < imgsrc2.length; i++) {
			img2[i] = new Image();
			img2[i].src = imgsrc2[i];
		}
	}
}

function loadImageArray3() {
		if (getAppVersion()) {
		img3 = new Array();
		for (i = 0; i < imgsrc3.length; i++) {
			img3[i] = new Image();
			img3[i].src = imgsrc3[i];
		}
	}
}

function setHome(arg, urlName){
	
if(navigator.appVersion.charAt(navigator.appVersion.indexOf("MSIE") + 5) >= 5 && navigator.platform.indexOf("Win16") == -1 
 && navigator.platform.indexOf("Mac") == -1){
 		arg.style.behavior='url(#default#homepage)';
 		arg.setHomePage(urlName);
 		arg.href="#";
 		return true;
 	}
 	else {return false}
}

function swtch(num, imgname) {
	if (getAppVersion()) {
		document[imgname].src = img[num].src;
	}
}

function swtch2(num, imgname) {
	if (getAppVersion()) {
		document[imgname].src = img2[num].src;
	}
}

function swtch3(num, imgname) {
	if (getAppVersion()) {
		document[imgname].src = img3[num].src;
	}
}

function arrayStep(num, dir, imgname) {
	if (getAppVersion()) {
	
	if (dir=="next"){
		if (imgpos==img.length-1){
			imgpos=1
		}
		else {
			imgpos=num+1;
			}
		}
	else	{
		if (imgpos<=1){
			imgpos=img.length-1;
		}
		else{
			imgpos=num-1;
		}
	}

	document[imgname].src = img[imgpos].src;
	}
}

function rollover(imgpath,imgName) {
	var newSrc = imgpath + "_ov.gif"
	document.getElementById(imgName).src = newSrc
}

function imgReplace(imgpath,imgName) {
	var newSrc = imgpath
	document.images[imgName].src = newSrc
}

function rollout(imgpath,imgName) {
	var newSrc = imgpath + ".gif"
	document.images[imgName].src = newSrc
}

function OpenWindow(theURL,winName,features) { 
//featureStr = 'toolbar=yes,scrollbars=yes,resize=yes,location=no,menubar=yes,width=700,height=560' 
  featureStr = features;
  opener = window.open(theURL,winName,featureStr);
}

function openWindow350450(theURL,winName) { 
	featureStr = 'toolbar=no,scrollbars=yes,resize=no,location=no,menubar=no,width=350,height=450' 
 // featureStr = features;
  opener = window.open(theURL,winName,featureStr);
}

function openWindow600600(theURL,winName) { 
	featureStr = 'toolbar=no,scrollbars=yes,resize=no,location=no,menubar=no,width=600,height=600' 
 // featureStr = features;
  opener = window.open(theURL,winName,featureStr);
}

function openWindow600600Resize(theURL,winName) { 
	featureStr = 'toolbar=no,scrollbars=yes,resize=yes,location=no,menubar=no,width=600,height=600' 
 // featureStr = features;
  opener = window.open(theURL,winName,featureStr);
}

 function showHideExplorer(){
  if(document.body) var iWidth = document.body.clientWidth;
  if(iWidth > 950) 
		{
		document.getElementById('explorerSpan').style.display = "";
		
		
		}
		else
		{
			 document.getElementById('explorerSpan').style.display = 'none';
		}
 }

function makedate(formName,dateField){
	var dayval;
	
	if ("undefined" == typeof(document.forms[formName].elements['day_' + dateField])) {
		dayval = '01';
	} else {
		dayval = document.forms[formName].elements['day_' + dateField].value
	}
	document.forms[formName].elements[dateField].value = dayval + " " + document.forms[formName].elements['month_' + dateField].value + " " + document.forms[formName].elements['year_' + dateField].value;
}

function loaddate(formName,dateField){
   dateStr = document.forms[formName].elements[dateField].value
   dateArr = dateStr.split(" ",4)
   
    document.forms[formName].elements['day_' + dateField].value = dateArr[0]
    document.forms[formName].elements['month_' + dateField].value = dateArr[1]
    document.forms[formName].elements['year_' + dateField].value = dateArr[2]
    // alert(document.forms[formName].elements['month_' + dateField].selectedIndex)
}


// XML DATE VARIANTS
function makeXmlDate(formName,dateField){
	var f = document.forms[formName]
	var dayval;
	
	if ("undefined" == typeof(f.elements['day_' + dateField])) {
		dayval = '01';
	} else {
		dayval = f.elements['day_' + dateField].value
	}
	
	f.elements[dateField].value = f.elements['year_' + dateField].value + "-" + f.elements['month_' + dateField].value + "-" + dayval;
	//alert(f.elements[dateField].value)
}

function loadXmlDate(formName,dateField){
	var f = document.forms[formName]
		
   dateStr = f.elements[dateField].value
   dateArr = dateStr.split("-",4)
   
    f.elements['day_' + dateField].value = dateArr[2].substr(0,2)
    f.elements['month_' + dateField].value = dateArr[1]
    f.elements['year_' + dateField].value = dateArr[0]
    // alert(document.forms[formName].elements['month_' + dateField].selectedIndex)
}

function makedatemmyy(formName,dateField){
	document.forms[formName].elements[dateField].value = document.forms[formName].elements['month_' + dateField].value + " " + document.forms[formName].elements['year_' + dateField].value;
}

function loaddatemmyy(formName,dateField){
   dateStr = document.forms[formName].elements[dateField].value
   dateArr = dateStr.split(" ",4)
   
    document.forms[formName].elements['month_' + dateField].value = dateArr[0]
    document.forms[formName].elements['year_' + dateField].value = dateArr[1]
    // alert(document.forms[formName].elements['month_' + dateField].selectedIndex)
}

function passImgToForm(targetFeild){

	cUrl = document.forms['imageDetailsForm'].elements['cURL'].value
	cAlt = document.forms['imageDetailsForm'].elements['cAlt'].value
	cWidth = document.forms['imageDetailsForm'].elements['cWidth'].value
	cHeight = document.forms['imageDetailsForm'].elements['cHeight'].value
	cName = document.forms['imageDetailsForm'].elements['cName'].value
	cImgHtml = '<img src="' + cUrl + '" width="' + cWidth + '" height="' + cHeight + '" alt="' + cAlt + '"'
	if (cName != ''){
		cImgHtml = cImgHtml + ' class="' + cName + '"'
		}
	cImgHtml = cImgHtml + '/>'
	opener.document.forms['pageContentForm'].elements[targetFeild].value = cImgHtml
	window.close()
}

function passFilePathToForm(targetFeild, filepath){

	opener.document.forms['pageContentForm'].elements[targetFeild].value = filepath
	window.close()
}

// Links dropdown
function getSelect(s) {
  return s.options[s.selectedIndex].value
}

// --------------------------------- Date and Time ----------------------------------

function screenDate(){
// Store the date in a variable
d = new Date()
dateText = ""

// Get the current day and convert it to the name of the day
dayValue = d.getDay()
if (dayValue == 0)
    dateText += "Sunday"
else if (dayValue == 1)
    dateText += "Monday"
else if (dayValue == 2)
    dateText += "Tuesday"
else if (dayValue == 3)
    dateText += "Wednesday"
else if (dayValue == 4)
    dateText += "Thursday"
else if (dayValue == 5)
    dateText += "Friday"
else if (dayValue == 6)
    dateText += "Saturday"

// Get the current month and convert it to the name of the month
monthValue = d.getMonth()
dateText += " "
if (monthValue == 0)
    dateText += "January"
if (monthValue == 1)
    dateText += "February"
if (monthValue == 2)
    dateText += "March"
if (monthValue == 3)
    dateText += "April"
if (monthValue == 4)
    dateText += "May"
if (monthValue == 5)
    dateText += "June"
if (monthValue == 6)
    dateText += "July"
if (monthValue == 7)
    dateText += "August"
if (monthValue == 8)
    dateText += "September"
if (monthValue == 9)
    dateText += "October"
if (monthValue == 10)
    dateText += "November"
if (monthValue == 11)
    dateText += "December"

// Get the current year; if it's before 2000, add 1900
if (d.getYear() < 2000) 
    dateText += " " + d.getDate() + ", " + (1900 + d.getYear())
else 
    dateText += " " + d.getDate() + ", " + (d.getYear())

// Get the current minutes
minuteValue = d.getMinutes()
if (minuteValue < 10)
    minuteValue = "0" + minuteValue

// Get the current hours
hourValue = d.getHours()

// Customize the greeting based on the current hours
if (hourValue < 12)
    {
    greeting = "Good morning!"
    timeText = " - " + hourValue + ":" + minuteValue + " AM"
    }
else if (hourValue == 12)
    {
    greeting = "Good afternoon!"
    timeText = " - " + hourValue + ":" + minuteValue + " PM"
    }
else if (hourValue < 17)
    {
    greeting = "Good afternoon!"
    timeText = " - " + (hourValue-12) + ":" + minuteValue + " PM"
    }
else
    {
    greeting = "Good evening!"
    timeText = " - " + (hourValue-12) + ":" + minuteValue + " PM"
    }
// Write the greeting, the date, and the time to the page
// document.write(greeting + " It's " + dateText + timeText)
document.write(dateText + timeText)
}

var i=0
var intHide
var intShow

function ewShowMenu(elmnt)
{
//alert(elmnt);
document.getElementById(elmnt).style.visibility="visible"
//clearInterval(intHide)
//intShow=setInterval("show("+ elmnt +")",10)
}
function ewHideMenu(elmnt)
{
document.getElementById(elmnt).style.visibility="hidden"
//clearInterval(intShow)
//intHide=setInterval("hide("+ elmnt +")",10)
}

function ewShow(elmnt)
{
if (i>-100)
	{
	i=i-1
	document.getElementById(elmnt).style.bottom=i
	}
}

function ewHide(elmnt)
{
if (i<0)
	{
	i=i+1
	document.getElementById(elmnt).style.bottom=i
	}
}

  function areyousure(qText,link){
     var agree = false;
     agree = confirm(qText);
     if(agree){
      location=link
       }
    }
    
    
 

//define objects for the main list
function ListItem(nvalue,description){
        //function for defining the elements of the main list
        this.nvalue=nvalue;
        this.description=description;
}

//define objects for the dependent list
function ListSubItem(category,nvalue,description){
        //function for defining the elements of the sublists
        this.category=category;
        this.nvalue=nvalue;
        this.description=description;
}


function reFillList(formName,mainlist,sublist,ArrList){
 var selValue;
 var nOption;
 selValue=document.forms[formName].elements[mainlist][document.forms[formName].elements[mainlist].selectedIndex].value;
 
 // clear the actual list by setting its length to 0
 document.forms[formName].elements[sublist].length=0
 document.forms[formName].elements[sublist].options[0]=new Option('Please Select','0');
 for (var i=0; i < ArrList.length;i++){
        //fill the box with the values corresponding to
        //the category in the first box
       //alert(ArrList[i].category + " - " + selValue + "(" + mainlist + ")")
        if (Number(ArrList[i].category)==Number(selValue)) {
                nOption=document.forms[formName].elements[sublist].length;
                document.forms[formName].elements[sublist].options[nOption]=new Option(ArrList[i].description,ArrList[i].nvalue);
        }
 }
 document.forms[formName].elements[sublist].options[0].selected=true;
}

function reFillList1(){
 var selValue;
 var nOption;
 selValue=document.form1.sublist[document.form1.sublist.selectedIndex].value;
 //alert("Selected value=" +selValue);
 // clear the actual list by setting its length to 0
 document.form1.sublist1.length=0
 for (var i=0; i < SubList1.length;i++){
        //fill the box with the values corresponding to
        //the category in the first box
        if (SubList1[i].category==selValue) {
                nOption=document.form1.sublist1.length;
                document.form1.sublist1.options[nOption]=new Option(SubList1[i].description,SubList1[i].nvalue);
        }
 }
 document.form1.sublist1.options[0].selected=true;
 return;
}

function clearList(formname,ddName){

	//alert("options - " + document.forms[formname].elements[ddName].options.length)
	document.forms[formname].elements[ddName].options.length = 0
	return;
}

function populate(sId,formname,biglist) {
	//find sId in lowest level -- levels below are length = 0 or please select
	var sLevel;
	var nOptionIndex;
	var arr = new Array(0,0,0,0);
	var nId = document.forms[formname].elements[sId].value;
	for (var i=2;i >= 0;i--){
		for(var j=0;j < biglist[i].length;j++){
//			alert(i + ',' + j + '> ' + biglist[i][j].nvalue + ' == ' + nId + ' ?');
			if (Number(nId) == Number(biglist[i][j].nvalue)) {
//			alert('hiya');
				arr[i]= biglist[i][j].nvalue;
				nId = biglist[i][j].category;
				j = biglist[i].length;
			}
		}
	}
	//here we have our values for each of the 4 levels
	// just need to 'select' the right option in each 
	//alert( 'PgId is' + sId + 'array is...' + arr[0] + ',' + arr[1] + ',' + arr[2] + ',' + arr[3]);
	for (var i=0;i<3;i++){
		
		sLevel = 'level_' + (i + 1) + '_' + sId;
		nOptionIndex = 0;
	//alert(sLevel);
	//	alert(i+':'+ document.forms[formname].elements[sLevel].options.length);
		for (j=0;j<document.forms[formname].elements[sLevel].options.length;j++) {
	//		alert(j+' == ' + arr[i]);
			if (Number(document.forms[formname].elements[sLevel].options[j].value) == Number(arr[i])) {
				nOptionIndex = j;
				j = document.forms[formname].elements[sLevel].options.length;
			}
		}
		
		document.forms[formname].elements[sLevel].selectedIndex = nOptionIndex;
		//alert(i + '> selected ' + nOptionIndex);
		if (i < 2) {
			sLevelNext = 'level_' + (i + 2) + '_' + sId;
			reFillList(formname,sLevel,sLevelNext,biglist[i+1])
		}
	}
	return;
}

function setsId(formname,sId) {
	var sListName;
	var sLink;
	sLink = '';
	for (var i=3; i > 0;i--){
		sListName='level_' + i + '_' + sId;
	//	alert(sListName);
		if (document.forms[formname].elements[sListName].length!=0) {
			selValue=document.forms[formname].elements[sListName][document.forms[formname].elements[sListName].selectedIndex].value;
	//		alert('selValue=' + selValue + ',i=' + i);
			if (selValue!='0') {
				i=0;
				document.forms[formname].elements[sId].value = selValue;
			}
		} 
		//alert(document.forms[formname].elements[sId].value);
	}
	return;
}

function tmpsetsId(formname,sId) {
	var sListName;
	var sLink;
	sLink = '';
	for (var i=4; i > 0;i--){
		sListName='level_' + i + '_' + sId;
		alert(sListName);
		if (document.forms[formname].elements[sListName].length == 0) {
		} else {
			selValue=document.forms[formname].elements[sListName][document.forms[formname].elements[sListName].selectedIndex].value;
			sLink += '>';
			sLink += selValue;
		}
	}
	document.forms[formname].elements[sId].value = sLink
	return;
}



function checkvalues(){
//show the selected values
        var val1;
        var val2;
		var val3;
        var cString;
        val1=document.form1.mainlist[document.form1.mainlist.selectedIndex].value;
        val2=document.form1.sublist[document.form1.sublist.selectedIndex].value;
		val3=document.form1.sublist1[document.form1.sublist1.selectedIndex].value;
        cString="Main List=value:" + val1 + "-Description:"+MainList[val1].description
        cString+="\n"
        cString+="Sub List=value:" + val2+ "-Description:"+SubList[val2].description
        cString+="\n"
        cString+="Sub List1=value:" + val3+ "-Description:"+SubList1[val3].description

        alert(cString);
}   

function setEditableDropdown(editfield,formName,fieldName){
//show the selected values
        var lastIndex;
        lastIndex = document.forms[formName].elements[fieldName].options.length - 1;      
        document.forms[formName].elements[fieldName].options[lastIndex].selected=true;
        document.forms[formName].elements[fieldName][document.forms[formName].elements[fieldName].selectedIndex].value = editfield.value      
}


// Admin function for Shipping Options Valid Regions List
function checkShippingLocationsForm(oCurrent) {
	toggleShipLocList(oCurrent.parentNode, oCurrent.checked, true)
}

function toggleShipLocList(oLI, bChecked, bIsTopLevel) {
	
	var oChildren, oChild, oListChildren, i, j, bCheckbox
	
	// Check the children, if they exist
	oChildren = oLI.childNodes
	bCheckbox = false
	for(i=0;i<oChildren.length;i++){
		oChild = oChildren.item(i)
		if (oChild.nodeName == 'SPAN' && !bIsTopLevel) { (bChecked)?oChild.className = 'locationName locationDisabled':oChild.className = 'locationName' }		
		if (oChild.type == 'checkbox' && !bIsTopLevel) { oChild.disabled = bChecked; bCheckbox = oChild.checked;}
		if (oChild.nodeName == 'UL') { 
		    oListChildren = oChild.childNodes
		    for(j=0;j<oListChildren.length;j++){
				toggleShipLocList(oListChildren.item(j), (bChecked || bCheckbox), false)
			}
		}
	}
	
}

function setupShipLocUL(oUL) {
	var oChildren, oChild
	// Check the children LIs
	oChildren = oUL.childNodes
	for(i=0;i<oChildren.length;i++){
		oChild = oChildren.item(i)
		if (oChild.nodeName == 'LI') { setupShipLocList(oChild, false) }
	}
}

function setupShipLocList(oLI, bChecked) {
	
	var oChildren, oChild, oListChildren, i, j, bParCheck
	
	// Check the children, if they exist
	oChildren = oLI.childNodes
	bParCheck = bChecked
	for(i=0;i<oChildren.length;i++){
		oChild = oChildren.item(i)
		if (oChild.nodeName == 'SPAN') { (bChecked)?oChild.className = 'locationName locationDisabled':oChild.className = 'locationName' }		
		if (oChild.type == 'checkbox') { 
			oChild.disabled = bChecked; 
			if (!bParCheck) {bParCheck = oChild.checked}
		}
		if (oChild.nodeName == 'UL') {			
		    oListChildren = oChild.childNodes
		    for(j=0;j<oListChildren.length;j++){
				setupShipLocList(oListChildren.item(j), bParCheck)
			}
		}
	}
	
}

//validate forms, checking that all required fields/options have been specified
function validate(oForm)
{
	emailAdd=oForm.sEmail.value.indexOf("@");
//	txt=oForm.txt.value;
	submitOK="True";
	
	if (oForm.sCompany.value=="")
	{
		alert("Please enter your Company");
		submitOK="False";
	}

	if (emailAdd==-1) 
	{
		alert("Please enter a valid e-mail address");
		submitOK="False";
	}

	if (submitOK=="False")
	{
		return false;
	}
}

function formReset(oForm)
{
	oForm.reset()
}

function form_check_old(oForm) {
	return true;
}

function form_check(oForm) {
	var oElem,i,bValid,cCheckRadio,cTestName,oOptions,bSelected, nElemTypeGroup;
	var aRadioCheck = new Array();
	bValid = true;
	

	
	for (i=0; i<oForm.length;i++ ) {
		oElem = oForm.elements[i]
		
		switch (oElem.type) {
			case "text": nElemTypeGroup = 1; break;
			case "textarea": nElemTypeGroup = 1; break;
			case "checkbox": nElemTypeGroup = 3; break;
			case "radio": nElemTypeGroup = 3; break;
			case "select-one": nElemTypeGroup = 2; break;
			case "select-multiple": nElemTypeGroup =2; break;
			default: nElemTypeGroup = 1; break;
		}
		
		// Check if the element is required
		if (oElem.className.toLowerCase().indexOf("required")>=0) {
			switch (nElemTypeGroup) {
			
				case 1:
					if (form_check_value(oElem.value)) {
						form_alert("required",oElem)
						bValid = false
					}
					break;
				case 2: 
					if (oElem.selectedIndex < 0) {
						form_alert("required",oElem)
						bValid = false						
					}
					else if (form_check_value(oElem.options[oElem.selectedIndex].text)) {
						form_alert("required",oElem)
						bValid = false					
					}
					break;					
				case 3:
					// Check if the checkbox group has already been checked through
					cCheckRadio = "," + aRadioCheck.join(",") + ","
					cTestName = "," + oElem.name + ","
					if (cCheckRadio.indexOf(cTestName)<0) {
						
						// Not found - let's do the checks
						aRadioCheck.push(oElem.name)
						oOptions = document.getElementsByName(oElem.name)
						bSelected = false;
						if (oOptions.length>1) {
							for (i=0; i<oOptions.length;i++) { if(oOptions[i].checked) { bSelected = true; break } }
						}
						if (!bSelected && oOptions.length>1) {
							form_alert("required",oElem)
							bValid = false	
						}
					}
					break;		
			}
			if (!bValid) { break }
		}
	}
	
	if (bValid) {
		for (i=0; i<oForm.length;i++ ) {
			oElem = oForm.elements[i]		
			if (nElemTypeGroup == 1) {
				if (form_check_value(oElem.value)) {oElem.value = '';}
			}
		}
	}
	
	return bValid;
}

function form_alert(cAlertType,oElem) {

	var cLabel
	// Get the label	
	cLabel = form_get_label(oElem)
	switch (cAlertType) {
	
		case "required":
			if (cLabel=="") {
				alert("You have not all the required information")
			}
			else {
				alert("You have not any information in the field : " + cLabel)
			}
			oElem.focus()
			break;
	}
}


function form_get_label(oElem) {

	var cLabel, oLabels, i

	cLabel=""
	oLabels = document.getElementsByTagName("label")

	for (i=0; i<oLabels.length;i++ ) {
		if (oLabels[i].htmlFor == oElem.name) { cLabel = oLabels[i].firstChild.nodeValue; break; }
	}
	
	return cLabel;


}

function form_check_value(cValue) {

	var aKeyDefs = new Array("please enter","[please enter","[select ","please select","[please select")
	var bIsDefault,i
	
	bIsDefault = false
	cValue = cValue.toString()
	cValue = cValue.toLowerCase()
	
	for (i=0; i<aKeyDefs.length;i++ ) {
		if (cValue.substr(0,aKeyDefs[i].length) == aKeyDefs[i].toLowerCase()) { bIsDefault = true }
	}
	
	return bIsDefault
}

function pollCheck(o) { 
	var pVal;
	pVal = false;
	for(i=0;i<o.length; i++) {
		if (o.elements[i].type=='radio' && o.elements[i].checked) { pVal = true; }
	}
	if (!pVal) {alert('You have not selected anything to vote for!');}
	return pVal;
}

