/* submit.js */

/* this should really be renamed common.js */

checkTextLimits = function( data ) {
   //parse data { { 'text01', 1, 20 }, { 'text02', 2, 30 }, { 'text03', 2, 24|12 } }
   var truncated = false;
   for ( var i = 0; i < data.length; i++ ) {
       var el = document.getElementById( data[i][0] );
       var orgTxt = el.value;
       //trim trailing whitespace off lines of orgTxt
       var lines = orgTxt.split( '\n' );
       var orgAr = new Array();
       for ( var j = 0; j < lines.length; j++ ) {
          orgAr[j] = lines[j].replace( /\s+$/, '' );
       }
       orgTxt = orgAr.join( '\n' );
       el.value = limitText( el.name, el.value, data[i][1], data[i][2].toString().split( '|' ) );
       
       if ( orgTxt.toString().replace( /[\r\n]/g, '' ) != el.value.toString().replace( /[\r\n]/g, '' ) ) { 
          el.focus();
          jAlert( 'All of your text did not fit. Please review it.' );
          return false;
       }
   }
   return true; 
}

limitText = function( fld, txt, lc, ll ) {
  var lines = txt.split( '\n' );
  var txtAr = new Array();
  if ( ll.length > 1 ) {
     lim = lines.length < ll.length ? lines.length : ll.length;
     for ( var i = 0; i < lim; i++ ) {
        txtAr[i] = lines[i].substring( 0, ll[i] ).replace( /\s+$/, '' );
     }
  } else {
     lim = lines.length < lc ? lines.length : lc;
     for ( var i = 0; i < lim; i++ ) {
        txtAr[i] = lines[i].substring( 0, ll[0] ).replace( /\s+$/, '' );
     }
  }
  //reassemble text
  var nt = txtAr.join( '\n' );
  return nt;
}

testLimitsAdd = function( data ) {
   if ( ! checkTextLimits( data ) ) {
      setOrAddElement( 'theForm', 'natc', 'false' );
   }
   return true;
}  

chkForm = function( data ) {
  if ( document.theForm.Quantity.value <= 0 ) {
    alert( 'Please choose a quantity.' );
    return false;
  }
  return testLimitsAdd( data );
}

function changePic( picName, imgName) {
   if ( document.images ) {
      document[picName].src= imgName;
   }
}
 
function changeText( id, newtext ) {
   document.getElementById(id).innerHTML=newtext
}
 
function changeLink( id, newlink ) {
   document.getElementById(id).href=newlink
}

function changeStock( ) {
   document.theForm.submit();
}
 
function Scale2 ( selectedScale )
{
  document.theForm.scale2.value = selectedScale ;
  document.theForm.submit() ;
}
function QuarkDocColor2 ( selectedQuarkDocColor )
{
  document.theForm.QuarkDocColor2.value = selectedQuarkDocColor ;
  document.theForm.submit() ;
}

function sendMessage( messageText ) {
  //this should be nicer than an alert.
  var messageElement = document.getElementById( 'sp_message' );
  if ( typeof document.body.style.maxHeight == "undefined" ) {
    document.getElementById( 'hideSelect' ).style.display = 'block';
  }
  if ( messageText == '' ) {
    messageElement.innerHTML = '';
    messageElement.style.padding = 0;
  } else {
    var msgHtml  = '<p style="text-align: center; padding: 20px;">' + messageText + '</p>';
       // msgHtml += '<p style="text-align: center; padding: 25px 10px;">';
       // msgHtml += '<input class="btn" type="button" value="OK" onclick="okMessage();" />';
       // msgHtml += '</p>';
    messageElement.innerHTML = msgHtml;
    messageElement.style.padding = '30px';
    messageElement.style.color   = '#990000';
  }
  messageElement.style.display = 'block';
  document.getElementById( 'sp_overlay' ).style.display = 'block';
  setTimeout( "okMessage()", 1900 );
}

