
progress_uploaded = 'no';
selected_filename = 'none';
var valid_extensions = /(.jpg|.jpeg|.gif|.png|.tif|.tiff)$/i;
var stepthrough_which = '';
var stepthrough_element = '';
var stepthrough_showflasher = 'yes';
var is_order_ready = 'no';
var step_do_not_proceed = 'no';

function doOrder(ordertype) {
	if (is_order_ready == 'yes') {
		new Ajax.Request('assets/ajax.php?a=complete_order&type='+ordertype, { method: 'get'});
	}
	else {
	alert('Your order is not ready for submission. Please check your settings.');	
	}
}

function CheckExtension(filename)
{
if (valid_extensions.test(filename)) return true;
alert('The selected file is not a valid file type.');
return false;
}

function clickContinue(step) {
if (step == 'upload') {
	if (progress_uploaded != 'yes') {
		if (selected_filename != 'none') {
	upload();
	disableContinue(step);
		}
		else {
			alert('Please select a file using the Browse & Select A File button');
		}
	}
	else {
		
		goPanel('crop');
	}
}
}
function disableContinue(step) {
document.getElementById('continueButton_'+step).src = 'images/continue_d.jpg';	
}
upload_error = 'no';
upload_successful = 'no';
function enableContinue(step) {
	document.getElementById('continueButton_'+step).src = 'images/continue.jpg';

if (step == 'upload') {
	if (selected_filename != 'none' && progress_uploaded == 'yes') {
		//alert('1');
		stepthrough_which = 'step_1_browse';
		stepthrough_element = document.getElementById(stepthrough_which);
	continueStepThrough();	
	}
	else if (upload_error != 'yes' && progress_uploaded != 'yes') {
		//alert('2');
		// have selected file, now enabling continue button...
		if (upload_successful == 'yes') {
		// file uploaded successfully, continue ..	
		   stepthrough_which = 'continueButton_upload';
		   stepthrough_element = document.getElementById(stepthrough_which);
		   continueStepThrough();
		}
		else {
		// no file yet, lets ...hold off on celebrating..	
			stepthrough_which = 'step_1_browse';
			stepthrough_element = document.getElementById(stepthrough_which);
			continueStepThrough();
		}

	}
	else {
		//alert('3');
		// was an error, and going back to main element to try again.
		stepthrough_element.style.border="0px";
		stepthrough_which = 'step_1_browse';
		stepthrough_element = document.getElementById(stepthrough_which);
		upload_error = 'no';
		//continueStepThrough();
	}
}
else {
	    if (step == 'paper') { step_do_not_proceed = 'yes'; }
		continueStepThrough();	

}
	
}
function reloadSizes() {
new Ajax.Request('assets/ajax.php?a=sizes', {
  method: 'get',
  onSuccess: function(transport) {
    
    document.getElementById('sizeSelectorDiv').innerHTML = transport.responseText;
}});	
}
function goPanel(step) {
if (step == 'upload') {
TabbedPanels1.showPanel(0);	
}
else if (step == 'paper') {
	if (document.getElementById('ps_height_selection').value > 1) {
	 document.getElementById('tab_paper').style.display='list-item';
     TabbedPanels1.showPanel(2);
	 if (stepthrough_which == 'continueButton_paper') {
		 step_do_not_proceed = 'no';
		continueStepThrough(); 
		
	 }
	}
	else {
	alert('Please select the size for your printed image.');	
	}
}
else if (step == 'crop') {
	enableContinue('upload');
	document.getElementById('tab_print').style.display='list-item';
TabbedPanels1.showPanel(1);	

new Ajax.Request('assets/ajax.php?a=sizes', {
  method: 'get',
  onSuccess: function(transport) {
    
    document.getElementById('sizeSelectorDiv').innerHTML = transport.responseText;
}});
document.getElementById('ableHideUpload').innerHTML = document.getElementById('ableHideUploadTEXT').innerHTML;
progress_uploaded = 'yes';
}
else if (step == 'order') {
	papertype = document.getElementById('ps_ppaper').innerHTML;
	if (papertype.match('Canvas')) {
	Lightview.show({ href: 'color.php', rel: 'iframe', options: { width: 650, height: 500 }});
	}
	else {
			
			
	}

}
}
function updateReadyorder(isorderready) {
	if (isorderready == 'yes') {
		is_order_ready = 'yes';
		$('final_step_orderready').show();
		$('final_step_paper').hide();
		changeStepIcon('4');
	}
	else {
		is_order_ready = 'no';
		$('final_step_orderready').hide();
		$('final_step_paper').show();
		hideStepIcon('4');
	}
}
function clickChangeEdge() {
Lightview.show({ href: 'color.php', rel: 'iframe', options: { width: 650, height: 500 }});	
}
function setCanvasEdge(edgetype,edgecolor) {
	if (edgecolor) {
	edgecolor = edgecolor.replace('#','');	
	}
		new Ajax.Request('assets/ajax.php?a=setedge&type='+edgetype+'&color='+edgecolor, { method: 'get'});
		Lightview.hide();
			
			changeStepIcon('4');
}
function updateEdgeType(edgetype) {
	if (edgetype == 'none') {
		document.getElementById('edgetype_desc').style.display='none';
		document.getElementById('edgetype_show').style.display='none';
	}
	else {
		document.getElementById('edgetype_desc').style.display='block';
		document.getElementById('edgetype_show').style.display='block';
		extrahtml = document.getElementById('edgetype_change').innerHTML;
		document.getElementById('edgetype_show').innerHTML = edgetype + extrahtml;
	}
}
function updateEdgeColor(edgecolor) {
	if (edgecolor == 'none') {
		document.getElementById('edgecolor_desc').style.display='none';
		document.getElementById('edgecolor_show').style.display='none';
	}
	else {
		document.getElementById('edgecolor_desc').style.display='block';
		document.getElementById('edgecolor_show').style.display='block';
		document.getElementById('edgecolor_show').innerHTML = "#"+edgecolor;
		document.getElementById('edgecolor_show').style.color = "#"+edgecolor;
	}
}
oldqtyvalue = '0';
function qtySetOldValue(oldvalue) {
	if (oldvalue == '- MORE -') { }
	else {
	oldqtyvalue = oldvalue;
	}
}
function qtyIncrease() {
	new Ajax.Request('assets/ajax.php?a=increase_qty', { method: 'get'});
}
function qtyChange(newqty) {
	
	if (newqty == '- MORE -') {
		qtyDropAdd('10');
		
	}
	else {
	new Ajax.Request('assets/ajax.php?a=change_qty&qty='+newqty, { method: 'get'});
	}
}
function qtyDropAdd(howmany) {
	objList = document.getElementById('newqtydrop');
	var intIdx = -1 ;
	var intMax = objList.options.length ;
	var objCount = 0;
	for( var intOpt = 0 ; intOpt < intMax ; intOpt ++ ) {
		objCount++;
	}
	if (parseInt(objCount) < 1) {
	objCount = 2;	
	}
	jQuery("#newqtydrop option").remove();
	newObjCount = (parseInt(objCount) - 1);
	newObjCount = (parseInt(newObjCount) + parseInt(howmany));
	for( var intOpt = 0 ; intOpt < newObjCount ; intOpt ++ ) {
		jQuery("#newqtydrop").append("<option value='"+intOpt+"'>"+intOpt+"</option>");	
	}
	//jQuery("#newqtydrop").append("<option value='"+intOpt+"'>"+intOpt+"</option>");
	jQuery("#newqtydrop").append("<option value='- MORE -'>- MORE -</option>");
	updateQty(oldqtyvalue);


}
function evalWidthHeight(sele) {
	selWidth = sele.value;
	if (selWidth == '- Select -' || selWidth == '' || !selWidth) {
	}
	else {
     new Ajax.Request('assets/ajax.php?a=update_width&width='+selWidth, { method: 'get'});
	 enableContinue('paper');
	}
}
function changePaper(newpaper) {
    new Ajax.Request('assets/ajax.php?a=update_paper&selection='+newpaper, { method: 'get'});
	enableContinue('order');
	
}
function changeName(newpaper) {
    new Ajax.Request('assets/ajax.php?a=change_name&selection='+newpaper, { method: 'get'});
	
}

