function dowLinkUp(){
 	$('div#deal-of-the-week-outer div#deal-of-the-week').click(function(){
 		var link = $(this).find('a').attr('href');
 		window.location = link;
 	});
}

$(document).ready(function(){
 
 	dowLinkUp();
 });


function show_hide_products(ele) {
	
	var prods = document.getElementById("products");
	
	if(ele.value=="product") {
		
		prods.style.display = "block";
	}
	else {
		
		prods.style.display = "none";
		document.forms[0].pid.value = "";
		document.forms[0].autocomplete.value = "";
	}
}

function post_request(url, pid, msg) {
	
	if (confirm(msg)) {
		
		document.getElementById("delform").innerHTML = "<form action=\"" + url + "\" method=\"post\" id=\"df\"><input type=\"hidden\" name=\"product\" value=\"" + pid  + "\" /></form>";
		
		document.getElementById("df").submit();
	}
}

function check_allowed_action() {
	
	var naf = false;
	var no_avg = false;
	var not_allowed = false;
	var no_avg_na = false;
	
	for(i=0; i < document.forms[0].length; i++) {
   
   		if(document.forms[0].elements[i].type == "checkbox" && 
		   document.forms[0].elements[i].checked) {
			
			nme = 'allowed_' + document.forms[0].elements[i].value;
			nme_avg = 'avg_' + document.forms[0].elements[i].value;
			
			if(document.forms[0].elements[eval("'" + nme + "'")].value==0) {
				
				naf = true;
			}
			
			if(document.forms[0].elements[eval("'" + nme_avg + "'")].value==0) {
				
				no_avg = true;
			}
		}		
    }
    
    var field = document.getElementById('order_action');
	
	if(field.value=="PDF_NEW" || field.value=="PDF") {
		
		if(naf==true) {
			not_allowed = true;
		}
	}
	else if(field.value=="EMAIL") {
		
		if(naf==true) {
			not_allowed = true;
		}
		else if(no_avg==true) {
			no_avg_na = true;
		}
	}
	
	if(not_allowed==true) {
		
		alert ("Sorry the order action requires all the items you have picked to be already PAID and APPROVED");
		return false;
	}
	else if(no_avg_na==true) {
		
		alert ("Sorry the order action requires stock to have been previously entered for the products you have chosen");
		return false;
	}
	else {
		
		var f = document.getElementById('order_action');	
	
		if(f.value=="CANC_BESPOKE" || 
		   f.value=="CANC_AUTH" ||  
		   f.value=="CANC_REQUEST" ||  
		   f.value=="CANC_ADDR") {
			
			if (confirm("Are you sure you want to cancel these items?")) {
				
				return true;
			}
			else {
				
				return false;
			}
		}
		else {
			
			return true;	
		}
	}
}


function populate_notes_field(v) {
		
	//checks whether needs to poulate notes field in admin order viewer page on option seletion from drop down
	var action = v.value;
	var field = document.getElementById('items_note_content');
	
	if(action=="CANC_AUTH") {
		field.value = "Upon attempting to authorise your card, the transaction was declined by your card issuer. This could be for a number of reasons including incorrect card details, insufficient funds or random fraud checks by the bank. Should you wish to place the order again please do so via our website.";
	}
	else if(action=="CANC_REQUEST") {
		field.value = "As per your instructions, we have cancelled your order and your card will not be charged.";	
	}
	else if(action=="CANC_ADDR") {
		field.value = "Upon authorising your card, the transaction was successful but the billing address and postcode did not match those registered with your card issuer. For security purposes we can not fulfil orders that fall into this criteria. If you wish to place the order again, please visit our site and ensure that the correct billing address is entered.";	
	}
	else {
		
		field.value = "";	
	}
}

function confirmAction(msg) {
	
	if(confirm(msg)) {
					
		return true;
	}
	else {
					
		return false;
	}	
}

function jumpmenu(jm, url) {
				
	if(jm.options[jm.selectedIndex].value != '' ) {
					
		var this_url = url + jm.options[jm.selectedIndex].value;
					
		location.href = this_url;
	}
}

function popImage(url,width,height) {
	
	window.open(url, 'zoomed', 'width=' + width + ',height=' + height);
}

function popWin(url,width,height,title) {
	
	window.open(url, title, 'width=' + width + ',height=' + height + ',scrollbars=Yes');
}

function changeShipping(shp) {
	
	document.getElementById("ship-form").submit();
}

function calcCharge() {
	
	var running_total = 0;
	var checked = false;
	
	for(i=0; i < document.forms[0].length; i++) {
   
   		if(document.forms[0].elements[i].type == "checkbox" && 
		   document.forms[0].elements[i].checked && 
		   !document.forms[0].elements[i].disabled) {
			
			nme = 'val' + document.forms[0].elements[i].value;
			
			running_total += parseFloatATotal(document.forms[0].elements[eval("'" + nme + "'")].value);
			
			checked = true;			
		}		
    }
   
    if(checked==true) {
    
   		running_total += parseFloatATotal(document.forms[0].ship_amount.value);
		running_total -= parseFloatATotal(document.forms[0].credit_amount.value);
    }
    
    var obj = document.getElementById('charge_total');
    obj.innerHTML = totalFormat(running_total);
}


function calcRefund() {
	
	var running_total = 0;
	
	for(i=0; i < document.forms[0].length; i++) {
   
   		if(document.forms[0].elements[i].type == "checkbox" && 
		   document.forms[0].elements[i].checked && 
		   !document.forms[0].elements[i].disabled && 
		   document.forms[0].elements[i].name !== "diff") {
			
			nme = 'val' + document.forms[0].elements[i].value;
			
			running_total += parseFloatATotal(document.forms[0].elements[eval("'" + nme + "'")].value);
		}		
    }
    
    if(document.forms[0].elements['diff'].checked) {
    
    	running_total += parseFloatATotal(document.forms[0].elements['diff'].value);
    }
    
    var obj = document.getElementById('refund_total');
    obj.innerHTML = totalFormat(running_total);
}


function parseFloatATotal(totalToFloat) {

	var returnVal = parseFloat(totalToFloat);
	
	if(isNaN(returnVal)) { 
		
		return 0.00; 
	}
	else {
	
		return returnVal;
	}
}

function totalFormat(amount) {	
	
	var num = parseFloat(amount);
	if(isNaN(num)) { num = 0.00; }
	num *= 100;
	num = Math.round(num);
	num /= 100;
	s = new String(num);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	return s;
}

$(document).ready(function(){
	$('td.options label').click(function(){
		$(this).prev('input').attr('checked','checked');
		changeShipping();
		return false;
	});
});