function okMessage() {
  document.getElementById( 'sp_message' ).style.display = 'none';
  document.getElementById( 'sp_overlay' ).style.display = 'none';
  if ( document.getElementById( 'hideSelect' ) ) { document.getElementById( 'hideSelect' ).style.display = 'none'; }
}

function createInput( inName, inValue, inType ) {
  myInput = document.createElement( 'input' );
  myInput.setAttribute( "name",  inName );
  myInput.setAttribute( "value", inValue );
  myInput.setAttribute( "type",  inType );
  return myInput;
}

function setOrAddElement( inForm, inElement, inValue ) {
  var exists = false;
  for( var i = 0; i < document.forms.length; i++ ) {
    if ( document.forms[i].name == inForm ) {
      for ( var j = 0; j < document.forms[i].elements.length; j++ ) {
        if ( document.forms[i].elements[j].name == inElement ) {
          document.forms[i].elements[j].value = inValue;
          exists = true;
          break
        }
      }
      if ( !exists ) {
        document.forms[i].appendChild( createInput( inElement, inValue, 'hidden' ));
      }
    }
  }
}

function imageUpload( message_text ) {
  setOrAddElement( 'uploadFile', 'natc', 'false' );
  var umel = document.getElementById( 'uploadMessage' );
  if ( message_text == '' ) {
    umel.innerHTML = '';
    umel.style.padding = '0';
  } else {
    umel.innerHTML = message_text;
    umel.style.padding = '10px';
    umel.style.color = '#990000';
  }
  document.getElementById( 'sp_overlay' ).style.display = 'block';
  document.getElementById( 'sp_uploadImage' ).style.display = 'block';
  if ( typeof document.body.style.maxHeight == "undefined" ) {
    document.getElementById( 'hideSelect' ).style.display = 'block';
  }
  //transfer text fields from one form to the other
  var x = document.forms[0];

  for ( i = 0; i < x.elements.length; i++ ) {
    var xel = x.elements[i];
       
    var alreadyTransferred = false;
        
    var y = document.forms[1];
    
    if ( ( xel.type == 'text' ) || ( xel.type == 'textarea' ) || ( xel.type == 'hidden' ) ) {
      for( j = 0; j < y.elements.length; j++ ) {
        var yel = y.elements[j];
        if ( xel.name == yel.name ) {
           alreadyTransferred = true;
           yel.value = xel.value;
        }
      }
           
      if ( !alreadyTransferred ) {
         myInput = document.createElement( 'input' );
	       myInput.setAttribute( "name", xel.name );
	       myInput.setAttribute( "value", xel.value );
	       myInput.setAttribute( "type", "hidden" );
	       y.appendChild( myInput );
      }
    } 
  }  
}

function changeParam( parameter ) {
   document.getElementById( 'sp_overlay' ).style.display = 'block';
   document.getElementById( parameter ).style.display    = 'block';
   if ( typeof document.body.style.maxHeight == "undefined" ) {
    document.getElementById( 'hideSelect' ).style.display = 'block';
  }
} 

function changeCancel( element ) {
   $( '.spPanel' ).css( 'display', 'none' );
}

function clearChoicePanes() {
   $( '.spPanel' ).css( 'display', 'none' );
}

