// JavaScript Document

var popup;
var validator_quiet = {
	// validates that the field value string has one or more characters in it
	isNotEmpty : function(elem) {
		var str = elem.value;
		var re = /.+/;
		if(!str.match(re)) {
			return false;
		} else {
			return true;
		}
	},
	//validates that the entry is a positive or negative number
	isNumber : function(elem) {
		var str = elem.value;
		var re = /^[-]?\d*\.?\d*$/;
		str = str.toString( );
		if (!str.match(re)) {
			return false;
		}
			return true;
	},
	// validates that the entry is 16 characters long
	isLen16 : function(elem) {
		var str = elem.value;
		var re = /\b.{16}\b/;
		if (!str.match(re)) {
			return false;
		} else {
			return true;
		}
	},
	// validates that the entry is formatted as an e-mail address
	isEMailAddr : function(elem) {
		var str = elem.value;
		var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
		if (!str.match(re)) {
			return false;
		} else {
			return true;
		}
	},
	// validate that the user made a selection other than default
	isChosen : function(select) {
		if (select.selectedIndex == 0 || select.options[select.selectedIndex].value == 'null') {
//			alert("Please make a choice from the list. ("+select.title+")");
			return false;
		} else {
			return true;
		}
	},
	// validate that the user has checked one of the radio buttons
	isValidRadio : function(radio) {
		var valid = false;
		for (var i = 0; i < radio.length; i++) {
			if (radio[i].checked) {
				return true;
			}
		}
	//	alert("Make a choice from the radio buttons.");
		return false;
	},
	focusElement : function(formName, elemName) {
	//	var elem = document.forms[formName].elements[elemName];
	//	elem.focus( );
	//	elem.select( );
	}
}

function initMyScroller() {
	// var scrollCont = $(".myScrollerContainer .myScrollerContent");
	// $(".myScrollerContainer .btnUp")
	// $(".myScrollerContainer .btnDown")
	//alert( 'Hi' );
	var interval = 5;
	var timer;
	var myInt;
	var is_scrolling = false;
	$$(".myScrollerControls .btnDown").addEvent('mousedown',function() {
		timer = function(){ 
			var pos = $('scrollBox1').getScroll().y;
			$('scrollBox1').scrollTo(0,pos+interval);
		};
		myInt = setInterval( timer, 1 );
	} );
	$$(".myScrollerControls .btnDown").addEvent('mouseup',function() { clearInterval(myInt); });
	$$(".myScrollerControls .btnDown").addEvent('mouseout',function() { clearInterval(myInt); });
	
	$$(".myScrollerControls .btnUp").addEvent('mousedown',function() {
		timer = function(){ 
			var pos = $('scrollBox1').getScroll().y;
			$('scrollBox1').scrollTo(0,pos-interval);
		};
		myInt = setInterval( timer, 1 );
	} );
	$$(".myScrollerControls .btnUp").addEvent('mouseup',function() { clearInterval(myInt); });
	$$(".myScrollerControls .btnUp").addEvent('mouseout',function() { clearInterval(myInt); });
}

function submit_recipient_info()
{
	if ( validator_quiet.isNotEmpty( $('recipient_name') ) && validator_quiet.isEMailAddr( $('recipient_email') ) )
	{
		$('submit_selection').submit();
	}
}

function show_gdc_banner()
{
	
}

	function init_control_panel(){
		var mySlide = new Fx.Slide('control_panel');
		$('control_toggle').addEvent('click', function(e){
			e = new Event(e);
			mySlide.toggle();
			e.stop();
		});		
		mySlide.hide();
		$('control_panel').className = 'shown';
	}
	function initSearchPanel() {
		//alert( 'Event Firing' );
		var mySlide = new Fx.Slide('search_panel').hide();
		document.getElementById('search_panel').style.visibility = 'visible';
		document.getElementById('search_panel').style.display = 'block';
						
		$('search_toggle').addEvent('click', function(e){
			e = new Event(e);
			mySlide.toggle();
			e.stop();
		});		
		mySlide.hide();
	};

/*************************************************/
/* SHOPPING CART FUNCTIONS */
/*************************************************/

