
var input_quantity = 0;

// Deletes an address file from the system. 
function delete_addressfile(file_id){	
	if (confirm('Are you sure you wish to delete this address file?')){
		location.href='delete_address_file.php?file_id='+file_id;
		//xajax_deleteAddressFile(file_id);
	}
	
	return;
}
		
		function SubmitFormWithCondition(objform, cond, error_message){
			alert(2);
			if (cond){
				objform.submit();
			}
			else{
				alert(error_message);
			}
		}
		
		function isCreditCardExpired(){
			var month_obj = document.getElementsByName("month")[0];
			var month = month_obj.options[month_obj.selectedIndex].value;
			
			var year_obj = document.getElementsByName("year")[0];
			var year = year_obj.options[year_obj.selectedIndex].value;
					
			var today = new Date();
			expiry = new Date(year, month);
			if (today.getTime() > expiry.getTime()){				
				return false;
			}
			else{
				document.forms.billingDetails.ccExpiration.value = month + "/" + year;
				return true;
			}
	
	return;
}
		
function isIE(){
	if (parseInt(navigator.appVersion)==4)
		return 1;
	return 0;
}
		
function validateCSVAssociatedFields(){
	var dropdowns = document.getElementsByName("fieldtype[]");
	var len = dropdowns.length;
	var arr_options = Array("recipient", "company", "address_1", "city", "state", "postal", "first_name", "last_name", "address_2", "ignore");
	var arr_count = new Array();
	for (i=0; i<10; ++i){
		count = 0;
		for (j=0; j<len; ++j){
			if (arr_options[i] == dropdowns[j].value){
				count++;
			}
		}
		arr_count[i] = count;
	}
			
	var address_count = 0;
	for (i=0; i<10; ++i){
		if (arr_count[i] > 1 && arr_options[i]!="ignore"){
			alert("Fields cannot be selected twice");
			return;
		}
		if (arr_options[i]=="address_1" || arr_options[i]=="address_2"){
			address_count += arr_count[i];
		}
	}
	if (address_count > 1){
		alert("Fields cannot be selected twice");
		return;
	}
						
	if(!arr_count[0] && (!arr_count[6] || !arr_count[7])){
		alert("Recipient or pair of First name and Lastname must be selected");
		return;
	}
			
	if (arr_count[0] && (arr_count[6] || arr_count[7])){
		alert("Only select Recipient or pair of First name and Lastname");
		return;
	}
		
	if (!arr_count[3]){
		alert("City must be selected");
		return;
	}
			
	if (!arr_count[4]){
		alert("State must be selected");
		return;
	}
			
	if (!arr_count[5]){
		alert("Zip Code must be selected");
		return;
	}
			
	if (!arr_count[2] && !arr_count[8]){
		alert("Address must be selected");
		return;
	}
			
	document.import_address.submit();
			
}


function updateQuantity(i, min_quantity, address_file_quantity, address_record_count){
		
	var itemQuantity = document.getElementById("itemQuantity_" + i).value;
	if (isPositiveNumber(itemQuantity)){
		if (itemQuantity < min_quantity){					
			alert("The minimum order requirement for this order is " + min_quantity + ". Please adjust your quantity.");
		}else if (itemQuantity > address_file_quantity && address_file_quantity != 0){
			alert("Your entered quantity " + itemQuantity + " is more than the " + address_record_count + " address records available in your selected print files. Please adjust your quantity.");
		}
		else{
			document.update_quantity.item_order.value = i;
			document.update_quantity.update_flag.value = 1;
			document.update_quantity.delete_flag.value = "";
			document.update_quantity.submit();
		}
	}
	else{
		alert("Please enter a valid Quantity.");
	}
}
		
function disableCheckOutButton(partner_url){
	var e = document.getElementById("btnCheckOut");
	if (e){
		e.src=partner_url + "images/containers/submit-checkout_disabled_bg.gif";
		e.disabled="disabled";
		e.onclick = "";
	}
}
		
function enableCheckOutButton(partner_url){
	var e = document.getElementById("btnCheckOut");
	if (e){
		e.src=partner_url + "images/containers/submit-checkout_bg-trans.gif";
		e.disabled="";
		e.onclick = function (){
		location.href = "billing.php";
		};
	}
}
		

		
function deleteItemFromCart(i){
			
	var itemQuantity = document.getElementById("itemQuantity_" + i).value;
	if (confirm("Are you sure you want to delete this item?")){
		document.update_quantity.item_order.value = i;
		document.update_quantity.update_flag.value = "";
		document.update_quantity.delete_flag.value = 1;
		document.update_quantity.submit();
	}
}
		
function isValidFileType(filename, stringextensions){
	splitName = filename.split(".");
	fileType = splitName[splitName.length - 1].toLowerCase();
	if (stringextensions.indexOf(fileType) < 0){
		return 0;
	}
	return 1;
}
		
