/**
 * Place-A-Bid Application
 */


if( !window.getObject ) {
  function getObject( obj ) {
    var type = typeof(obj);
	obj = ( type.toLowerCase() == "string" )? document.getElementById(obj) : obj;

	return obj;
  }
}

/**
 * Functions for Displaying/Closing Form(s)
 */

if( !window.topScrolled ) {
function topScrolled() {
  var y;
  if (self.pageYOffset) {// all except Explorer
	y = self.pageYOffset;
  }
  else if (document.documentElement && document.documentElement.scrollTop) {// Explorer 6 Strict
	y = document.documentElement.scrollTop;
  }
  else if (document.body) {// all other Explorers
	y = document.body.scrollTop;
  }

  return y;
}
};

if( !window.widthScrolled ) {
function widthScrolled() {
  var x;
  if (self.pageXOffset) // all except Explorer
	x = self.pageXOffset;
  else if (document.documentElement && document.documentElement.scrollLeft) // Explorer 6 Strict
	x = document.documentElement.scrollLeft;
  else if (document.body) // all other Explorers
	x = document.body.scrollLeft;

  return x;
}
};

function formatPrice( obj ) {
  var field = getObject(obj);
  
  re = /[^\.\d]+/;
  val = field.value.replace( re, "" );
  if( val == "" ) val = 0.00;
  val = Math.round(val*100)/100;
  str = "" + val;
  
  val = ( str.indexOf(".") == -1 )? val + ".00" : val;
  str = "" + val;
  if( !str.match( /[\d]+\.\d\d/ ) ) val = val + "0";
  
  field.value = val;
}

/* Get the _addbid form */
function getAddBidForm()
{
  for( x=0; x < document.forms.length; x++ )
  {
	id = document.forms[x].getAttribute('id');
	name = document.forms[x].getAttribute('name');
	
	if( id && name )
	{	
	  temp = id.toLowerCase();
      if( temp.indexOf("placeabid") > -1 )
	  {
	    temp = name.toLowerCase();
	    if( temp.indexOf("addbid") > -1 )
	    {
	      return document.forms[x];
	    }
	  }
	}
  }
}

/* Set the onsubmit function for the refernced form */
function setOnSubmit( fForm )
{
  if( fForm )
    fForm.onsubmit = function() { return validatefrmPlaceabid_AddBid(fForm); };
}

window.onload = function() {
  setOnSubmit( getAddBidForm() );
}

function relative_uri()
{
  abs_url = document.URL;
  domain = document.domain;
  index = abs_url.indexOf(domain);
  relative_url = abs_url.substr( index+domain.length, abs_url.length-index+domain.length );
  
  return relative_url;
}


function validatefrmPlaceabid_AddBid( fForm )
{
  try
  {
    /* Check if the Validator Object Exists.  If not, error out of the Submit */
    startup_err = "An Error has occurred and we cannot process your request at this Time.\n\n";
    startup_err += "Please contact the system administrator at serveradmin@hardwoodbrokers.com.\n\n";
    startup_err += "We apologize for the inconvenience.\n\n";
    sig = "- Web Administrator";
  
    __ERRNUM__ = 99999;
    __ERROR__ = { "number":__ERRNUM__, "message":startup_err };
  
    if( !window.FormValidator ) { alert( startup_err + sig ); return false; }
  
    var validator = new FormValidator();
	
	//extra_prod_info
	applicable_mat_dir = "/area_rugs";
	applicable_mat_dir_dev = "/product-page-rug-int.php";
    request_uri = relative_uri();
	if( request_uri.indexOf(applicable_mat_dir) == 0 || request_uri.indexOf(applicable_mat_dir_dev) == 0 )
	{
	  /**
	   * Means the Page is Under Area Rugs,
	   * so the extra_prod_info field will need to be valdiated
	   */
	  if( !validator.isValidRequired( fForm.extra_prod_info.value, true ) ) {
        alert( "Rug Sizes " + validator.parseErrors().upperCaseWords() );
	    fForm.extra_prod_info.focus();
	    validator.errors.empty();
        return false;
      }	  
	}
	
	//bid_price
    if( !validator.isValidCurrency( fForm.bid_price.value, true ) ) {
      alert( "Bid Price " + validator.parseErrors().upperCaseWords() );
	  fForm.bid_price.focus();
	  validator.errors.empty();
      return false;
    }
	
	if( fForm.bid_price.value < 1 ) {
	  validator.errors.push("must be greater than or equal to $1.00.");
      alert( "Bid Price " + validator.parseErrors().upperCaseWords() );
	  fForm.bid_price.focus();
	  validator.errors.empty();
      return false;
    }
	
	//bid_quantity
	if( !validator.isValidNumeric( fForm.bid_quantity.value, true ) ) {
      alert( "Bid Quantity " + validator.parseErrors().upperCaseWords() );
	  fForm.bid_quantity.focus();
	  validator.errors.empty();
      return false;
    }

	if( fForm.bid_quantity.value < parseInt(fForm.min_quantity.value) ) {
	  units_label = fForm.units_label.value;
	  if( fForm.min_quantity.value > 1 )
	  {
		if( units_label == "rug" )
	      units_label = units_label + "s"; units_label = units_label.replace(/ss/, "s");
	  }
	  validator.errors.push("must be at least " + fForm.min_quantity.value + " " + units_label);
      alert( "Bid Quantity " + validator.parseErrors().upperCaseWords() );
	  fForm.bid_quantity.focus();
	  validator.errors.empty();
      return false;
    }
  
    //bidder_name
    if( !validator.isValidString( fForm.bidder_name.value, true ) ) {
      alert( "Your Name " + validator.parseErrors().upperCaseWords() );
	  fForm.bidder_name.focus();
	  validator.errors.empty();
      return false;
    }
  
    //bidder_email
    if( !validator.isValidEmail( fForm.bidder_email.value, true ) ) {
      alert( "Your Email " + validator.parseErrors().upperCaseWords() );
	  fForm.bidder_email.focus();
	  validator.errors.empty();
      return false;
    }
  
    //bidder_phone
    if( !validator.isValidPhone( fForm.bidder_phone.value, true ) ) {
      alert( "Your Phone " + validator.parseErrors().upperCaseWords() );
	  fForm.bidder_phone.focus();
	  validator.errors.empty();
      return false;
    }
  
    //captcha
    c = fForm.captcha.value.trim();

    if( c.length < 6 ) {
      alert( "Access Code Must be 6 Characters long" );
	  fForm.captcha.focus();
	  validator.errors.empty();
      return false;
    }
  
    return true;

  } catch(e) {
	additionally = "Additionally, the error encountered was:\n\n";
	additionally += e.message + "\n\n";
    alert( startup_err + additionally + sig );
	
	return false;
  }
}




