//A function to only allow numbers to be entered
function keyCheck(e, decimal)
{
   // the following block is to prevent numbers. could be modified to do the oppsite
//  var key = window.event ? e.keyCode : e.which;
//	var keychar = String.fromCharCode(key);
//	reg = /\d/;
//	return !reg.test(keychar);


	//With FireFox Support
	var KeyID = (window.event) ? event.keyCode : e.which;

	if((KeyID >= 65 && KeyID <= 90) ||
	   (KeyID >= 97 && KeyID <= 122) ||
	   (KeyID >= 33 && KeyID <= 47) ||
	   (KeyID >= 58 && KeyID <= 64) ||
	   (KeyID >= 91 && KeyID <= 96) ||
	   (KeyID >= 123 && KeyID <= 126))
	{
	  // check for "."

	  if (decimal == true & KeyID == 46)
	     return true;

		return false;
	}
  else
  {
    return true;
  }

}

function displaySubmitMsg( div_id )
{
  document.getElementById( div_id ).innerHTML='<img src="/tenders/images/ajax-loader1.gif"/><b> Please wait ...</b>';
}

function cleanCurrency(element)
  {

    var val = $(element).value

    var val = val.replace(/,/g , "");

    if (!isFinite(val)) {
        return val;
    }

    var s = ""+val, abs = Math.abs(val), _, i;

    if (abs > 1000) {
        _  = (""+abs).split(/\./);
        i  = _[0].length % 3 || 3;

        _[0] = s.slice(0,i + (val < 0)) +
               _[0].slice(i).replace(/(\d{3})/g,',$1');

        s = _.join('.');
    }

    $(element).value = s;
    return s;

  }

//This method replaces a substring with another one.
//It is part of the string object
String.prototype.replaceAll = function(strTarget, strSubString )
{
  var strText = this;
  var intIndexOfMatch = strText.indexOf( strTarget );

  // Keep looping while an instance of the target string
  // still exists in the string.
  while (intIndexOfMatch != -1)
  {
    // Relace out the current instance.
    strText = strText.replace( strTarget, strSubString )

    // Get the index of any next matching substring.
    intIndexOfMatch = strText.indexOf( strTarget );
  }

  // Return the updated string with ALL the target strings
  // replaced out with the new substring.
  return( strText );
}

//Useful to convert nnumbers to currency
function fixedRound(num)
{
  //return Math.round( num * 100 ) / 100 ;
  return Math.round(num * Math.pow(10,2))/Math.pow(10,2);
}


function submitReloadAndCloseModalbox( form_id )
{
  Modalbox.hide();
  document.getElementById(form_id).submit();
  pause(1000);
  //window.opener.location.reload();

}

// Function to pause for the given milliseconds
function pause(millisecondi)
{
  var now = new Date();
  var exitTime = now.getTime() + millisecondi;

  while(true)
  {
    now = new Date();
    if (now.getTime() > exitTime)
      return;
  }
}

// --------------------- common ajax functions ---------------------
/**
 * a generic function to update the number of items and the last added values.
 * This is used in adding rows dynamically to a form.
 */
function updateRowValues(num_items, last_added)
{
  var num = document.getElementById( num_items );
  var last = document.getElementById( last_added );

  last.value  = (last.value - 1) + 2;
  num.value  = (num.value - 1) + 2;

}



function deleteRow(section_to_update, num_items, row_id)
{
  var uploaded_files = document.getElementById( section_to_update );
  var item = document.getElementById( row_id );

  rows = uploaded_files.getElementsByTagName("tr");

  try
  {
    for(i=0; i < rows.length; i++)
    {
      if (rows[i].id == row_id)
      {
        uploaded_files.removeChild( rows[i] );
      }
    }
  }
  catch (err)
  {
//    txt="This will be fixed soon\n\n";
//    txt+="Click OK to continue.\n\n";
//    alert(txt);
  }

  var num = document.getElementById( num_items );
  num.value  = num.value - 1;
}

var ray={
  ajax:function(st)
  	{
  		this.show('load');
  	},
  show:function(el)
  	{
  		this.getID(el).style.display='';
  	},
  getID:function(el)
  	{
  		return document.getElementById(el);
  	}
}

// ------------ generic function for the autocomplete functionality ---

function check_country(suburb, post_code, state)
{
    var container = new Array()
    container[1] = suburb;
    container[2] = post_code;
    container[3] = state;

    var country = ($('signUp_country').value).split('_');

    if (country[1] == 'Australia')
    {
      $('location_container').style.display = 'block';
      /* for (i=1;i<container.length;i++)
      {
         $(container[i]).disabled = true;
         $(container[i]).value=' ';
         $(location).value = ' ';
      } */
    }
    else
    {
      $('location_container').style.display = 'none';
      /* for (i=1;i<container.length;i++)
      {
         $(container[i]).autocomplete=true;
         $(container[i]).value = '';
         $(container[i] + '_hidden').value = '';
         $(container[i]).disabled = false;
      } */
    }
}

//check/uncheck all checkboxes in a container
function selectAll( container )
{
   var input_boxes = document.getElementById( container ).getElementsByTagName( "input" );

   for(i=0; i < input_boxes.length; i++)
   {
     if (input_boxes[ i ].name == "select_all" )
       continue;

     if (input_boxes[ i ].type == "checkbox")
     {
       input_boxes[ i ].checked = document.getElementById("select_all").checked;
     }
   }
}

 var currentId = null;

 function Accordian(contentId) {
    var slideDown = 0.5;
    var slideUp = 0.5;

    contentId = document.getElementById(contentId);

    if (currentId != contentId) {
            if (currentId == null) {
                    new Effect.SlideDown(contentId, {duration: slideDown});
                    } else {
                    new Effect.SlideUp(currentId, {duration: slideUp});
                    new Effect.SlideDown(contentId, {duration: slideDown});
            }
            currentId = contentId;
    } else {
            new Effect.SlideUp(currentId, {duration: slideUp});
            currentId = null;
    }
 };


function toggleCheckBox( element_id )
{
   if (document.getElementById( element_id ).checked )
   {
     document.getElementById( element_id ).value = 1;
     document.getElementById( element_id ).checked=false;
   }
   else
   {
     document.getElementById( element_id ).value=0;
     document.getElementById( element_id ).checked=true;
   }
}

function checkCurrentId(current)
{
   var currentId = document.getElementById(current).value;

   if (currentId == 1)
   {
     document.getElementById(current).value = 0;
   /*  $('location_details').style.display = 'block';*/
     return true;
   }
   else
   {
     /*$('location_details').style.display = 'none';*/
     document.getElementById(current).value = 1;
     return false;
   }
}

var CSSColumns = {

    maxHeight: 0,

    els: new Array(),

    equalise: function(){

        for (var i=0;i<arguments.length;i++) if (!$(arguments[i])) return;

        for(var i=0;i<arguments.length;i++)

        {

            this.els.push($(arguments[i]));

        }

        this.maxHeight = this.calcMaxHeight();

        for(var i=0;i<this.els.length;i++){

            this.els[i].style.height = this.maxHeight + "px";

        }

    },

    calcMaxHeight: function(){

        var h = 0;

        for(var i=0;i<this.els.length;i++)

        {

            if(this.els[i].getHeight()>h)

            {

                h=this.els[i].getHeight();

            }

        }

        return h;

    }

}
function confirmMessage(message)
{
  return confirm(message);
}

