//javascript functions for website

$(document).ready(function() {
	$.ajaxSetup ({  
		cache: false,
		async: false
	});  
});

function changeFAQ(faqId) {
	$.get("getfaq.asp?faqId=" + faqId, function(data){
		$('#faqAnswer').html(data);
	});
}

function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    }
	
    return true;
}


function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_'";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}


function quickShow(subcat) {
	$('.quickpick').slideUp();
	$('#quickpick_'+subcat).slideDown();
}

function DLShow(secid) {
	$('.dlist').slideUp();
	$('#dlist_'+secid).slideDown();
}

function changeCols(prodId,matcode) {
	$('#coldrop').load('getcols.asp?prodId='+prodId+'&matcode='+matcode);
}

function changeChart(charttype) {
	if(charttype == 'Linea') {
		$('#fitting_Standard').hide();
		$('#fitting_Linea').show();
	}else{
		$('#fitting_Linea').hide();
		$('#fitting_Standard').show();
	}
}

function chkOrthopaedic() {
	if($('#name').val() == '' || $('#email').val() == '' || $('#tel').val() == '' || $('#address1').val() == '' || $('#address2').val() == '' || $('#towncity').val() == '' || $('#postcode').val() == '') {
		alert("Please fill in all the fields before proceeding");
		return false;
	}else if(! isValidEmail($('#email').val())) {
		alert("Please enter a valid email address");
		return false;
	}else{
		return true;
	}
}

function chkContactForm() {
	if($('#name').val() == '' || $('#email').val() == '' || $('#tel').val() == '' || $('#subject').val() == '' || $('#message').val() == '' ) {
		alert("Please fill in all the fields before proceeding");
		return false;
	}else if(! isValidEmail($('#email').val())) {
		alert("Please enter a valid email address");
		return false;
	}else{
		return true;
	}
}


function chkSignup() {
	if($('#name').val() == '' || $('#email').val() == '' || $('#tel').val() == '' || $('#address1').val() == '' || $('#towncity').val() == '' || $('#postcode').val() == '') {
		alert("Please fill in all the fields before proceeding");
		return false;
	}else if(! isValidEmail($('#email').val())) {
		alert("Please enter a valid email address");
		return false;
	}else{
		return true;
	}
}

function chkRequestLogin() {
	if($('#reqname').val() == '' || $('#reqemail').val() == '' || $('#reqjobtitle').val() == '' || $('#reqdept').val() == '' || $('#reqorganisation').val() == '' || $('#reqtel').val() == '') {
		alert("Please fill in all the fields before proceeding");
		return false;
	}else if(! isValidEmail($('#reqemail').val())) {
		alert("Please enter a valid email address");
		return false;
	}else{
		return true;
	}
}

function changeDel(delivery,frompage) {
	document.location.href = "changedelivery.asp?delivery="+delivery+"&frompage="+frompage
}

function chkVAT() {
	if($('#name').val() == '' || $('#address').val() == '' || $('#email').val() == '' || $('#reason').val() == '') {
		alert("Please fill in all the fields before proceeding");
		return false;
	}else if($('#vatexempt').is(':checked') == false) {
		alert("Please tick the box to legally confirm the declaration");
		return false;
	}else{
		return true;
	}
}

function checkPayment() {
	var submitit = true;
	
	//check for empty form fields
	if ($("#firstname").val() == "" || $("#surname").val() == "" || $("#address1").val() == "" || $("#towncity").val() == "" || $("#postcode").val() == "" || $("#CardNumber").val() == "" || $("#CardHolder").val() == "" || $("#ExpiryDate").val() == "") {
		alert("Please fill in all the form fields");
		submitit = false;
	}
	
	if (submitit == true) {
		document.getElementById('submit').disabled = true;
		document.getElementById('submit').value = 'Processing...';
	}
	
	return submitit;
}

function checkCountry(thecountry) {
	if(thecountry != 'United Kingdom') {
		$('#overseasconfirm').show();
	}else{
		$('#overseasconfirm').hide();
	}
}

function showInfo(stock_code) {
	$('#info_'+stock_code).show();
}

function hideInfo(stock_code) {
	$('#info_'+stock_code).hide();
}

function chkQuant(stock_code) {
	if ($("#q_"+stock_code).val() == 0) {
		alert("Please enter a product quantity");
		return false;
	}else{
		return true;
	}
}

function chkPO() {
	if ($("#PONumber").val() == '') {
		alert("Please enter a purchase order number");
		return false;
	}else{
		return true;
	}
}

function changeImage(filename,filetype) {
	if(filetype == 'flash') {
		$('#prodImage').html('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="320" height="240" id="button" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="/images/orthopaedic/'+filename+'" /><param name="quality" value="high" /><param name="wmode" value="window" /><param name="bgcolor" value="#ffffff" /><embed src="/images/orthopaedic/'+filename+'" quality="high" bgcolor="#ffffff" width="350" height="240" name="button" align="middle" wmode="window" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>')
	}else{
		$('#prodImage').html('<img src="images/orthopaedic/'+filename+'" /><br />')
	}
}

function chkChangePassword() {
	if($('#curPassword').val() == '' || $('#newPassword').val() == '' || $('#confirmPassword').val() == '' ) {
		alert("Please fill in all the fields before proceeding");
		return false;
	}else if($('#newPassword').val() != $('#confirmPassword').val()) {
		alert("Your password and confirmed password are not the same");
		return false;
	}else{
		return true;
	}
}

function compareFull() {
	alert("You can only compare a maximum of 4 products at once. Please remove an item to be able to compare something different");
	document.location.href = '/compare.asp';
}