function changeCharms( charm1, charm2 ) {
 document.getElementById( 'sp_overlay' ).style.display = 'block';
 document.getElementById( 'sp_changeCharm' ).style.display = 'block';
   if ( typeof document.body.style.maxHeight == "undefined" ) {
    document.getElementById( 'hideSelect' ).style.display = 'block';
 }
 if ( charm1 != '' ) {
    document.getElementById( 'charm1_' + charm1 ).style.border = '1px solid #000';
    document.getElementById( 'charm1Check_' + charm1 ).src = '/images/charms/charms_check_on.gif';
 } else {
    document.getElementById( 'charm1_upper' ).style.border = '1px solid #000';
    document.getElementById( 'charm1Check_upper' ).src = '/images/charms/charms_check_on.gif';
    document.getElementById( 'charm1_lower' ).style.border = '1px solid #000';
    document.getElementById( 'charm1Check_lower' ).src = '/images/charms/charms_check_on.gif';
 }
 if ( charm2 != '' ) {
    document.getElementById( 'charm2_' + charm2 ).style.border = '1px solid #000';
    document.getElementById( 'charm2Check_' + charm2 ).src = '/images/charms/charms_check_on.gif';
 } else {
    document.getElementById( 'charm2_upper' ).style.border = '1px solid #000';
    document.getElementById( 'charm2Check_upper' ).src = '/images/charms/charms_check_on.gif';
    document.getElementById( 'charm2_lower' ).style.border = '1px solid #000';
    document.getElementById( 'charm2Check_lower' ).src = '/images/charms/charms_check_on.gif';
 }
 var borEl = document.getElementById( 'charmScrollBorder' );
 var bakEl = document.getElementById( 'charmScrollBackground' );
 borEl.scrollTop = document.getElementById( 'borderScrollTop' ).value;
 bakEl.scrollTop = document.getElementById( 'backgroundScrollTop' ).value;
 //alert( 'bor = ' + document.getElementById( 'borderScrollTop' ).value + '\nbak = ' + document.getElementById( 'backgroundScrollTop' ).value );
}

function selectCharm( charm, order ) {
   $( '.charms' + order + 'li' ).css( 'border', '' );
   $( '.charms' + order + 'img' ).attr( 'src', '/images/charms/charms_check_off.gif' );
   setOrAddElement( 'theForm', 'charm' + order , charm );
   if ( charm != '' ) {
      $( '#charm' + order + '_' + charm ).css( 'border', '1px solid #000' );
      $( '#charm' + order + 'Check_' + charm ).attr( 'src', '/images/charms/charms_check_on.gif' );
   } else {
      $( '#charm' + order + '_upper' ).css( 'border', '1px solid #000' );
      $( '#charm' + order + 'Check_upper' ).attr( 'src', '/images/charms/charms_check_on.gif' );
      $( '#charm' + order + '_lower' ).css( 'border', '1px solid #000' );
      $( '#charm' + order + 'Check_lower' ).attr( 'src', '/images/charms/charms_check_on.gif' );
   }
}

function bigTextChange() {
  var bigText = document.getElementById( 'bigText' ).value;
  setOrAddElement( 'theForm', 'text12', bigText );
  document.theForm.submit();
}

function styleChange( qd, styleID ) {
  setOrAddElement( 'theForm', 'quarkDoc', qd );
  setOrAddElement( 'theForm', 'StyleID', styleID );
  setOrAddElement( 'theForm', 'changeStyle', '1' );
  //setOrAddElement( 'theForm', 'recenter', 'true' );
  document.theForm.submit();
}

function monogramChange( monogram ) {
  setOrAddElement( 'theForm', 'monogram', monogram );
  setAltMonoCheckBox();
  document.theForm.submit();
}

function fontChange( fontName ) {
  setOrAddElement( 'theForm', 'iconFont', fontName );
  document.theForm.submit();
}

function colorChange( qd, colorID ) {
  setOrAddElement( 'theForm', 'quarkDoc', qd );
  setOrAddElement( 'theForm', 'iconColor', colorID );
  document.theForm.submit();
}

function iconChange( artFile ) {
  setOrAddElement( 'theForm', 'iconArt', artFile );
  document.theForm.submit();
}

function shapeChange( qd, ShapeID ) {
  setOrAddElement( 'theForm', 'quarkDoc', qd );
  setOrAddElement( 'theForm', 'shapeID', ShapeID );
  setOrAddElement( 'theForm', 'recenter', 'true' );
  setOrAddElement( 'theForm', 'changeShape', '1' );
  document.theForm.submit();
}

function imageUploadCancel() {
  $( '.spPanel' ).css( 'display', 'none' );
}

