/*** preload ***/
var badBrowser;
var pixel = new Image();
pixel.src = "images/pixel.gif";

$(document).ready( function() {
   // change to target blank
   $("a[rel=blank]").attr( "target", "_blank" ).removeAttr( "rel" );

   // fix pngs
   badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");

   if( badBrowser ) {
      // get all pngs
      $( "img[src$=.png]" ).each( function() {
         if( !this.complete ) {
            this.onload = function() { fixPng( this ) };
         } else {
            fixPng( this );
         }
      } );

      $( ".background_png" ).each( function() {
         fixPngBackground( this );
		} );
   }

   // content scroll
   $("#content-scroll").animate( { height: getNewContentHeight() }, "slow" );
   $(window).resize( function() {
      $("#content-scroll").height( getNewContentHeight() );
   } );

   // dropdowns
   $( "li.dropdown" ).hover( function() {
                                $( "ul", this ).css( "display", "block" );
                             },
                             function() {
                                $( "ul", this ).css( "display", "none" );
                             } );

   // block right click
   $(document).bind( "contextmenu", function( e ) {
		e.preventDefault();
	});

   // preload images
   MM_preloadImages( 'images/support/artists_f2.gif',
                     'images/support/businesses_f2.gif',
                     'images/support/collectors_f2.gif',
                     'images/support/designers_f2.gif',
                     'images/support/galleries_f2.gif',
                     'images/support/general_f2.gif' );
} );

function fixPng( png ) {
   var src = png.src;
   if( !png.style.width ) { png.style.width = $( png ).width(); }
   if( !png.style.height ) { png.style.height = $( png ).height(); }
   
   // replace by pixel image
   png.onload = function() { };
   png.src = pixel.src;

   // set filter ( display original image )
   png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
}

function fixPngBackground( element ) {
   backgroundImg = $( element ).css( "background-image" );

   if( backgroundImg.indexOf( ".png" ) != -1 ) {
      src = backgroundImg.replace( 'url("', '' ).replace( '")', '' );
      $( element ).css( "background-image", "none" );

      // set filter ( display original image )
      element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
   }
}

function getNewContentHeight() {
   var windowHeight = ( $(window).height() > 612 ) ? $(window).height() : 612;
   var contentScrollHeight = windowHeight - 10 - 10 - 100 - 15 - 15 - 19;
   document.cookie = "scrollHeight=" + String( contentScrollHeight );
   return contentScrollHeight;
}


/*** image manipulation functions ***/
function MM_findObj(n, d) { //v4.01
   var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
   d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
   if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
   for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
   if(!x && d.getElementById) x=d.getElementById(n); return x;
}

