// This function will create a trim function on the string class which we can
// use to ensure the customer has provided data in a relevant field
String.prototype.trim = function() {
  return this.replace(/^\s*|\s*$/g, "")
}

// This function is used to validate the contents of the account registration form
function validate_registration_form(form)
{
	$errors = '';

	if (form.email.value.trim() == '') $errors += '- Please provide your email address\n';
	if (form.name.value.trim() == '') $errors += '- Please provide your name\n';
	if (form.password.value.trim() == '') $errors += '- Please provide a password\n';
	if (form.password.value.trim() != form.confirmpassword.value.trim()) $errors += '- Your password confiration is not the same as your password\n';
	if (form.company.value.trim() == '') $errors += '- Please provide your company name\n';
	if (form.companytype.value.trim() == '') $errors += '- Please select your company type\n';
	if (form.telephone.value.trim() == '') $errors += '- Please provide your telephone number\n';

    if ($errors) alert('The following error(s) occurred:\n' + $errors);
    return $errors == '';

}

// This function handles requesting the build of the terms file 
function get_latest_terms_file(terms_file_id)
{	

	$('terms_container').addClass('hidden');
	$('progress_container').removeClass('hidden');
	if ($('message_panel')) {
		$('message_panel').addClass('hidden');
	}
		
	// Send the AJAX request
    var request = new Request.JSON( {
    	method: 'get',
    	url: '/wholesaler_terms_build.php?terms_file_id=' + terms_file_id,
    	onComplete: function( information ) {
    		if (information == true) {

    			document.location.href='/wholesaler_terms_details.php?action=rebuilt&terms_file_id=' + terms_file_id;
    		
    		}
    	}} 
    ).get();
	
	
}

function check_update_terms_file(form) {
	$errors = '';

	if ($('terms_file_description').value.trim() == '') $errors += '- Please provide a description for your terms file\n';
	if ($('terms_file_supplier').value.trim() == '') $errors += '- Please supplier of your terms file\n';
	if ($('terms_file_supplier_catalogue').value.trim() == '') $errors += '- Please supplier of your terms file\n';
	if ($('terms_file_cost_price_default').value.trim() == '') $errors += '- Default terms file from supplier\n';
	if ($('terms_file_server').value.trim() == '') $errors += '- Please provide the remote FTP server\n';
	if ($('terms_file_user').value.trim() == '') $errors += '- Please provide the FTP server name\n';
	if ($('terms_file_password').value.trim() == '') $errors += '- Please provide FTP password\n';
	if ($('terms_file_remote_file').value.trim() == '') $errors += '- Please specify the path to your terms file\n';
	if ($('terms_file_transfer_frequency').value.trim() == '') $errors += '- Please specify the automatic transfer frequency\n';
	if ($('terms_file_notify').value.trim() == '') $errors += '- Do you want to be notified of changes to your terms file\n';
	
	if ($errors) alert('The following error(s) occurred:\n' + $errors);
    return $errors == '';
	
	return false;
}

function check_create_terms_file(form) {
	$errors = '';

	if ($('terms_file_description').value.trim() == '') $errors += '- Please provide a description for your terms file\n';
	if ($('terms_file_supplier').value.trim() == '') $errors += '- Please supplier of your terms file\n';

	if ($errors) alert('The following error(s) occurred:\n' + $errors);
    return $errors == '';
	
	return false;
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}