

function RoundTo2dp(X) { return Math.round(X*100)/100 }
function RoundTo1dp(X) { return Math.round(X*10)/10 }
function RoundTo0dp(X) { return Math.round(X*1)/1 }
function roundTowardsZero(n){
	return (n<0?-1:+1)*Math.floor(Math.abs(n)) 
}


// Check whether string s is empty.
 function IsEmpty(aTextField) {
   if ((aTextField.value.length==0) ||
   (aTextField.value==null)) {
      return true;
   }
   else { return false; }
}

// Check whether string s is integer only.
function IsInteger(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

// Check whether string s is numeric only.
function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}
//
function IsNegativeNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNegativeNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNegativeNumber == true; i++) 
      { 
      if(i==0){
		Char = sText.charAt(i);
		if (Char=='-'){
			IsNegativeNumber = true;
		}
		else{
			IsNegativeNumber = false;
		}

      }
      else{      
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
			{
			IsNegativeNumber = false;
			}
		}
      }
   return IsNegativeNumber;
   
}

//Validate email address
function isValidEmail(str) 
{
   return (str.indexOf(".") > 0) && (str.indexOf("@") > 0);
 
}


function ResetAddress(ddlCity,txtCity,ddlSuburb,txtSuburb,txtStreet,txtContact,txtPhone,txtDesc,ddlAddress) 
{
  
    ddlSuburb.selectedIndex=0
    ddlSuburb.style.width = "110px"
    
    txtSuburb.style.width = "0px"
    txtSuburb.innerText = "" 
    
    ddlCity.selectedIndex=0
    ddlCity.style.width = "110px"
    
    txtCity.style.width = "0px"
    txtCity.innerText = "" 
    
    txtStreet.innerText = "" 
	txtContact.innerText = "" 
	txtPhone.innerText = "" 
	txtDesc.innerText = ""  
	
	if (ddlAddress)		
	{	
		ddlAddress.selectedIndex=0
	}
	
	return false
}

function ddlChangeFunction(ddlCity,txtCity,ddlSuburb,txtSuburb) 
{
    if (ddlCity.selectedIndex==0) 
    {
        ddlSuburb.style.width = "110px"
        txtSuburb.style.width = "0px"
        
        ddlCity.style.width = "110px"
        txtCity.style.width = "0px"
    }
    else
    {
		if (ddlCity.selectedIndex==ddlCity.length-1) 
		{
			txtSuburb.style.width = "110px"
			ddlSuburb.style.width = "0px"  
	        
			txtCity.style.width = "110px"
			ddlCity.style.width = "0px"
		}
		else
		{
			txtSuburb.style.width = "110px"
			ddlSuburb.style.width = "0px"   
	        
			ddlCity.style.width = "110px"
			txtCity.style.width = "0px"
        }
    }
		
}

function address(id,name,city,suburbID,suburb,street,contact,phone,instruction)
{
	var objAddress = new Object()
	objAddress.id = id
	objAddress.name = name
	objAddress.city = city
	objAddress.suburbID = suburbID
	objAddress.suburb = suburb
	objAddress.street = street
	objAddress.contact = contact
	objAddress.phone = phone
	objAddress.instruction = instruction
	objAddress.display = display
	return objAddress    	
}

function display(ddlCity,txtCity,ddlSuburb,txtSuburb,txtStreet,txtContact,txtPhone,txtDesc,ddlAddress)
{		
	with(ddlCity){
		for(var loopIndex = 0; loopIndex < length; loopIndex++){
			if(options[loopIndex].text == this.city) {				
				selectedIndex = loopIndex
				//window.alert("name: " + this.name +"selected index is:" + loopIndex)	
			}
		}
	}
	
	if (this.suburbID == -1 ){
        ddlSuburb.style.width = "0px" 
        txtSuburb.style.width = "110px"
        txtSuburb.innerText = this.suburb          	
	}
	else{
        txtSuburb.style.width = "0px"
        ddlSuburb.style.width = "110px"
		with(ddlSuburb){
			for(var loopIndex = 0; loopIndex < length; loopIndex++){
				if(options[loopIndex].text == this.suburb) {				
					selectedIndex = loopIndex
					//window.alert("name: " + this.name +"selected index is:" + loopIndex)	
				}
			}
		}
	}
	
	txtStreet.innerText = this.street
	txtContact.innerText = this.contact
	txtPhone.innerText = this.phone
	txtDesc.innerText = this.instruction
	
}

function getAddress(ddlCity,txtCity,ddlSuburb,txtSuburb,txtStreet,txtContact,txtPhone,txtDesc,ddlAddress)
{
		ddlCity.selectedIndex=0
		ddlCity.style.width = "110px"
		
		txtCity.style.width = "0px"
		txtCity.innerText = "" 
				
        txtSuburb.style.width = "0px"
        txtSuburb.innerText = ""  
        
        ddlSuburb.style.width = "110px"
        ddlSuburb.selectedIndex=0
               
        txtStreet.innerText = "" 
		txtContact.innerText = "" 
		txtPhone.innerText = "" 
		txtDesc.innerText = ""   

	if (ddlAddress.selectedIndex ==0) {
			
	}
	else{
	
		for(var loopIndex = 0; loopIndex < myArray.length; loopIndex++){
			if(myArray[loopIndex].id ==ddlAddress[ddlAddress.selectedIndex].value) {
				myArray[loopIndex].display(ddlCity,txtCity,ddlSuburb,txtSuburb,txtStreet,txtContact,txtPhone,txtDesc,ddlAddress)
			}
		}
	}
}