function delete_cart_item( id )
{
	if (confirm('Delete this item? This change is permanent.'))
	{
		var el = document.getElementById('item_box_'+id);
		
		var myajax = new Request( {
							  url: "ajax_admin.php",
							  data: "action=delete_cart_item&id="+id,
							  method: 'get', 
							  onComplete: function() {
								  if (this.response.text == 'true') 
								  	el.parentNode.removeChild(el);
							  }
							  }).send();
	}
}

function delete_new_cart_item( id )
{
	var cont = $('new_item_container');	
	cont.removeChild($('new_item_'+id));
}

function add_cart_item()
{
	var cont = $('new_item_container');	
	
	var num = $('new_item_count').value;
	num = parseInt(num) + 1;
	$('new_item_count').value = num;
	
	var cats = "";
	var out = new Array();
	
	out.push("<img src='images/layout/delete.png' height=\"16\" width=\"16\" style=\"cursor: pointer;\" onclick=\"delete_new_cart_item('"+num+"');\" />");
	out.push("<table class='edit_cart_items_table'><tr><td>Item Title:</td><td colspan='5'>&nbsp;&nbsp;&nbsp;<input type='text' name=\"new_items["+num+"][title]\" style=\"width: 90%;\" /></td></tr>");
	out.push("<td>Category</td><td colspan='5'><div id='categories_"+num+"'>");
	out.push(cats);
//	for ($j=0;$j<count($cats);$j++) { <option value="<?= $cats[$j]['catIndex'] ?>" <? if ($cats[$j]['catIndex'] == $item['category']) { ?> selected="selected"<? } ?>><?=$cats[$j]['cat']?></option><? } 
	out.push("</select></td>");
	out.push("</tr><tr><td>Regular Price:</td><td>&nbsp;$<input type='text' name=\"new_items["+num+"][price]\" /></td>");
	out.push("<td>Ship (CA):</td><td>&nbsp;$<input type='text' name='new_items["+num+"][shipping_ca]' /></td>");
	out.push("<td>&nbsp;</td>");
	out.push("</tr><tr>");
	out.push("<td>Sale Price:</td><td>&nbsp;$<input type='text' name=\"new_items["+num+"][sale_price]\" /></td>");
	out.push("<td>Ship (US):</td><td>&nbsp;$<input type='text' name='new_items["+num+"][shipping_us]' /></td>");
	out.push("<td>SMP:</td><td> $<input type='text' name=\"new_items["+num+"][secondary_market]\" /></td>");
	out.push("</tr><tr>");
	out.push("<td>Sale End:</td><td>&nbsp;&nbsp;&nbsp;<input type='text' id='new_sale_end_"+num+"' name=\"new_items["+num+"][sale_date]\" style=\"width: 120px;\" /> <img src='images/layout/calendar.png' width='16' height='16' border='0' id='new_calendar_img_"+num+"' style=\"cursor: pointer;\" /></td>");
	out.push("<td>Framed Size:</td><td>&nbsp;&nbsp;<input type='text' name=\"new_items["+num+"][size]\" /></td>");
	out.push("<td>Issue Prices:</td><td>&nbsp;$<input type='text' name=\"new_items["+num+"][retail_value]\" /></td>");
	out.push("</tr></table>");
	
	var newel = document.createElement('div');
	newel.id = 'new_item_'+num;
	newel.className = "new_item_box";
	newel.style.backgroundColor = "rgb(221, 221, 221)";
	newel.style.marginTop = "30px";
	newel.style.marginRight = "10px";
	newel.style.marginBottom = "30px";
	newel.style.marginLeft = "10px";
	newel.style.padding = "5px";
	newel.innerHTML = out.join("");
	
	cont.appendChild(newel);
	
	var myajax = new Request( {
		  url: "ajax_admin.php",
		  data: "action=get_categories",
		  method: 'get', 
		  async : 'false',
		  onComplete: function() {
			  cats += "<select name=\"new_items["+num+"][category]\">";
			  cats += this.response.text;
			  cats += "</select>";
			  var itemel = document.createElement('div');
			  itemel.innerHTML = cats;
			  
			  $('categories_'+num).appendChild(itemel);
		  }
	  }).send();
     var settings = {
          tl: { radius: 10 },
          tr: { radius: 10 },
          bl: { radius: 10 },
          br: { radius: 10 },
          antiAlias: true,
          autoPad: true,
          validTags: ["div"]
      }

      var myBoxObject = new curvyCorners(settings, $('new_item_'+num));
      myBoxObject.applyCornersToAll();
	  
	  new DatePicker( $('new_sale_end_'+num), { additionalShowLinks: ['new_calendar_img_'+num], showOnInputFocus: false, format : '%Y-%m-%d', stickyWinToUse : StickyWin.Modal });
}
function apply_promo_code()
{
	var code = $('promo_code').value;
	var myajax = new Request( {
						  	url: "ajax_admin.php",
							data: "action=check_code&code="+code,
							method : 'get',
							onComplete : function() {
								window.location.reload();
							}
						  }).send();
}
/*************************************************/
/* BLOG FUNCTIONS */
/*************************************************/