function updatePaper(newpaper) {
	document.getElementById('ps_ppaper').innerHTML = newpaper;
}
function updateFilename(filename) {
document.getElementById('ps_filename').innerHTML = filename;	
}
function updatePrintSize(width,height) {
	document.getElementById('ps_pwidth').innerHTML = width+' inches';
	document.getElementById('ps_pheight').innerHTML = height+' inches';
	if (document.getElementById('ps_height_selection')) {
	document.getElementById('ps_height_selection').value = height;
	}
}
function updateQty(qty1) {

	//document.getElementById('ps_qty').innerHTML = qty1;
	objList = document.getElementById('newqtydrop');
	
	
	var intIdx = -1 ;
	var intMax = objList.options.length ;
	for( var intOpt = 0 ; intOpt < intMax ; intOpt ++ ) {
		if( objList.options[intOpt].value == qty1 ) {
			intIdx = intOpt ; // this is the index
			intOpt = intMax ; // escape the loop
		}
	}
	objList.selectedIndex = intIdx;

}

function updateSub(sub1) {
	if (document.getElementById('ps_sub')) {
	document.getElementById('ps_sub').innerHTML = sub1;
	}
}
function updateImg() {
	var rand = Math.random();
	document.getElementById('ps_img').src = 'assets/viewimg.php?rand='+rand;
}
function updateCropper() {

}
function resetImg() {
		document.getElementById('ps_img').src = 'images/space.png';
}
function resetProcess() {
	updateFilename('Upload an image');
	updatePrintSize('0','0');
	resetImg();
}
function startStepThrough() {

	stepthrough_which = 'step_1_browse';
	stepthrough_element = document.getElementById('step_1_browse');
	toggleBorder('on');

}

