/* Toggles image when corresponding checkbox is checked or unchecked */
function toggle_image( id, value, image, hover ) {
	if( value ) {
		MM_swapImage( id + '_image', '', '/img/' + hover, 1 );
	} else {
		MM_swapImage( id + '_image', '', '/img/' + image, 1 );
	}
}


/* Selects and deselects checkbox when corresponding image is clicked */
function toggle_checkbox( id, image, hover ) {
	var value = !document.getElementById( id ).checked;
	document.getElementById( id ).checked = value;
	toggle_image( id, value, image, hover );
}


/* Selects and deselects all diamond shape type search criteria */
function toggle_select_all( name, value, c ) {
	var f = document.getElementById('ProductSearchForm');
	var inputs = f.getElementsByTagName('input');
	for(var i=0;i<inputs.length;i++) {
		if( inputs[i].type == 'checkbox' && inputs[i].className == c ) {
			inputs[i].checked = value;
			if( inputs[i].onclick ) {
				inputs[i].onclick();
			}
		}
	}
}


/* Populate weight range fields */
function weight_range( value ) {
	var weight_range_values = value.split(',');
	document.getElementById('ProductCaratWeightMin').value = weight_range_values[0];
	document.getElementById('ProductCaratWeightMax').value = weight_range_values[1];
}


/* Selects and deselects all certification type search criteria */
function toggle_select_all_certs( name, value ) {
	var f = document.getElementById('certs');
	var inputs = f.getElementsByTagName('input');
	for(var i=0;i<inputs.length;i++) {
		if( inputs[i].type == 'checkbox' ) {
			inputs[i].checked = value;
			if( inputs[i].onclick ) {
				inputs[i].onclick();
			}
		}
	}
}


function highlightOn( rowId, checkboxId ) {
	if( !document.getElementById( checkboxId ).checked ) {
		document.getElementById( rowId ).style.backgroundColor = '#ffff66';
	}
}
function highlightOff( rowId, checkboxId ) {
	if( !document.getElementById( checkboxId ).checked ) {
		document.getElementById( rowId ).style.backgroundColor = '';
	}
}
function highlight( rowId, checkboxValue ) {
	if( checkboxValue ) {
		document.getElementById( rowId ).style.backgroundColor = '#ffff66';
	} else {
		document.getElementById( rowId ).style.backgroundColor = '';
	}
}
function highlightRow( rowId ) {
	if( document.getElementById( rowId ).style.backgroundColor ) {
		document.getElementById( rowId ).style.backgroundColor = '';
	} else {
		document.getElementById( rowId ).style.backgroundColor = '#ffff66';
	}
}



function validate_order() {
	if( document.getElementById( 'OrderAttnName' ).value &&
	( document.getElementById( 'OrderShippingType0' ).checked ||
	document.getElementById( 'OrderShippingType1' ).checked ||
	document.getElementById( 'OrderShippingType2' ).checked ) ) {

		return true;
	} else {
		alert( 'Please enter a name and select a shipping type' );
		return false;
	}
}

/* Selects and deselects all certification type search criteria */
function toggle_select_display_options( name, value ) {
	document.getElementById('ClientSiteOptionShowPhotos').checked = value;
	document.getElementById('ClientSiteOptionShowAvailability').checked = value;
	document.getElementById('ClientSiteOptionShowCertificates').checked = value;
}

function updateClock ( )
{
  var currentTime = new Date ( );

  var currentHours = currentTime.getHours ( );
  var currentMinutes = currentTime.getMinutes ( );
  var currentSeconds = currentTime.getSeconds ( );

  // Pad the minutes and seconds with leading zeros, if required
  currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
  currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

  // Choose either "AM" or "PM" as appropriate
  var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

  // Convert the hours component to 12-hour format if needed
  currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

  // Convert an hours component of "0" to "12"
  currentHours = ( currentHours == 0 ) ? 12 : currentHours;

  // Compose the string for display
  var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;

  // Update the time display
  document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}




Date.prototype.getWeek = function() {
	var onejan = new Date(this.getFullYear(),0,1);
	return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
} 








/*
 * This function shows or hides appropriate divs and fieldsets
 * based on user selection or pre-existing data
 */
function setupPage() {

	// If this product is enabled
	if($('ClientProductSettingEnabled1').checked) {

		// Show pricing options
		$('showPricing').show();

		// If pricing is enabled
		if($('ClientProductSettingShowPricing1').checked) {

			// Show markup options
			$('markupType').show();

			// Display appropriate markup ranges
			if($('ClientProductSettingMarkupTypeId1').checked) {
				$('flatRate').show();
				$('priceRange').hide();
				$('weightRange').hide();
			}
			if($('ClientProductSettingMarkupTypeId2').checked) {
				$('flatRate').hide();
				$('priceRange').hide();
				$('weightRange').show();
			}
			if($('ClientProductSettingMarkupTypeId3').checked) {
				$('flatRate').hide();
				$('priceRange').show();
				$('weightRange').hide();
			}

		// If pricing is disabled
		} else {

			// Hide markup options
			$('markupType').hide();

				// Hide markup ranges
				$('flatRate').hide();
				$('priceRange').hide();
				$('weightRange').hide();
		}
	
	// If this product is disabled
	} else {
		
		// Hide pricing options
		$('showPricing').hide();

			// Hide markup options
			$('markupType').hide();

				// Hide markup ranges
				$('flatRate').hide();
				$('priceRange').hide();
				$('weightRange').hide();
	}

	// If there is any unsaved input with errors, show that div
	$$('div.error').each(function(s) {
		s.show();
	});
}

/*
 * This function applies tooltips based on link's rel attribute
 */
function enableToolTips() {
	$$('a[rel]').each(function(element) {
		new Tip(element, element.rel, {
			style: 'protoblue',
			hook: { target: 'rightTop', tip: 'leftTop' },
			offset: { x: 4, y: 4 },
			stem: 'leftTop'
		});
	});
}

/*
 * This function shows or hides appropriate divs and fieldsets
 * based on user selection
 */
function setupFinishPage() {

	// On website
	if($('ClientSettingWhere1').checked) {

		// Hide bookmark
		$('bookmark').hide();

		// Show who question
		$('who').show();

		// Me
		if($('ClientSettingWho1').checked) {
			$('instructions').show();
			$('email').hide();

		// Someone else
		} else if($('ClientSettingWho0').checked) {
			$('instructions').hide();
			$('email').show();
		}

	// In store
	} else {
		$('bookmark').show();
		$('who').hide();
		$('email').hide();
		$('instructions').hide();
	}
}

/*
 * This function helps create bookmarks
 */
function bookmark(url, title) {
	if (window.sidebar) { // firefox
		window.sidebar.addPanel(title, url, "");
	}
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
	else if(document.all) { // ie
		window.external.AddFavorite(url, title);
	}
}
