jQuery.noConflict();
$j = jQuery;
$j().ready(function(){
  //$j('#alrt').css('opacity',0).show();   // this should probably be done in the css.
  
  // wrap all the content inside of #mid-col-2 with a div.white
  if( $j('#mid-col-2').length && !$j('#mid-col-2 .white').length ) {
    if( !$j('#mid-col-2 .blank').length ) {
      $j('#mid-col-2').wrapInner('<div class="white"></div>');
    }
  }
    
  
  // the .white class is used to set a page's body to use the white background. if detected we update the rest of the skin.
  if( $j('.white').length ){
    $j('#bbot').show();              // turn on the sliver under the banner
    $j('#main').addClass('white');   // turn the background image on in main
    $j('<div id="main-bot"></div>').insertAfter('#main.white');
  }
 
  
});

var tymer;
function alrt( msg ){
  if( $j('#page').find('#alrt').length ){
    $j('#page').find('#alrt').remove();
    clearTimeout( tymer );
  }
  $j('#page').prepend( '<div id="alrt"></div>' );
  var str = '<p>';
      str += msg;
      str += '</p>';
  $j('#alrt').html( str );
  $j('#alrt').css({opacity:0,display:"block"});
  $j('#alrt').animate({ opacity: 0.90}, 700 );  tymer = window.setTimeout("$j('#alrt').animate({opacity: 0}, 1500, function(){ $j(this).remove() } )",4000);
  //$j('#alrt').fadeIn();    window.setTimeout("$j('#alrt').fadeOut()",4000);   // the simpler way to do it, but this leaves us with an opaque alert bar and that's just too severe.
}



function validateAddress_vEditAddress( e )
{
  // if all the address fields are inputted we make an ajax request to see if we can find it.
  var reqFields = [ 'address','city','State','zipcode' ];
  var RegExs = [ false,false,false,/^\d{5}([\-]\d{4})?$/ ];
  var elements = [];
  var ajaxIt = true;
  var data = {}
  for(var i=0;i<reqFields.length;i++){
    var f = $j(':input[name='+reqFields[i]+']');
    if( f.val() == '' ){
      ajaxIt = false;
    } else { 
      if( RegExs[i] ){
        ajaxIt = RegExs[i].test(f.val()); 
      }
    }
    elements.push($j(':input[name='+reqFields[i]+']'));
    data[reqFields[i]] = elements[i].val();
  }
  
  var cont = true;
  if( ajaxIt ){
    var myForm = $j( e.target );
    myForm.find('.submit')[0].disabled = true;
    // first display the ajax-loader graphic
    $j('.white form').append('<img src="/_img/ajax-loader-wt.gif" class="ajax-signal" />');
    cont = false;
    $j.ajax({
      type: "POST",
      url: "/_checkAddress.cfm",
      data: data,
      success: function(msg){
        $j( '.ajax-signal' ).remove();
        myForm.find('.submit')[0].disabled = false;
        var result = msg.replace(/^\s+|\s+$/g,"").split(',');
        if( result.length > 1 && result[0] == 1 ){
          $j('#fld-lat').val( parseFloat( result[1].replace(/^\s+|\s+$/g,"") ) );
          $j('#fld-lng').val( parseFloat( result[2].replace(/^\s+|\s+$/g,"") ) );
          myForm.unbind( 'submit',validateAddressProxy );
          myForm.submit();
        } else {
          alrt( 'The Address you entered could not be found. Check the fields highlighted below before proceeding.' );
          for(var i=0;i<elements.length;i++){
            elements[i].addClass( 'failed' );
          }
        }
      }
    });
  }
  return cont;
}


function validateAddress_mEditAddress( e )
{
  // if all the address fields are inputted we make an ajax request to see if we can find it.
  var reqFields = [ 'deliveryAddress1','deliveryCity','deliveryState','deliveryZipCode' ];
  var RegExs = [ false,false,false,/^\d{5}([\-]\d{4})?$/ ];
  var elements = [];
  var ajaxIt = true;
  var data = {}
  for(var i=0;i<reqFields.length;i++){
    var f = $j(':input[name='+reqFields[i]+']');
    if( f.val() == '' ){
      ajaxIt = false;
    } else { 
      if( RegExs[i] ){
        ajaxIt = RegExs[i].test(f.val()); 
      }
    }
    elements.push($j(':input[name='+reqFields[i]+']'));
    data[reqFields[i]] = elements[i].val();
  }
  
  var cont = true;
  if( ajaxIt ){
    var myForm = $j( e.target );
    myForm.find('.submit')[0].disabled = true;
    // first display the ajax-loader graphic
    $j('.white form').append('<img src="/_img/ajax-loader-wt.gif" class="ajax-signal" />');
    cont = false;
    $j.ajax({
      type: "POST",
      url: "/_checkAddress.cfm",
      data: data,
      success: function(msg){
        $j( '.ajax-signal' ).remove();
        myForm.find('.submit')[0].disabled = false;
        var result = msg.replace(/^\s+|\s+$/g,"").split(',');
        if( result.length > 1 && result[0] == 1 ){
          $j('#fld-lat').val( parseFloat( result[1].replace(/^\s+|\s+$/g,"") ) );
          $j('#fld-lng').val( parseFloat( result[2].replace(/^\s+|\s+$/g,"") ) );
          myForm.unbind( 'submit',validateAddressProxy );
          myForm.submit();
        } else {
          alrt( 'The Address you entered could not be found. Check the fields highlighted below before proceeding.' );
          for(var i=0;i<elements.length;i++){
            elements[i].addClass( 'failed' );
          }
        }
      }
    });
  }
  return cont;
}