function imageUploadSubmit() {
  document.getElementById( 'sp_uploadImageMask' ).style.display = 'block';
  document.uploadFile.submit();
}

function imageChangeTo( imageFile ) {
  setOrAddElement( 'uploadFile', 'changeImage', imageFile );
  document.uploadFile.upload.value = "";
  document.uploadFile.submit();
}

function imageDelete( imageFile ) {
   setOrAddElement( 'uploadFile', 'deleteImage', imageFile );
   document.uploadFile.submit();
}

function addToBasketConfirm( saveValue ) {
   
   if ( !checkTextLimits( allTextParams ) ) addToBasketCancel(); 
  
    if ( document.theForm.Quantity.value != 0 ) {
        
        var x = document.getElementById( "Quantity" );
        
        xStr = x.options[x.selectedIndex].text;
        xAr = xStr.split( ' ' );
        
        qty = xAr[0];
        amt = xAr[ xAr.length - 2];
        //var shapeType = '';
        var shapeSize = '';
        var shapeName = '';
        
        shapeSize = document.getElementsByName( 'confirmAddShapeSize' )[0].value;
        shapeName = document.getElementsByName( 'confirmAddShapeName' )[0].value;
        
        if ( shapeName.indexOf( '(without water)' ) > 0 ) {
         shapeName = 'bottled water labels <small>(without water)</small>';
        }
        else if ( shapeName.slice(-1) != 's' ){
         shapeName = shapeName + 's';
        }
        
       if ( shapeName.indexOf( '(without water)' ) > 0 ) {
        xStr = qty + ' ' + shapeName + '<br />' + amt + ' &#8226; ' + shapeSize;
       } else {
        xStr = qty + ' ' + shapeName + ' &#8226; ' + amt + '<br />' + shapeSize;
       }
       confirmQTYStr = 'Your order is for ' + qty + ' ' + shapeName + '. ';
        
      document.getElementById( 'confirmAmount' ).innerHTML     = xStr;
      document.getElementById( 'sp_overlay' ).style.display    = 'block';
	     document.getElementById( 'sp_confirmAdd' ).style.display = 'block';
      
      if ( document.getElementById( 'sp_confirmQTYmsg_amount' ) ){
       document.getElementById( 'sp_confirmQTYmsg_amount' ).innerHTML = confirmQTYStr;
      }
	     if ( typeof document.body.style.maxHeight === 'undefined' ) {
  	       document.getElementById( 'hideSelect' ).style.display = 'block';
	     }
	     if ( document.theForm.saveValue ) {
	        document.theForm.saveValue.value = saveValue;
       }
	  } else {
	      alert('Please choose a quantity.');
	 }
}

function noImageNoAdd( msgTxt ) {
  document.getElementById( 'sp_overlay' ).style.display = 'block';
  document.getElementById( 'sp_confirmAdd' ).style.width = '280px';
  document.getElementById( 'sp_confirmAdd' ).innerHTML  = '<p style="text-align: center; padding: 10px;">' + msgTxt + '</p>';
  document.getElementById( 'sp_confirmAdd' ).innerHTML += '<p style="text-align: center;"><input type="button" class="btn"  value="OK" onclick="endNoAdd();" /></p>';
  document.getElementById( 'sp_confirmAdd' ).style.display = 'block';
}

function endNoAdd() {
  document.getElementById( 'sp_overlay' ).style.display = 'none';
  document.getElementById( 'sp_confirmAdd' ).style.display = 'none';
}

function addToBasketCancel() {
 document.getElementById( 'layoutAccept' ).checked = false;
 document.getElementById( 'prodAccept' ).checked = false;
 $( '.spPanel' ).css( 'display', 'none' );
}