function continueStepThrough() {
	if (stepthrough_element) {
	stepthrough_element.style.border="0px";
	}
	if (stepthrough_which == 'step_1_browse') {
		new_step_name = 'continueButton_upload';
	}
	if (stepthrough_which == 'continueButton_upload') {
		new_step_name = 'sizeSelectorDiv';
		changeStepIcon('2');
		
	}
	if (stepthrough_which == 'sizeSelectorDiv') {
		new_step_name = 'continueButton_paper';
	}
	
	if (step_do_not_proceed == 'no') {
	if (stepthrough_which == 'continueButton_paper') {
		new_step_name = 'paper_select_td';
		changeStepIcon('3');
	}
	if (stepthrough_which == 'paper_select_td') {
		new_step_name = 'continueButton_order';
		
	}
	}
	stepthrough_which = new_step_name;
	stepthrough_element = document.getElementById(new_step_name);
	
}
function changeStepIcon(step) {
if (step == '1') { document.getElementById('bottomsteps_1').className = 'stepON'; }
if (step == '2') { document.getElementById('bottomsteps_2').className = 'stepON'; }
if (step == '3') { document.getElementById('bottomsteps_3').className = 'stepON'; }
if (step == '4') { document.getElementById('bottomsteps_4').className = 'stepON'; }
}
function hideStepIcon(step) {
if (step == '1') { document.getElementById('bottomsteps_1').className = 'stepOFF'; }
if (step == '2') { document.getElementById('bottomsteps_2').className = 'stepOFF'; }
if (step == '3') { document.getElementById('bottomsteps_3').className = 'stepOFF'; }
if (step == '4') { document.getElementById('bottomsteps_4').className = 'stepOFF'; }
}
last_element = 'none';
elementPulseArray = new Array();

function toggleBorder(onoff) {
	if (stepthrough_element.id != last_element && stepthrough_element.id != 'step_1_browse1') {
		if (elementPulseArray[stepthrough_element.id] != 'yes') {
			new Effect.Pulsate(stepthrough_element.id, { pulses: 2, duration: 1.0 });
			elementPulseArray[stepthrough_element.id] = 'yes';
		}
	}
	if (stepthrough_element.id == 'step_1_browse1') {
		document.getElementById('step_1_browse').style.border="2px solid #10ba1c";
		document.getElementById('step_1_browse').style.padding="3px";
	}
			//stepthrough_element.style.border="2px solid #10ba1c";
			//stepthrough_element.style.padding="3px";
			setTimeout ( "toggleBorder('off')", 150 );
			last_element = stepthrough_element.id;
}
function disableFlasher() {
	stepthrough_element.style.border="0px";
	stepthrough_showflasher = 'no';
	stepthrough_element.style.border="0px";
}

function showLoading() {
	document.getElementById('loadingdisplay').style.display='block';
}

function hideLoading() {
	document.getElementById('loadingdisplay').style.display='none';
}
function dimLights() {
new Effect.Opacity('TabbedPanels1', { from: 1.0, to: 0.3, duration: 0.1 });	
}
function showLights() {
	new Effect.Opacity('TabbedPanels1', { from: 0.3, to: 1, duration: 0.1 });
}
function doneCroppingImage(width,height) {
	Lightview.hide();
	new Ajax.Request('assets/ajax.php?a=done_cropping&new_width='+width+'&new_height='+height, { method: 'get'});
	enableContinue('paper');
}


Event.observe( window, 'load', function() { qtyDropAdd('10'); } );
Event.observe( window, 'load', function() { hideLoading(); } );
Event.observe( window, 'load', function() { startStepThrough(); } );
Ajax.Responders.register({
  onCreate: function() {
    showLoading();
	delayedAjaxStart();
  },
  onComplete: function() {
    hideLoading();
	delayedAjaxEnd();
  }
});

  var loaded = false;
  var inajaxevent = false;

  
function delayedAjaxStart() {
	loaded = false;
    window.setTimeout('delayedAjaxStartEvent()', 1000);
}
function delayedAjaxEnd() {
	loaded = true;
	if (inajaxevent) {
	new Effect.Opacity('TabbedPanels1', { from: 0.3, to: 1, duration: 0.1 });
	}
	inajaxevent = false;
}
function delayedAjaxStartEvent() {
  if (!loaded) {
	  inajaxevent = true;
    new Effect.Opacity('TabbedPanels1', { from: 1.0, to: 0.3, duration: 0.1 });
  }
  else {
  inajaxevent = false;
  }
}