function validateAddress( e )
{
  //console.log( 'validateAddress( e )' );
  // if all the address fields are inputted we make an ajax request to see if we can find it.
  var reqFields = [ 'deliveryAddress1','deliveryCity','deliveryState','deliveryZipCode' ];
  var RegExs = [ false,false,false,/^\d{5}([\-]\d{4})?$/ ];
  var elements = [];
  var ajaxIt = true;
  var data = {}
  for(var i=0;i<reqFields.length;i++){
    var f = $j(':input[name='+reqFields[i]+']');
    if( f.val() == '' ){
      ajaxIt = false;
    } else { 
      if( RegExs[i] ){
        ajaxIt = RegExs[i].test(f.val()); 
      }
    }
    elements.push($j(':input[name='+reqFields[i]+']'));
    data[reqFields[i]] = elements[i].val();
  }
  
  var cont = true;
  if( ajaxIt ){
    var myForm = $j( e.target );
    myForm.find('.submit')[0].disabled = true;
    // first display the ajax-loader graphic
    $j('.white form').append('<img src="/_img/ajax-loader-wt.gif" class="ajax-signal" />');
    cont = false;
    $j.ajax({
      type: "POST",
      url: "/_checkAddress.cfm",
      data: data,
      success: function(msg){
        $j( '.ajax-signal' ).remove();
        myForm.find('.submit')[0].disabled = false;
        var result = msg.replace(/^\s+|\s+$/g,"").split(',');
        if( result.length > 1 && result[0] == 1 ){
          $j('#fld-lat').val( parseFloat( result[1].replace(/^\s+|\s+$/g,"") ) );
          $j('#fld-lng').val( parseFloat( result[2].replace(/^\s+|\s+$/g,"") ) );
          myForm.unbind( 'submit',validateAddress );
          myForm.submit();
        } else {
          alrt( 'The Address you entered could not be found. Check the fields highlighted below before proceeding.' );
          for(var i=0;i<elements.length;i++){
            elements[i].addClass( 'failed' );
          }
        }
      }
    });
  }
  return cont;
}

function validateUsername( e )
{
  var proceed = false;
  var fld = $j( e.target );
  if( fld.val() != '' ){
    var regx = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    proceed = (regx.test(fld.val()));
    if( proceed ){    
      fld.parents('.label').prepend('<img src="/_img/ajax-loader-wt.gif" class="ajax-signal" />');
      $j.ajax({
        type: "POST",
        url: "/_checkEmail.cfm",
        data: "usernamejoin="+fld.val(),
        success: function(msg){
          $j( '.ajax-signal' ).remove();
          if( msg.replace(/^\s+|\s+$/g,"") != 1 ){
            alrt( 'Select a unique email address.' );
            fld.removeClass( 'pass' ).addClass( 'failed' );
            //fld.focus();
          } else {
            fld.addClass( 'pass' );
          }
        }
      });
    } else {
      fld.addClass('failed');
    }
  }
}


function validateVendorAddress( e )
{
  // if all the address fields are inputted we make an ajax request to see if we can find it.
  var reqFields = [ 'address','city','State','zipcode' ];
  var RegExs = [ false,false,false,/^\d{5}([\-]\d{4})?$/ ];
  var elements = [];
  var ajaxIt = true;
  var data = {}
  for(var i=0;i<reqFields.length;i++){
    var f = $j(':input[name='+reqFields[i]+']');
    if( f.val() == '' ){
      ajaxIt = false;
    } else { 
      if( RegExs[i] ){
        ajaxIt = RegExs[i].test(f.val()); 
      }
    }
    elements.push($j(':input[name='+reqFields[i]+']'));
    data[reqFields[i]] = elements[i].val();
  }
  
  var cont = true;
  if( ajaxIt ){
    //var myForm = $j( e.target );
    //myForm.find('.submit')[0].disabled = true;
    // first display the ajax-loader graphic
    $j('.white form').append('<img src="/_img/ajax-loader-wt.gif" class="ajax-signal" />');
    cont = false;
    $j.ajax({
      type: "POST",
      url: "/_checkAddress.cfm",
      data: data,
      success: function(msg){
        $j( '.ajax-signal' ).remove();
        //myForm.find('.submit')[0].disabled = false;
        var result = msg.replace(/^\s+|\s+$/g,"").split(',');
        if( result.length > 1 && result[0] == 1 ){
          $j('#fld-lat').val( parseFloat( result[1].replace(/^\s+|\s+$/g,"") ) );
          $j('#fld-lng').val( parseFloat( result[2].replace(/^\s+|\s+$/g,"") ) );
          step_frm.frmStep( 2 );
          $j(':input[name="step1F"]').unbind( 'click',validateVendorAddressProxy );
        } else {
          alrt( 'The Address you entered could not be found. Check the fields highlighted below before proceeding.' );
          for(var i=0;i<elements.length;i++){
            elements[i].addClass( 'failed' );
          }
          cont = false;
        }
      }
    });
  }
  return cont;
}



// ************************************************
// *
// *	get dbug.log functionality ready

dbug = {
	firebug: false, debug: false, log: function(msg) {},
	enable: function() { if(this.firebug) this.debug = true; dbug.log = console.debug; dbug.log('enabling dbug');	},
	disable: function(){ if(this.firebug) this.debug = false; dbug.log = function(){}; }
}
if (typeof console != "undefined") { // safari, firebug
	if (typeof console.debug != "undefined") { // firebug
		dbug.firebug = true; if(window.location.href.indexOf("debug=true")>0) dbug.enable();
	}
}
// ------------------------------------------------



jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

