/********************************************************************************
GLOBAL JAVASCRIPT FUNCTIONS

These are used in all pages in the New CMS site.
Please put functions in here that are likely to be used in many pages
//********************************************************************************/

function y2k(number)    { return (number < 1000) ? number + 1900 : number; }
function padout(number) { return (number < 10) ? '0' + number : number; }

var today = new Date();
var day   = today.getDate();
var month = today.getMonth();
var year  = y2k(today.getYear());
var currentdate = padout(day) + '/' + padout(month+1) + '/' + year;
delete today;

var calwindow = null;


/*--------------------------------------------------------------------------------
used to check/uncheck all the checkboxes
passed in:     allObject- the checkbox for 'all'
                  formObject- the name of the checkbox to be ticked
--------------------------------------------------------------------------------*/
function SelectAll(allObject, formObject) {
var blnIsChecked = false;

    //-- they currently have it checked so uncheck all
    if ( allObject.checked == false ) {
        for ( var i=0; i<formObject.length; i++ ) {
            formObject[i].checked = false;
        }
    } else {
        for ( var i=0; i<formObject.length; i++) {
            formObject[i].checked = true;
        }
    }
}

/*--------------------------------------------------------------------------------
used to check/uncheck all the checkboxes
passed in:     allObject- the checkbox for 'all'
                  formObject- the name of the checkbox to be ticked
--------------------------------------------------------------------------------*/
var SelectAllButtonChecked = false; //-- global variable
function SelectAllButton(formObject) {

    //-- they currently have it checked so uncheck all
    if ( SelectAllButtonChecked ) {
        for ( var i=0; i<formObject.length; i++ ) {
            formObject[i].checked = false;
            SelectAllButtonChecked = false;
        }
    } else { //-- check all the boxes
        for ( var i=0; i<formObject.length; i++) {
            formObject[i].checked = true;
            SelectAllButtonChecked = true;
        }
    }
}


/*--------------------------------------------------------------------------------
used to validate email addresses
passed in:		strEmail: the email as a string
returns:		true or false (used in valid script to test whether it exists or not
--------------------------------------------------------------------------------*/
function checkemail(strEmail) {
	var testresults;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	
	if ( filter.test(strEmail) ) {
		testresults=true;
	} else {
		testresults=false;
	}
	
	return testresults;
}

/*---------------------------------------------------------------------------------
used to display random magazine covers
passed in: nothing
returns: document.write
----------------------------------------------------------------------------------*/
function WriteRandomCovers(strBreak) {
	//-- array dec
	var arrProduct = new Array;
	var arrProductDesc = new Array;
	var arrRandom = new Array;
	
	//-- get random number
	var ran_unrounded = Math.random()*4;
	arrRandom[0] = Math.round(ran_unrounded);
	
	//-- get the second random number
	//-- ensure it is not a duplicate
	do {
		var ran_unrounded=Math.random()*3;
		arrRandom[1] = Math.round(ran_unrounded);
		
	} while (arrRandom[1] == arrRandom[0]);
	
	//-- add items to array
	arrProduct[0] = "AMM_Cover.jpg";
	arrProduct[1] = "CR_Cover.jpg";
	arrProduct[2] = "RS_Cover.jpg";
	arrProduct[3] = "AL_Cover.jpg";
	arrProduct[4] = "USL_Cover.jpg";
	
	arrProductDesc[0] = "Australias Mining Monthly Magazine Cover";
	arrProductDesc[1] = "Contractor Magazine Cover";
	arrProductDesc[2] = "ResourceStocks Magazine Cover";
	arrProductDesc[3] = "Australian Longwall Magazine Cover";
	arrProductDesc[4] = "American Longwall Magazine Cover";
	
	//-- remove the items from the array based on random number
	for (var intArrItem in arrRandom) {
		arrProduct.splice(arrRandom[intArrItem],1);
		arrProductDesc.splice(arrRandom[intArrItem],1);
	}
		
	//-- add items to the page
	document.write ("<br />");
	document.write ("<p align='left'>");
	//-- add random images from the array
	for (var intArrItem in arrProduct) {
		document.write("<a href='/static.asp?t=0&i=1#" + arrProductDesc[intArrItem] + "'><img border='0' src='/web_images/magcovers/" + arrProduct[intArrItem] + "' alt='" + arrProductDesc[intArrItem] + "' /></a>" + strBreak);
	}
	
	document.write("</p>");	
}



/*---------------------------------------------------------------------------------
used to display random magazine covers
passed in: nothing
returns: document.write
----------------------------------------------------------------------------------*/
function confirmdialog(strQuestion) 
{
  if (confirm(strQuestion)) 
  { 
    return true; 
  } 
  else 
  { 
    return false; 
  }
}

/*---------------------------------------------------------------------------------
used to open a new window
passed in: theURL,winName,features 
returns: nothing
----------------------------------------------------------------------------------*/
		
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

/*---------------------------------------------------------------------------------
Used to Close current window and reload the parent window
passed in: theURL,winName,features 
returns: nothing
----------------------------------------------------------------------------------*/

var pWin;
function setParent(){
    pWin = top.window.opener;
}

function reloadParent(){
   pWin.location.reload(true);
   this.window.close();
}


/* ------------------------------------------------------------------
	To Limit Text on a form element. 
	
	Requires a element to hold the text, and a text box to display the count.
------------------------------------------------------------------ */
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function checkChecked(checkBoxName, submitID, formName){
var objForm = document.forms[0].elements
 
  for(i=0;i<objForm.length;i++){
    if(objForm[i].type == "checkbox" && objForm[i].name.indexOf(checkBoxName)!= "-1" && objForm[i].checked){
		document.getElementById(submitID).disabled = false;
       return true;
	   
    }
  }
  document.getElementById(submitID).disabled = true;
  return false;
	/* alert(checkBox.checked); 
	var boxes
	boxes = document.formName.checkBoxName.length;
	alert(boxes);
	if (checkBoxName.checked) {
		
	} else {
		document.getElementById(submitID).disabled = true
	}
	*/
}

function exportToExcel(tableID){
	//alert("test");
	var tableInnerHTML;
	tableInnerHTML = document.getElementById(tableID).innerHTML;
	document.write("<form name=exportToExcel action=../../../../../commonlib/exportToExcel.asp method=POST>");
	document.write("<input type=hidden name=tableToExport value=" + escape(tableInnerHTML) + ">");
	document.write("<input type=hidden name=filename value=" + escape(tableID) + ">");
	document.write("<input type=submit value=Export /></form>");
}