function get_blog_entry( id )
{
	var el = $('curvy_0');

	var settings = {
	  tl: { radius: 10 },
	  tr: { radius: 10 },
	  bl: { radius: 10 },
	  br: { radius: 10 },
	  antiAlias: true,
	  autoPad: true,
	  validTags: ["div"]
	}
	obj.innerHTML = "<div style=\"text-align: center; padding: 10px;\"><br><br><br><br><br><img src='images/layout/wait28trans.gif'><br><br><br><br></div>";

	var myajax = new Request( {
						  url: "ajax_admin.php",
						  data: "action=get_entry&id="+id,
						  method : 'get',
						  onComplete : function() {
							  var newel = document.createElement('div');
							  newel.id = 'curvy_0';
							  newel.className = 'blog_box';
							  newel.innerHTML = this.response.text;
							  $('main_content').replaceChild(newel, el);
							  var myBoxObject = new curvyCorners(settings, $('curvy_0'));
							  myBoxObject.applyCornersToAll();
							  var myBoxObject1 = new curvyCorners(settings, $('curvy_1'));
							  myBoxObject1.applyCornersToAll();
							  window.scroll(0,0);
						  }
						  }).send();
	
}
/* ADMIN */
function delete_blog_entry( id )
{
	var myajax = new Request( {
						  	url: "ajax_admin.php",
							data: "action=delete_entry&id="+id,
							method : 'get',
							onComplete : function() {
								window.location.reload();
							}
						  }).send();
}

function show_enlarged( img )
{
	show_popup("", "<div style='text-align: center;'><br><br><br><img src='ajax_enlargement.php?img="+img+"'><br><br><br></div>", "null");	

	var myajax = new Request( {
						  url: "ajax_enlargement.php",
						  data: "img="+img,
						  method : 'get',
						  onComplete : function() {
							  
						  }
						  }).send();
}

function show_popup( title, myContent, callback )
{
//	alert ( content );
	var foot = "<div style='text-align: right;'>";
	if ( callback != "null" ) foot += "<img src='images/layout/btn_ok.jpg' style='cursor: pointer;' onclick=\""+callback+";\" />";
	foot += "&nbsp;<img src='images/layout/btn_cancel.jpg' class='closeSticky' style='cursor: pointer;'></div>";
	
	myContent = "<h3>"+title+"</h3>"+myContent + foot;
	
	popup = new StickyWin.Modal({
	  content: myContent, //a string or a DOM element
		width: '600px',
		allowMultiple : false,
		draggable: false,
		id : 'popup',
		position: 'center',
		destroyOnClose : true,
	  offset: {
			x: 0, /*over to the left*/
			y: -200 /*and down a little*/
	  },
	  modalOptions: {
		modalStyle: {
		'border' : '0px #999999 solid',
		'background-color' : '#000000',
		'opacity':.75,
		'width' : '100%',
		'height' : '100%'
		}
	  }
	});
}
function show_img_popup( title, image )
{
	var myContent = "<img src='images/pieces/"+image+"' />";
	var foot = "<div style='text-align: right;'>";
//	if ( callback != "null" ) foot += "<img src='images/layout/btn_ok.jpg' style='cursor: pointer;' onclick='"+callback+";' />";
	foot += "&nbsp;<img src='images/layout/btn_cancel.jpg' class='closeSticky' style='cursor: pointer;'></div>";
	
	content = content + foot;
	
	popup = new StickyWin.Modal({
	  content: myContent, //a string or a DOM element
		width: '600px',
		allowMultiple : false,
		draggable: false,
		id : 'popup',
		position: 'center',
		destroyOnClose : true,
	  offset: {
			x: 0, /*over to the left*/
			y: -100 /*and down a little*/
	  },
	  modalOptions: {
		modalStyle: {
		'border' : '0px #999999 solid',
		'background-color' : '#000000',
		'opacity':.75,
		'width' : '100%',
		'height' : '100%'
		}
	  }
	});
}