function checkBoxClicked(FrontCheckBoxId, BackCheckBoxId, onEventElementId, partner_url, next_url, add_cond){
	var objCheckBoxFront = document.getElementById(FrontCheckBoxId);
	var objCheckBoxBack = document.getElementById(BackCheckBoxId);
	var isFrontChecked = true;
	var isBackChecked = true;
	if (objCheckBoxBack){
		isBackChecked = objCheckBoxBack.checked;
	}
	if (objCheckBoxFront){
		isFrontChecked = objCheckBoxFront.checked;
	}

	var e = document.getElementById(onEventElementId);
	if (e && e.src){
		if (!isFrontChecked || !isBackChecked || !add_cond){
			e.src=partner_url+"/images/containers/submit-next-disabled_bg-trans.gif";
			//e.disabled="disabled";
			if (add_cond){
				e.onclick = function () {alert("Please make sure that you've reviewed the Front and the Back for errors.");}
			}
			else{
				e.onclick = function (){alert("Please make sure: \n 1. You\'ve reviewed the Front for errors.\n 2. You\'ve reviewed the Back for errors. \n 3. Design Name is not empty.");}	
			}
		}
		else{
			e.src=partner_url+"/images/containers/submit-next_bg-trans.gif";
			e.disabled="";
			e.onclick = function (){
				if (next_url!=""){
					location.href = next_url;
				}
				else
					document.forms[0].submit();
				}
			}
	}
}
		
function isPositiveNumber(number){
	var pattern = /^\d+$/
	if (number.match(pattern)) {
		if (parseInt(number) > 0){
			return 1;			
		}
	}
	return 0;
}
		
// This is called when the "Continue" button is pushed on product selections. 
// We may have different types of form objects for the quantity, so this function checks
// that. 
function isPositiveNumberObject(obj) { 
			
	if (!obj.value) { 
		// Means this may be a select box. 	
		// If it's from a select box, for right now, let's assume that we don't need
		// to verify input, let's always assume it's a fixed number. 
		pos = 1;
	} else {
		// Input text box, so verify the input. 
		pos = isPositiveNumber(obj.value);
		input_quantity = obj.value;
	}
			
	return pos;
}
		
/**
 *  Called from the product selections page. 
 */
function SubmitFormWithCondition(objform, cond, error_message){
	//alert("Condition: "+cond);
	if (cond){
		objform.submit();
	}
	else{
		alert(error_message);
	}
}
		
function checkQuantity(val, partner_url,order_stock){
	var btnNext = document.getElementById("btnNext");
	if (btnNext){
		if (val>0){
			btnNext.src=partner_url+"/images/containers/submit-next_bg-trans.gif";
			btnNext.disabled="";
			//xajax_estimatePrice(val,order_stock);
		}else{
			btnNext.src=partner_url+"/images/containers/submit-next-disabled_bg-trans.gif";
			//btnNext.disabled="disabled";
		}
	}
}
		
		
		function checkDesignName(obj, partner_url, next_url) {			
			var btnNext = document.getElementById("btnNext");
			if (obj.value!=""){
				var objCheckBoxFront = document.getElementById("review_1");
				var objCheckBoxBack = document.getElementById("review_2");
				var isFrontChecked = true;
				var isBackChecked = true;
				if (objCheckBoxBack){
					isBackChecked = objCheckBoxBack.checked;
				}
				if (objCheckBoxFront){
					isFrontChecked = objCheckBoxFront.checked;
				}
				if (!isFrontChecked || !isBackChecked){				
					btnNext.src=partner_url+"/images/containers/submit-next-disabled_bg-trans.gif";
					//btnNext.disabled="disabled";					
					btnNext.onclick = function (){alert("Please make sure that you've reviewed the Front and the Back for errors.");}
				}
				else{
					btnNext.src=partner_url+"/images/containers/submit-next_bg-trans.gif";
					btnNext.disabled="";					
					btnNext.onclick = function (){
						document.forms[0].submit();
					}
				}
			}
				
			else{
				btnNext.src=partner_url+"/images/containers/submit-next-disabled_bg-trans.gif";
				//btnNext.disabled="disabled";
				btnNext.onclick = function (){alert("Please make sure: \n 1. You\'ve reviewed the Front for errors.\n 2. You\'ve reviewed the Back for errors. \n 3. Design Name is not empty.");}	
			}
		}
		
		
		function checkKeyValue(obj, e, partner_url) {
			
			
			var code = 0;
			var ENTER_CODE = 13;
			var BACKSPACE_CODE = 8;
			var DEL_CODE = 46;
			var btnNext = document.getElementById("btnNext");
			if (e){
				code=e.keyCode? e.keyCode : e.charCode;
			}

			if (code == ENTER_CODE || code == BACKSPACE_CODE || code == DEL_CODE || obj.value == ""){
				if (btnNext){
					if (obj.value == ""){					
						btnNext.src=partner_url + "/images/containers/submit-next-disabled_bg-trans.gif";
						btnNext.disabled="disabled";
					}
					else{
						btnNext.src=partner_url + "/images/containers/submit-next_bg-trans.gif";
						btnNext.disabled="";
					}
				}				
			}
			else{
				if (btnNext){
					btnNext.src=partner_url + "/images/containers/submit-next_bg-trans.gif";
					btnNext.disabled="";
				}
			}
		}
		
		function submitForm(index){
			var btnNext = document.getElementById("btnNext");
			if (btnNext){
				if (!btnNext.disabled){
					document.forms[index].submit();
				}
			}
		}
		
		function adjustDiv(h){
			if (document.getElementById("forix_content"))
				document.getElementById("forix_content").style.height = h + 20 + "px";			
		}
		
		function adjustCancelNextDiv(){
			
			if (parseInt(navigator.appVersion)==4){
				if (navigator.appVersion.indexOf("MSIE 6.0") > 0){
					w1 = 891;
					w2 = 889;
				}else
					if (navigator.appVersion.indexOf("MSIE 7.0") > 0){
						w1 = 886;
						w2 = 884;
					}
				if (document.getElementById("forix_content"))
					document.getElementById("forix_content").style.width = w1 + "px";
				if (document.getElementById("next_cancel"))
					document.getElementById("next_cancel").style.width = w2 + "px";
			}

		}

		function cancel_order(){
			if (confirm('Are you sure you would like to cancel your order?  By doing so, you will lose previously entered information for this job.')){
				location.href='/cancel_order.php';
			}	
		}
		
		function check_shipping_method(){
			//vendorStoreId = document.getElementById('vendor_store_id').value;
		
			
			if (document.forms.shipping_selection.shipment_quantity.value > 0){
				
				
				if (!document.forms.shipping_selection.shipping_method.value && document.forms.shipping_selection.verify_ignore.value != 1){
					alert('Please select a shipping method.');	
				}else{
					is_pickup_shipping = document.getElementById('is_pickup_shipping').value;
					
					if (is_pickup_shipping){
						isVendorValid = false;
						if (document.getElementById('vendor_store_id') != null){
							vendorStoreId = document.getElementById('vendor_store_id').value;
							
							if (vendorStoreId != 0){
								isVendorValid = true;
							}
						}
						if (isVendorValid == true){
							location.href='billing.php';
						}
						else{
							alert('Please select a vendor store.');	
						}
					}
					else{
						location.href='billing.php';
					}
				}			
			}else{
				location.href='billing.php';
			}
				
		}
		
		function select_shipping_method(){
			document.forms.shipping_selection.submit();
		}