function OpenDialog1()
{	
	var dialog1 = showModalDialog("dialogbox1.htm",'',"dialogWidth:360px; dialogHeight:260px")
	
	if (dialog1) {		
		//alert("sdfsdf");		
	}
	else{
		window.location=window.location;  	
	}	
}

function OpenDialog2()
{	
	var dialog1 = showModalDialog("dialogbox2.htm",'',"dialogWidth:740px; dialogHeight:700px")
	if (dialog1) {		
		//alert("sdfsdf");		
	}
	else{
		window.location=window.location;  	
	}	
}

function OpenDialog3()
{	
	var dialog1 = showModalDialog("dialogbox3.htm",'',"dialogWidth:280px; dialogHeight:250px")
	if (dialog1) {		
		//alert("sdfsdf");		
	}
	else{
		window.location=window.location;  	
	}	
}
function DumbClick(){
return false
}	
function InitialPageFocus(){
window.focus()
}

function QuatityChanged(txtQuatity)
{	
	if(!IsNumeric(txtQuatity.value)) 
	{	 
		alert("Please enter numeric data onlybbb.");     
		txtQuatity.focus();
		return false;
	} 
	else
	{
		if(txtQuatity.value=="."){
			txtQuatity.innerText="0"
		}
		txtQuatity.innerText=RoundTo2dp(txtQuatity.value)		
	}    
	if(IsEmpty(txtQuatity)) 
	{ 
		txtQuatity.innerText="0"	
	}   
    
}
//this is on view request page
function Check_PayType()
{	
	IsValidate = true;
	if (document.getElementById("ddlPayment").selectedIndex==0){
			alert("Please select a payment type first.")
			document.getElementById("ddlPayment").focus();
			IsValidate = false;
			}
	
	return IsValidate	
}

function CheckCust_PayType()
{	
	IsValidate = true;
	if (!document.getElementById('lblCustID')){
		document.getElementById("txtSearch").focus();
		alert("Please select a custome first.")
		IsValidate = false;
		}
	else{		
		if (document.getElementById("ddlPayment").selectedIndex==0){
			alert("Please select a payment type first.")
			document.getElementById("ddlPayment").focus();
			IsValidate = false;
			}
		else{
			
		}
	}	
	return IsValidate	
}

function CountChar(txtBox,Max,Name)
{
	if(txtBox.value.length>Max)
	{
		alert( Name.toUpperCase() + ' maximum characters allowed '+Max+ ' !!')		
		txtBox.select();
		txtBox.focus();
		return false;
	}
}


function DefaultButtonClick(btn, event){
 if (document.all){
  if (event.keyCode == 13){
   event.returnValue=false;
   event.cancel = true;
   btn.click();
  }
 }
 else if (document.getElementById){
  if (event.which == 13){
   event.returnValue=false;
   event.cancel = true;
   btn.click();
  }
 }
 else if(document.layers){
  if(event.which == 13){
   event.returnValue=false;
   event.cancel = true;
   btn.click();
  }
 }
}


   //this is for image button 
function DefaultImageButtonClick(event){
 if (document.all){
  if (event.keyCode == 13){
   event.returnValue=false;
   event.cancel = true;
	document.getElementById("btnGo").focus(); 
	document.getElementById("btnGo").click();
  }
 }
 else if (document.getElementById){
  if (event.which == 13){
   event.returnValue=false;
   event.cancel = true;
	document.getElementById("btnGo").focus(); 
	document.getElementById("btnGo").click();
  }
 }
 else if(document.layers){
  if(event.which == 13){
   event.returnValue=false;
   event.cancel = true;
	document.getElementById("btnGo").focus(); 
	document.getElementById("btnGo").click();
  }
 }
}

//for remove enter key in a text box
function fnTrapKD(event){
 if (document.all){
  if (event.keyCode == 13){
   event.returnValue=false;
   event.cancel = true;
  // btn.click();
  }
 }
 else if (document.getElementById){
  if (event.which == 13){
   event.returnValue=false;
   event.cancel = true;
  // btn.click();
  }
 }
 else if(document.layers){
  if(event.which == 13){
   event.returnValue=false;
   event.cancel = true;
  // btn.click();
  }
 }
}


function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function ToggleHideBody(oObj)
{
	var oTarget = oObj.parentElement.parentElement.parentElement.parentElement.parentElement.nextSibling;
	var oImg = oObj.firstChild
						
	if ( oTarget.style.display == 'none' )
	{	
		oTarget.style.display = 'block';
		oImg.src="../images/opentriangle.gif" 	
		
	}else
	{
		oTarget.style.display = 'none';
		oImg.src="../images/triangle.gif" 	
	}

}