/*************************************************/
/* CORPORATE CLIENT FUNCTIONS */
/*************************************************/
function load_package()
{
	var cat = parseInt($("category").options[$("category").options.selectedIndex].value);
	var client = parseInt($("client_id").value);
	
	$('option_container').innerHTML = "";
	
	$('package_container').innerHTML = "<div style=\"text-align: center; padding: 10px;\"><br><br><br><br><br><img src='images/layout/wait28trans.gif'><br><br><br><br></div>";
	
	if (cat) {
		var myajax = new Request( {
							  url: "ajax_admin.php",
							  data: "action=load_package&cat="+cat,
							  method : 'get', 
							  onComplete: function() { $('package_container').innerHTML = this.response.text; } 
			}).send();
	} else {
		$('package_container').innerHTML = "";
	}
}
function load_pkgs_options()
{
	var cat = parseInt($("category").options[$("category").options.selectedIndex].value);
	var pkg = parseInt($("package").options[$("package").options.selectedIndex].value);
	var client = parseInt($("client_id").value);
	
	$('option_container').innerHTML = "";
	if (pkg) {
		$("option_container").innerHTML = "<div style=\"text-align: center; padding: 10px;\"><br><br><br><br><br><img src='images/layout/wait28trans.gif'><br><br><br><br></div>";
		var myajax = new Request( {
							  url: "ajax_admin.php",
							  data: "action=load_pkg_options&cat="+cat+"&package="+pkg,
							  method : 'get',
							  onComplete : function() {
								  $('option_container').innerHTML = this.response.text;
							  }, 
							  evalScripts : 'true' }).send();
	}
}
/*************************************************/
/* MAIN PAGE FUNCTIONS */
/*************************************************/

function auto_rotate() {
	if ( $('print_rotate').value == '1' ) {
		var id = $('print_ptr').value.toInt();
		if ( id == ($$('.print_image').length-1) ) id=0; else id++;
		hightlight_print( $('print_image_'+id.toString()) );
		setTimeout( "auto_rotate()" , 3600 );
	}
}

function hightlight_print( el ) {
	var id = el.id.split('_')[2];
	$$('.print_image').each( function(el) {
		var i = el.id.substr((el.id.length-1), 1);
		$('print_image_'+i).fade( 'out' );
		$('print_content_'+i).fade( 'out' );
		if ( $('print_tab_'+i).hasClass( 'on' ) ) $('print_tab_'+i).removeClass( 'on' );
		if ( !$('print_tab_'+i).hasClass( 'off' ) ) $('print_tab_'+i).addClass( 'off' );
	});
	$('print_ptr').value = id;
	var duration = 300;
	setTimeout( "$('print_image_"+id+"').fade( 'in' )", duration );
	setTimeout( "$('print_content_"+id+"').fade( 'in' )", duration );
	setTimeout( "$('print_tab_"+id+"').removeClass( 'off' )", duration );
	setTimeout( "$('print_tab_"+id+"').addClass( 'on' )", duration );
}
function hightlight_extra( el ) {
	var id = el.id.split('_')[2];
	var count = $$('.extra_tab').length;
	for ( var i=0; i<count; i++ ) {
		$('extra_'+i).fade( 'out' );
		if ( $('extra_tab_'+i).hasClass( 'on' ) ) $('extra_tab_'+i).removeClass( 'on' );
		if ( !$('extra_tab_'+i).hasClass( 'off' ) ) $('extra_tab_'+i).addClass( 'off' );
	}
	$('extra_ptr').value = id;
	var duration = 300;
	setTimeout( "$('extra_"+id+"').fade( 'in' )", duration );
	setTimeout( "$('extra_tab_"+id+"').removeClass( 'off' )", duration );
	setTimeout( "$('extra_tab_"+id+"').addClass( 'on' )", duration );
}