/**
* menuSelectQuantityList is called from product_selections.php when the stock selection is a menu. 
*/

function menuSelectQuantityList(order_type, order_size, page_count) { 
		
	stock_id = document.product_selections.order_stock.options[document.product_selections.order_stock.selectedIndex].value;
		
	xajax_get_quantity_list(order_type, order_size, page_count, stock_id);
		
	return;
}

function removeCouponFromCart() {
	var str = 'Are you sure you want to remove this Promotion?' ;
	if(confirm(str)) {
		if(typeof(document.promotion)!= "undefined") {
			if(typeof(document.promotion.update_coupon_flag) != "undefined") {
				document.promotion.update_coupon_flag.value = 1;
				
				document.promotion.submit();
			}
		}
		
		if(typeof(document.shipping_selection.update_coupon_flag)!= "undefined") {
			document.shipping_selection.update_coupon_flag.value = 1;
			
			document.shipping_selection.submit();
		}

	}
}	

/*
 *  Function: enableApprove
 *  This function is called from the design reveiw step where a user is waiting for the review thumbnails. 
 *  This function is called onLoad, and makes the approve button clickable so the user can continue in the process
 *  but only after a thumbnail has been generated. 
 *
 */
function enableDesignApprove() { 
	
	btn = document.getElementById("btnNext");
	
	btn.disabled = false;
	
	
	return;
}


/**
* Function: radioStockPriceEstimate
* 
* This funtion is called from the product selections interface when the stock options 
* are radio buttons. 
* 
* @param quantity
* @param form
* @param div
* @param mailer
* @param portal_dir
* @return
*/
function radioStockPriceEstimate(quantity, form, div, mailer, portal_dir ) { 

	var selStock = 0;
	
	for (i=0; i< form.order_stock.length; i++) { 
		if (form.order_stock[i].checked == true) { 
			selStock = form.order_stock[i].value;

			break;
		}

	}

	//alert("Quantity: "+quantity+" Selected Stock:"+selStock+", Portal: "+portal_dir);
	
	checkQuantity(quantity, portal_dir, selStock);
	
	
	xajax_estimatePrice(quantity,selStock, div, mailer);

	return;
}

/**
 * Function menuSelectStockPriceEstimate
 * 
 * Is called from the product selection pages on a quantity change from a select menu. 
 * 
 * @param quantity
 * @param stock
 * @param portal_dir
 * @param div
 * @param mailer
 * @return
 */

function menuSelectStockPriceEstimate(quantity, stock, portal_dir, div, mailer) { 
	
	checkQuantity(quantity, portal_dir, stock);
	
	
	xajax_estimatePrice(quantity, stock, div, mailer);
			
	return;

}