/*
   -- filter2Num  --
   ^^^^^^^^^^^^^^^^^

 This function can be called in two different ways (one argument or two)

 filter2Num( inString )

   ( 100 decimal places mode )
   Returns a valid Number no matter what goes in
   Eg 23.2363 or 23, 
 
 filter2Num( inString , numDecimalPlaces )

  ( truncates to  numDecimalPlaces -- always rounds down ! )
   Returns a valid Number no matter what goes in.
   Eg  23.23 or 23 where numDecimalPlaces = 2; 
   


*/
function filter2Num(  )
{ 

 var args = filter2Num.arguments;  
 instr = "" + args[0];

 if ( args.length == 2 )
	maxDecimalPlaces = parseInt( 0 + args[1]  );
 else
	maxDecimalPlaces = 100;

 zeroes=0;

 if (instr == "")
	  return 0;

 isANum = "";


 passedDecimalPoint = false;
 numDecimalPlaces = 0;

 for ( k=0 ; k < instr.length; k++ ) {

    check = instr.charAt(k)


    if (( check == "$" )&&( isANum.length > 1 ) ){
     	break;
    }

    if ( ( check == "." ) && ( !passedDecimalPoint ) ) {
      isANum = isANum + ".";
      passedDecimalPoint = true;
    }

    if ( ( k==0 ) && (check == "-" ) ) {
      isANum = "-" + isANum ;
    }

    if (  !isNaN(parseInt(check)) && ( check != "$" ) && ( check != "," )  ) {
    	isANum = isANum + check;
    }

    if ( passedDecimalPoint ) {
        numDecimalPlaces++;
        if (numDecimalPlaces > maxDecimalPlaces) 
           break;
    }

 }


 if ( isANum == ""  )
	return 0;
 else {
	temp = parseFloat( isANum );
  return temp;
  }

}



/*
   -- fmtPrice  --
   ^^^^^^^^^^^^^^^^^
// <!-- Original taken from Tim Wallace Tax Calculator, and a few changes
// <!-- The JavaScript Source http://javascript.internet.com -->
// COMPLETELY RE-WRITTEN to get rid of . when not needed and , at every 3rd digit by Shaun McCarthy on behalf of Helios Communications, and comments
*/

function fmtPrice(value) {


// WORK OUT THE ROUNDING

    if (Math.floor(value)<0) { // If it is a negative number
      DollarAmt = Math.ceil(value); 
      var cents=100*(Math.abs(value)-Math.abs( Math.ceil(value)))+0.5; // and cents is betweeen 99 and 00 ie: 9975
      if ( cents > 100 ) {
        DollarAmt--; // Reduce the dollar amount by 1
        cents = cents - 100; // And set up cents
      }
    }
    else { // Its a positive number, so rounding up means increasing value.
      DollarAmt = Math.abs(Math.floor(value));
      var cents=100*(value-Math.floor(value))+0.5; // is cents between 99 and 00 ie: 9975
      if ( cents > 100 ) {    
        DollarAmt++;  // increase the dollar amount
        cents = cents - 100; // reset cents.
      }
    }


// WORK OUT THE , 's

    DollarAmt = DollarAmt + ""; // type cast?!?
    
    if (DollarAmt.charAt(0)=='-') DollarAmt=DollarAmt.substr(1); // chop the signage.
    
    NewLookDollarAmt = "";
    for (x=DollarAmt.length-1; x>=3; x-=3) // go backwards through the dollar amt stopping at every third number
      NewLookDollarAmt = ',' + DollarAmt.charAt(x-2) + DollarAmt.charAt(x-1) + DollarAmt.charAt(x) + NewLookDollarAmt;
      // If we find a 3rd number, append a , to the front, the three numbers, and then anything we found before. iterative gives:
      // number = 123456789, x=9 ,NLDA = "", step, NLDA = ","+"7"+"8"+"9"+NLDA = ",789"
      // step, x=6, LDA = ",456,789"
      // step, x=3, outside of loop,
      
    for (y=x;y>=0;y--) NewLookDollarAmt = DollarAmt.charAt(y) + NewLookDollarAmt; // Append the first few numbers to the front


// WORK OUT THE DECIMAL POINT

    if (Math.floor(value) == value) { // Check if the value has no dec point.
      if (Math.floor(value) < 0) {
        return("-$"+ NewLookDollarAmt+".00"); // PLEASE NOTE: These RETURN they do not change RESULT
      }
      else {
        return("$"+ NewLookDollarAmt+".00");
      }
    }    
    if (Math.floor(value) < 0 )  { // if it does, draw it
       result ="-$" + NewLookDollarAmt +"."; // PLEASE NOTE: These change RESULT and DO NOT RETURN
    }
    else {
       result ="$" + NewLookDollarAmt +".";
    }
    
    result += Math.floor(cents/10); //
    result += Math.floor(cents%10);
    return result; // Coz this is where the returning happens, after appending the cents :)
}