
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('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(":");
			
			$("#EmployeeName").val(Emp[0]);
			$("#EmployeeId").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;

					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 PopulateEmployeeBoxes = function(Row,IdBox,NameBox){

	if(Row.value.length == 0){
		IdBox.value = '';
		NameBox.value = '';

		IdBox.readOnly = true;
		NameBox.readOnly = true;
	}
	
	else if(Row.value == 0){
		IdBox.value = '';
		NameBox.value = '';
		
		IdBox.readOnly= false;
		NameBox.readOnly = false;		

	}
	else{
		IdBox.readOnly = true;
		NameBox.readOnly = true;

		IdBox.value = Employees[Row.value][0];
		NameBox.value = Employees[Row.value][1] + ' ' + Employees[Row.value][2];
	}
}