function addToBasketContinue() {
  var lay, pro, def;
  
  if ( document.getElementById( 'sp_confirmWarnLayout' ) ) document.getElementById( 'sp_confirmWarnLayout' ).innerHTML = '';
  if ( document.getElementById( 'sp_confirmWarnProd' ) ) document.getElementById( 'sp_confirmWarnProd' ).innerHTML = '';
  if ( document.getElementById( 'sp_confirmWarnDefault' ) ) document.getElementById( 'sp_confirmWarnDefault' ).innerHTML = '';
  
  lay = document.getElementById( 'layoutAccept' ) ? document.getElementById( 'layoutAccept' ).checked : true;
  pro = document.getElementById( 'prodAccept' ) ? document.getElementById( 'prodAccept' ).checked : true;
  def = document.getElementById( 'defaultAccept' ) ? document.getElementById( 'defaultAccept' ).checked : true;
  
  if ( ! ( lay && pro && def ) ) {
    document.getElementById( 'sp_confirmWarnMsg' ).innerHTML = 'Please check all boxes below to continue';
    if ( ! lay ) document.getElementById( 'sp_confirmWarnLayout' ).innerHTML = '>>>';
    if ( ! pro ) document.getElementById( 'sp_confirmWarnProd' ).innerHTML = '>>>';
    if ( ! def ) document.getElementById( 'sp_confirmWarnDefault' ).innerHTML = '>>>';
    return false;
	}
	saveValue = document.theForm.saveValue.value;
	if ( saveValue == 'save' ) {
		if ( document.theForm.saveChanges ) {
			document.theForm.removeChild( document.theForm.saveChanges );
		}
		myInput = document.createElement( 'input' );
		myInput.setAttribute( "name", saveValue );
		myInput.setAttribute( "type", "hidden" );
		document.theForm.appendChild( myInput );
    } else if ( saveValue == 'saveChanges' ) {
	    if ( document.theForm.save ) {
		    document.theForm.removeChild( document.theForm.save );
	    }
	    myInput = document.createElement( 'input' );
		myInput.setAttribute( "name", saveValue );
		myInput.setAttribute( "type", "hidden" );
		document.theForm.appendChild( myInput );
   } else if ( saveValue == 'add' ) {
      //probably do nothing
   } else {
	    //do nothing
   }
   
  if ( document.getElementById( 'instr_txtArea' ) && ( document.getElementById( 'instr_txtArea' ).value != '' ) ) {
     setOrAddElement( 'theForm', 'instructions', document.getElementById( 'instr_txtArea' ).value );
  }
   
	document.theForm.submit();
}

function basketCheckout() {
	myInput = document.createElement( 'input' );
	myInput.setAttribute( "name", "checkout" );
	myInput.setAttribute( "type", "hidden" );
	document.basketForm.appendChild( myInput );
	document.basketForm.PageContent.value = 15;
	document.basketForm.submit();
}

function checkContactForm() {
	var email_regex = new RegExp( "^[a-zA-Z0-9_+.]+@[a-zA-Z0-9_+.]+[.][a-zA-Z]{2,4}$" );
 
	 if ( document.contactForm.name.value == '' ) {
	     alert( 'Please enter a name!' );
	     document.contactForm.name.focus();
	     return false;
     }
	       
     if ( ! email_regex.test( document.contactForm.email.value ) ) {
	     alert( 'Please enter a valid email address!' );
	     document.contactForm.email.focus();
         return false;
     }
     
     if ( document.contactForm.phone.value == '' ) {
	     alert( 'Please enter a phone number!' );
	     document.contactForm.phone.focus();
	     return false;
     }

     return true;
}