/* Functions that swaps images. */
function MM_swapImage() { //v3.0
   var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
   var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

/* Functions that handle preload. */
function MM_preloadImages() { //v3.0
   var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


/*** validation of forms ***/
function validateNewsletter( theForm )
{
   var theInputs = theForm.elements;
   var count, blankInputs, anyBlankFields;
   
   anyBlankFields = false;
   blankInputs = 'the following fields are required.\n\n';
   
   for( count=0; count<theInputs.length; count++ )
   {
      if( ( ( theInputs[count].name == "email" ) ||
            ( theInputs[count].name == "first_name" ) ||
            ( theInputs[count].name == "last_name" ) ) &&
          ( theInputs[count].value == "" ) )
      {
         anyBlankFields = true;
         
         if( theInputs[count].name == "first_name" ) { blankInputs += "First Name"; }
         else if( theInputs[count].name == "last_name" ) { blankInputs += "Last Name"; }
         else if( theInputs[count].name == "email" ) { blankInputs += "Email"; }
         
         blankInputs += '\n';
      }
   }
   
   blankInputs += '\nplease enter information into these fields.';
   
   if( anyBlankFields )
   {
      hideMsgShowButton();
      alert( blankInputs );
   }
   else
   {
		if( validateCaptcha() )
		{
			theForm[ "company" ].value = theForm[ "first_name" ].value + " " + theForm[ "last_name" ].value;
			document.lead.submit();
		}
		else
		{
			hideMsgShowButton();
			alert( 'The captcha value you entered was incorrect. Please try again.' );
		}
   }
   
   return false;
}

function validateRequestInfo( theForm )
{
   var theInputs = theForm.elements;
   var count, blankInputs, anyBlankFields;
   
   anyBlankFields = false;
   blankInputs = 'The following fields are required.\n\n';
   
   for( count=0; count<theInputs.length; count++ )
   {
      if( ( ( theInputs[count].name == "first_name" ) ||
            ( theInputs[count].name == "last_name" ) ||
				( theInputs[count].name == "00N700000021eo9" ) || 
            ( theInputs[count].name == "email" ) ||
            ( theInputs[count].name == "phone" ) ) &&
          ( theInputs[count].value == "" ) )
      {
         anyBlankFields = true;
         
         if( theInputs[count].name == "first_name" ) { blankInputs += "First Name"; }
         else if( theInputs[count].name == "last_name" ) { blankInputs += "Last Name"; }
         else if( theInputs[count].name == "email" ) { blankInputs += "Email"; }
         else if( theInputs[count].name == "phone" ) { blankInputs += "Phone"; }
			else if(theInputs[count].name == "00N700000021eo9")  { blankInputs += "Interested In"; }
         
         blankInputs += '\n';
      }
   }

   blankInputs += '\nplease enter information into these fields.';
   
   if( anyBlankFields )
   {
      hideMsgShowButton();
      alert( blankInputs );
   }
   else
   {
		if( validateCaptcha() )
		{
			if(theForm[ "company" ].value == "")
			{
				theForm[ "company" ].value = theForm[ "first_name" ].value + " " + theForm[ "last_name" ].value;
			}

			document.lead.submit();
		}
		else
		{
			hideMsgShowButton();
			alert( 'The captcha value you entered was incorrect. Please try again.' );
		}
   }
   
   return false;
}

function validateSeekArt( theForm )
{
   var theInputs = theForm.elements;
   var count, blankInputs, anyBlankFields;
   
   anyBlankFields = false;
   blankInputs = 'The following fields are required.\n\n';
   
   for( count=0; count<theInputs.length; count++ )
   {
      if( ( ( theInputs[count].name == "first_name" ) ||
            ( theInputs[count].name == "last_name" ) ||
            ( theInputs[count].name == "email" ) ||
            ( theInputs[count].name == "company" ) ||
            ( theInputs[count].name == "city" ) ||
            ( theInputs[count].name == "state" ) ||
            ( theInputs[count].name == "00N700000026ris" ) ) &&
          ( theInputs[count].value == "" ) )
      {
         anyBlankFields = true;
         
         if( theInputs[count].name == "first_name" ) { blankInputs += "first name"; }
         else if( theInputs[count].name == "last_name" ) { blankInputs += "last name"; }
         else if( theInputs[count].name == "email" ) { blankInputs += "email"; }
         else if( theInputs[count].name == "company" ) { blankInputs += "company"; }
         else if( theInputs[count].name == "city" ) { blankInputs += "city"; }
         else if( theInputs[count].name == "state" ) { blankInputs += "state/province"; }
         else if( theInputs[count].name == "00N700000026ris" ) { blankInputs += "message"; }
         
         blankInputs += '\n';
      }
   }
   
   blankInputs += '\nplease enter information into these fields.';
   
   if( anyBlankFields )
   {
      hideMsgShowButton();
      alert( blankInputs );
   }
   else
   {
      document.lead.submit();
   }
   
   return false;
}

function validateSpecifyArt( theForm )
{
   var theInputs = theForm.elements;
   var count, blankInputs, anyBlankFields;
   
   anyBlankFields = false;
   atLeastOneChecked = false;
   blankInputs = 'the following fields are required.\n\n';
   
   for( count=0; count<theInputs.length; count++ )
   {
      if( ( ( theInputs[count].name == "name" ) ||
            ( theInputs[count].name == "company" ) ||
            ( theInputs[count].name == "phone" ) ||
            ( theInputs[count].name == "email" ) ||
            ( theInputs[count].name == "role_in_project" ) ||
            ( theInputs[count].name == "use_of_building" ) ||
            ( theInputs[count].name == "how_many_pieces" ) ) &&
          ( theInputs[count].value == "" ) )
      {
         anyBlankFields = true;
         
         if( theInputs[count].name == "name" ) { blankInputs += "name\n"; }
         else if( theInputs[count].name == "company" ) { blankInputs += "company\n"; }
         else if( theInputs[count].name == "phone" ) { blankInputs += "phone\n"; }
         else if( theInputs[count].name == "email" ) { blankInputs += "email\n"; }
         // else if( theInputs[count].name == "role_in_project" ) { blankInputs += "your role in project"; }
         // else if( theInputs[count].name == "use_of_building" ) { blankInputs += "use of the building"; }
         // else if( theInputs[count].name == "how_many_pieces" ) { blankInputs += "number of art pieces"; }
         
         // blankInputs += '\n';
      }

      if( ( theInputs[count].name == "type_of_art[]" ) && theInputs[count].checked )
      {
         atLeastOneChecked = true;
      }
   }

   if( !atLeastOneChecked )
   {
      anyBlankFields = true;
      blankInputs += "type of art";
      blankInputs += '\n';
   }
   
   blankInputs += '\nplease enter information into these fields.';
   
   if( anyBlankFields )
   {
      hideMsgShowButton();
      alert( blankInputs );
   }
   else
   {
      document.lead.submit();
   }
   
   return false;
}

function validateNakedWallsOffer( theForm )
{
   var theInputs = theForm.elements;
   var count, blankInputs, anyBlankFields;
   
   anyBlankFields = false;
   blankInputs = 'the following fields are required.\n\n';
   
   for( count=0; count<theInputs.length; count++ )
   {
      if( ( ( theInputs[count].name == "email" ) ||
            ( theInputs[count].name == "phone" ) ||
            ( theInputs[count].name == "first_name" ) ||
            ( theInputs[count].name == "last_name" ) ||
            ( theInputs[count].name == "street" ) ||
            ( theInputs[count].name == "city" ) ||
            ( theInputs[count].name == "state" ) ||
            ( theInputs[count].name == "zip" ) ) &&
          ( theInputs[count].value == "" ) )
      {
         anyBlankFields = true;
         
         if( theInputs[count].name == "email" ) { blankInputs += "email"; }
         else if( theInputs[count].name == "phone" ) { blankInputs += "phone"; }
         else if( theInputs[count].name == "first_name" ) { blankInputs += "first name"; }
         else if( theInputs[count].name == "last_name" ) { blankInputs += "last name"; }
         else if( theInputs[count].name == "street" ) { blankInputs += "address"; }
         else if( theInputs[count].name == "city" ) { blankInputs += "city"; }
         else if( theInputs[count].name == "state" ) { blankInputs += "state/province"; }
         else if( theInputs[count].name == "zip" ) { blankInputs += "zip/postal code"; }
         
         blankInputs += '\n';
      }
   }
   
   blankInputs += '\nplease enter information into these fields.';
   
   if( anyBlankFields )
   {
      alert( blankInputs );
   }
   else
   {
		document.lead.submit();
   }
   
   return false;
}

function validateCaptcha()
{
    challengeField = $("input#recaptcha_challenge_field").val();
    responseField = $("input#recaptcha_response_field").val();
    //alert(challengeField);
    //alert(responseField);
    //return false;
    var html = $.ajax({
		type: "POST",
		url: "common/recaptcha.php",
		data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
		async: false
    }).responseText;

    if(html == "success")
    {
        // $("#captchaStatus").html(" ");
        // Uncomment the following line in your application
        return true;
    }
    else
    {
        // $("#captchaStatus").html("Your captcha is incorrect. Please try again");
        Recaptcha.reload();
        return false;
    }
}


/*** hide button on post ***/
function hideMsgShowButton()
{
   document.getElementById( "the_submit_msg" ).style.display = 'none';
   document.getElementById( "the_submit_button" ).style.display = '';
}

function showMsgHideButton()
{
   document.getElementById( "the_submit_msg" ).style.display = '';
   document.getElementById( "the_submit_button" ).style.display = 'none';
}

