
var confirmLoggedIn = function(){
	var c = new SFCAjaxUtils();
	if(!c.isLoggedIn())
		document.location.href = "/sfc3/index.cfm";
}

var reOrder = function(args){

	if('initialized' in args && args.initialized.toLowerCase() == 'true')
		confirmLoggedIn();
	
	
	var qString = '&partNumber=' + args.partNumber + '&qty=' + args.Qty;

	if('preSelectSize' in args)
		qString += '&preSelectSize=' + args.preSelectSize;
		
	if('Size' in args)
		qString += '&Size=' + args.Size;
		 	
	if('Name' in args)
		qString += '&Name=' + args.Name; 
	
	if('tracker' in args)
		qString += '&tracker=' + args.tracker;
		
	if('addPDPAccessory' in args)
		qString += '&addPDPAccessory=' + args.addPDPAccessory;
		
	
	jOpenWin('ReOrderWin',reOrderURL + qString,function(){
	
		$("#VerifiedEmployees").change(function(){
			
			var Emp = this.value.split(":");
			
			$("#PREmployeeName").val(Emp[0]);
			$("#PREmployeeId").val(Emp[1]);
		});
		
		$("#btnReorder").click(function(){
			this.disabled=true;
			
			if('initialized' in args && args.initialized.toLowerCase() == 'true')
				confirmLoggedIn();
			
			var ajxOrder = new SFCAjaxUtils();
			ajxOrder.setForm('AOrder');
			
			ajxOrder.setCallbackHandler(function(res){
				if(res.SUCCESS==false){
					if(res.MSG && res.MSG.length)
						alert(res.MSG);
					else
						res.MSG ='';
							
					if(res.IDERR && res.IDERR ==1)
						res.MSG += '\n' + EmployeeIdErr;
					
					if(res.QTYERR && res.QTYERR ==1)
						res.MSG += '\n' + qtyErr;
					 
					if(res.EMPLOYEENAMEERR && res.EMPLOYEENAMEERR ==1)
						res.MSG += '\n' + EmployeeNameErr;
					
					if(res.SIZEERR && res.SIZEERR ==1)
						res.MSG += '\n' + SizeErr;
						
					if(res.PRESELECTSIZEERR && res.PRESELECTSIZEERR ==1)
						res.MSG += '\n' + PreSelectSizeErr;

					alert(res.MSG); 
					$("#btnReorder").removeAttr("disabled");
				}
				else
					AddToCart();
			});
			ajxOrder.validateAddToCart();
			
		});
	},{"left":"30%","top":"50%","width":"500px"},200);
	
	
}

var jOpenWin = function (id,url,callBack,css,pixelsFromTop,showSpeed,hideSpeed){
	
	 if(!$("#" + id).length){
	 	$("<div id='" + id + "' class='jOpenWin'><div id='" + id + "Content' class='jOpenWinContent'></div</div>").appendTo(document.body);
	 }
	
 	$("#" + id + "Content").load(url,function(){
		 
 		$("#" + id).css(css);

		if(pixelsFromTop){
			var scrollTop = $(window).scrollTop();
			var startPos = scrollTop + pixelsFromTop;
			
			$("#" + id).css("top",startPos);
		}

		$("#" + id).show();//slideDown((showSpeed) ? showSpeed : '');
				 
		callBack();

 		$("#" + id + "Close").click(function(){
 			$("#" + id).hide();//slideUp((hideSpeed) ? hideSpeed : '');
 		});	
		
 	});
 
}

var AddToCart = function(){
	var ajxOrder = new SFCAjaxUtils();
	ajxOrder.setForm('AOrder');
	ajxOrder.setCallbackHandler(function(id){
		$("#ReOrderWin").hide();
		confirmItemAdded(id);
	});	
	
	ajxOrder.addToCart();
}

var confirmItemAdded = function(id){
	
	jOpenWin('confirmAdded',confirmItemAddedURL + '&id=' + id,function(){
		
		updateItemCount();
		setTimeout(function(){
			$("#confirmAdded").hide();
		},1000*10);
		
	},{"left":"75%","width":"200px","height":"200px"},20);

}

var updateItemCount = function(){
	var ajxOrder = new SFCAjaxUtils();

	ajxOrder.setCallbackHandler(function(count){
		$("#ItemCount").html(count.ITEMCOUNT);
		$("#BasketTotal").html(count.CARTTOTAL);
	});
	
	ajxOrder.getCartItemCount();	

}

var RemoveBadChars = function(input){
	input.value =  validChars(input.value);
} 

var validChars = function (theString){
	
	var validChars = '0123456789';
	var newString ='';
	for (i=0; i<theString.length; i++){		
		if(validChars.indexOf(theString.charAt(i)) >= 0)
			newString +=theString.charAt(i);
	}
	return newString;
}


/*
 *
 *
	var CheesecakLogic = function(Type,eId){
		
		if(Type == CurrentAccountType)
			return true;
		else if(NumberOfItemsInCart > 0)
			alert('Please complete the ordering for all employees of this type, after you checkout you may return to order for this employee');
		else
			document.location.href = AccountSwitchURL + '&eId=' + eId;
			
		return false;
	}
	
	
	var PopulateEmployeeBoxes = function(Row,IdBox,NameBox){
			
		//back to unselected
		if(Row.value.length == 0){
			IdBox.value = '';
			NameBox.value = '';
	
			IdBox.readOnly = true;
			NameBox.readOnly = true;
		}
		
		//manual entry
		else if(Row.value == 0){
	
			//cheesecake factory specific code accts: 8531, 30349	
			if(CurrentAccountType)
				var continueOn = CheesecakLogic((confirm('Is this a new hire / trainee that is eligible for Payroll deduction? press "ok" if yes')) ? 'PDP' : 'DBA',Row.value);		
			else
				var continueOn = true;
				
			IdBox.value = '';
			NameBox.value = '';
	
			if(continueOn){
				IdBox.readOnly= false;
				NameBox.readOnly = false;		
			}
			else
				Row.value ='';
			
		}
		//employee selected
		else{
	
			//cheesecake factory specific code accts: 8531, 30349	
			if(CurrentAccountType)
				var continueOn = CheesecakLogic(Employees[Row.value][3],Row.value);		
			else
				var continueOn = true;
	
			IdBox.readOnly = true;
			NameBox.readOnly = true;
	
	
			if(continueOn){
				IdBox.value = Employees[Row.value][0];
				NameBox.value = Employees[Row.value][1] + ' ' + Employees[Row.value][2];
			}
			else{
				Row.value='';		
				IdBox.value ='';
				NameBox.value = '';
			
			}
			
	
		}
	}
 *
 *
 *
 */