function checkJoinUsForm() {

	var email_regex = new RegExp( "^[a-zA-Z0-9_+.]+@[a-zA-Z0-9_+.]+[.][a-zA-Z]{2,4}$" );

 

	 if ( document.joinusform.company.value == '' ) {

	     alert( 'Please enter your company name' );

	     document.joinusform.company.focus();

	     return false;

     }


	 if ( document.joinusform.address1.value == '' ) {

	     alert( 'Please enter an address' );

	     document.joinusform.address1.focus();

	     return false;

     }


	 if ( document.joinusform.city.value == '' ) {

	     alert( 'Please enter a city' );

	     document.joinusform.city.focus();

	     return false;

     }


	 if ( document.joinusform.state.value == '' ) {

	     alert( 'Please select a state' );

	     document.joinusform.state.focus();

	     return false;

     }


	 if ( document.joinusform.zip.value == '' ) {

	     alert( 'Please enter a zip code' );

	     document.joinusform.zip.focus();

	     return false;

     }


	 if ( document.joinusform.website.value == '' ) {

	     alert( 'Please enter the URL to your website' );

	     document.joinusform.website.focus();

	     return false;

     }


	 if ( document.joinusform.contactFirstName.value == '' ) {

	     alert( 'Please enter a first name in the first name field' );

	     document.joinusform.contactFirstName.focus();

	     return false;

     }
     
     
	 if ( document.joinusform.contactLastName.value == '' ) {

	     alert( 'Please enter a last name in the first name field' );

	     document.joinusform.contactLastName.focus();

	     return false;

     }


	 if ( document.joinusform.phone.value == '' ) {

	     alert( 'Please enter your phone number' );

	     document.joinusform.phone.focus();

	     return false;

     }
	       

     if ( ! email_regex.test( document.joinusform.email.value ) ) {

	     alert( 'Please enter a valid email address' );

	     document.joinusform.email.focus();

         return false;

     }

	 if ( document.joinusform.password1.value == '' ) {

	     alert( 'Please enter a password' );

	     document.joinusform.password1.focus();

	     return false;

     }

	 if ( document.joinusform.password2.value == '' ) {

	     alert( 'Please enter a confirmation password' );

	     document.joinusform.password2.focus();

	     return false;

     }

	 if ( document.joinusform.password1.value != document.joinusform.password2.value ) {

	     alert( 'Your password and confirmation password do not match' );

	     document.joinusform.password1.focus();

	     return false;

     }

	 if ( document.joinusform.terms.checked == false ) {

	     alert( 'You must read and agree to the terms and conditions before you may continue' );

	     document.joinusform.terms.focus();

	     return false;

     }
     
     var shiptype = '';
     for ( var i = 0; i < document.joinusform.shiptype.length; i++ ) {
	     if ( document.joinusform.shiptype[i].checked ) {
		     shiptype = document.joinusform.shiptype[i].value;
	     }
     }
     
     if ( shiptype == '' ) {
	     
	     alert( 'You must select a shipping option' );

	     document.joinusform.shiptype[1].focus();

	     return false;
     }

     return true;

}

function openTOS() {
  document.getElementById( 'sp_uploadTerms' ).style.display = 'block';
  document.getElementById( 'sp_uploadImage' ).style.display = 'none';
}
function closeTOS() {
  document.getElementById( 'sp_uploadTerms' ).style.display = 'none';
  document.getElementById( 'sp_uploadImage' ).style.display = 'block';
}
function openIMAGETXT() {
  document.getElementById( 'sp_imagePrep' ).style.display = 'block';
  document.getElementById( 'sp_uploadImage' ).style.display = 'none';
}
function closeIMAGETXT() {
  document.getElementById( 'sp_imagePrep' ).style.display = 'none';
  document.getElementById( 'sp_uploadImage' ).style.display = 'block';
}

function setAltMonoCheckBox(){
  if( document.getElementById( 'mCheck' ) ){
   document.getElementById('mCheck').checked = 'true';
  }
}

function sp_confirmQTYmsg() {
 if( document.getElementById( 'sp_confirmQTYmsg' ) ){
  if( document.getElementById( 'prodAccept' ).checked ){
   document.getElementById( 'sp_confirmQTYmsg' ).style.visibility = 'visible';}
  else {
   document.getElementById( 'sp_confirmQTYmsg' ).style.visibility = 'hidden';
  }
 }
}