jQuery(document).ready(function($)
{
  //hide the all of the element with class msg_body
  $("#order-cd").hide();
  //toggle the componenet with class msg_body
  $("#order-cd-click").click(function()
  {
    $("#order-cd").slideToggle(600);
	return false;
  });
});

function validatecdorderform(theForm) {
	var reason = "";	
	
	// billing fields
	reason += validateEmpty(theForm.name,"Please enter your name");
    reason += validateEmpty(theForm.address1,"Please enter your address");
    reason += validateEmpty(theForm.county,"Please enter your town / city");
	reason += validateEmpty(theForm.postcode,"Please enter your postal code");
    reason += validateEmail(theForm.emailaddress,"");	
      
	//alert(reason);  
	
    if (reason != "") {
      alert(reason);
      return false;
    }
	return true;
  }

function validateEmpty(fld,str) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = '#eb80e0'; 
        error = str+"\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function trim(s){ return s.replace(/^\s+|\s+$/, '');} 

function validateEmail(fld,section) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = '#eb80e0';
        error = section + "You didn't enter an email address\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#eb80e0';
        error = section + "Please enter a valid email address\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#eb80e0';
        error = section + "The email address contains illegal characters\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePostcode(fld,str) {
    var error = "";
  
    if (!isValidPostcode(fld.value)) {
        fld.style.background = '#eb80e0'; 
        error = str+"\n"
    } else {
        fld.style.background = 'White';
		fld.value = formatPostcode(fld.value);
    }
    return error;   
}

/* tests to see if string is in correct UK style postcode: AL1 1AB, BM1 5YZ etc. */
function isValidPostcode(p) {
	//var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
	var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2}[A-Z]{0,1} ?[0-9][A-Z]{2}/i;
	return postcodeRegEx.test(p);
}
/*	formats a VALID postcode nicely: AB120XY -> AB1 0XY */
function formatPostcode(p) {
	if (isValidPostcode(p)) {
		var postcodeRegEx = /(^[A-Z]{1,2}[0-9]{1,2})([0-9][A-Z]{2}$)/i;
		//var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2}[A-Z]{0,1} ?[0-9][A-Z]{2}/i;
		return p.replace(postcodeRegEx,"$1 $2").toUpperCase();
	} else {
		return p;
	}
}
