var xmlhttp;

// check username is not already in use
function checkUserName() {

xmlhttp=null;
if (window.XMLHttpRequest)
  {// code for Firefox, Opera, IE7, etc.
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null)
  xmlhttp.onreadystatechange=check_user_list;
  xmlhttp.open("GET",'acheckuserlist.php',true);
  xmlhttp.send('');
}

function check_user_list()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"function checkUserName() {
	var nameList = xmlhttp.responseText
	var myName   = document.register_form._username.value;
    var pattern = eval("/"  + myName +  "/") ;
	var result   = pattern.test(nameList);
	if (result == true) {
		alert ("Sorry, the screen name " + myName + " has already been chosen.\n\nPlease choose another name.");
	}
  }
  }
}

function pop_sell_instr_box() {
// takes selected option from _search_instr_cat select and populates
//    _search_instr_type using data from hidden input _search_typelist
	var tmpdata;
 	var instrLine;
	var searchdata = document._placead_form._sell_typelist.value;
	var selIdx = document._placead_form._sell_instr_cat.selectedIndex;
	var selVal =document._placead_form._sell_instr_cat.options[selIdx].value;
	var aSrchdata = new Array();
	var atmpdata = new Array();
	aSrchdata = searchdata.split('##');
	document._placead_form._sell_instr_type.length=0;
	var j=1;
	var instrLine = new Option("All instruments","0");
	document._placead_form._sell_instr_type.options[0]=instrLine;
	for (i=0; i < aSrchdata.length; i++) {
 		atmpdata = aSrchdata[i].split(',');
 		if (atmpdata[0] == selVal) {
	 		var instrLine = new Option(atmpdata[2],atmpdata[1]);
			document._placead_form._sell_instr_type.options[j]=instrLine;
			j++;
		}
	}
}

function checkpayment(orig_amt) {
	var new_amt = document._placead_form._instr_price.value;
	if (new_amt*1 > orig_amt*1) {

	alert ("Selling price cannot be increased once the item has been advertised!\n\nIf you have accidentally entered a low price and need to adjust it,\nplease contact TAF Music to have the price adjusted.");
	document._placead_form._instr_price.value = orig_amt;
	}
}
function checkpostagecost(orig_amt) {
	var new_amt = document._placead_form._instr_postage.value;
	if (new_amt*1 > orig_amt*1) {

	alert ("Postage price cannot be increased once the item has been advertised!\nIf you have entered a low postage amount by accident, please contact TAF Music to have the postage adjusted.");
	document._placead_form._instr_postage.value = orig_amt;
	}
}

function allowpostagecost(){
// alert("document.getElementById('item_postage').disabled=false");
	document.getElementById('item_postage').disabled= false ;
}

function removepostagecost(){
	document.getElementById('item_postage').value = 0;
	document.getElementById('item_postage').disabled="true";
}

// ** REGISTRATION FUNCTIONS

// check username is not already in use
function checkUserName_xxold_NOT_AJAX() {
	var nameList = document.register_form.usernameslist.value;
	var myName   = document.register_form._username.value;
    var pattern = eval("/"  + myName +  "/") ;
	var result   = pattern.test(nameList);
	if (result == true) {
		alert ("Sorry, the screen name " + myName + " has already been chosen.\n\nPlease choose another name.");
	}
}




function checkprice(){
	var instr_price = document._placead_form._instr_price.value;
	var post_price  = document._placead_form._instr_postage.value;

	var sale_total = (instr_price * 1) + (post_price * 1);

	var post_amt;
	var disp_amt;

	if (sale_total < 100) {
		post_amt = 0.50;
	}
	if (sale_total > 100 && sale_total < 500.01){
		post_amt = (sale_total * 1) * .005;
	}
	if (sale_total > 500 && sale_total < 3500.01){
		post_amt = ((sale_total-500 * 1)* .0025) + 2.50;
	}
	if (sale_total > 3500) {
		post_amt = 10.00;
	}
	eval('document.getElementById("post_price").innerHTML="' + number_format_post(post_amt) + '"');

}

function number_format_post(mynumber) {
	var newno = ((mynumber * 1)) + .001;

	var tempno = newno.toString();
	var aNum = tempno.split(".");
	return (aNum[0] + '.' +  aNum[1].substr(0,2));
}

// check the user has correctly populated the fields before they submit the form
function check_adform(formtype){
//alert ("Here..." + formtype);
	var err = new Array();
	var err_msg;
	var error_msg;
	var error_message;
	var j=0;
	if (formtype == "instr") {
		var instrument = document.getElementById('_instrument').value;
		var detail = document.getElementById('_instrument').value;
		var sel_instr = document.getElementById('_sell_instr_type').selectedIndex;
		var del_type = document.getElementById('_delivery_pick_only').checked;
		var postage = document.getElementById('item_postage').value;
		var amount = document.getElementById('_instr_price').value;
		if (instrument.length < 3) {
			err[j] = 'Enter the Instrument name';
			j++;
		}
		if (detail < 3) {
			err[j] = 'Enter description of the instrument';
			j++;
		}
		if (sel_instr == 0) {
			err[j] = 'Choose an instrument category';
			j++;
		}
		if (del_type == false  && postage < 0.3) {
			err[j] = 'Enter postage cost';
			j++;
		}
	}

	if (formtype == "print") {
		var title = document.getElementById('_title').value;
		var del_type = document.getElementById('_delivery_pick_only').checked;
		var postage = document.getElementById('item_postage').value;
		var amount = document.getElementById('_price').value;


//		alert("now here " + title + ' .. ' + del_type + ' .^. ' + postage);
		if (title.length < 3) {
			err[j] = 'Please enter the title of the book or piece';
			j++;
		}
		if (del_type == false && postage < 0.3) {
			err[j] = 'Enter postage cost';
			j++;
		}
	}
// alert (err[0] + err[1]);
 	error_msg='';

 	for (i=0; i < err.length; i++) {
 		error_msg += err[i] + "  \n";
 	}
	error_message = error_msg.substr(0,(error_msg.length)-2);

	if (error_message.length > 0) {
		var error_msg = "Please check the following:\n" + error_message;
 		alert (error_msg + "\n\n" );
	} else {
		if (amount==0) {
		   if(confirm("You have not entered a price for this item.\nAre you planning to give it away free of charge?\n(Please note there is a minimum charge of 50 pence (unless you are using a free ad code) to place a detailed ad on this site.")) {
			  document._placead_form.submit();
			}
		} else {
			document._placead_form.submit();
		}
	}

	}

// check the user has correctly populated the fields before they submit the form
function check_adform_old(formtype){
	var err = new Array();
	var j=0;
	if (formtype=="instr") {
		var instrument = document._placead_form._instrument.value;
		var detail = document._placead_form_instr_desc._instrument.value;
		var sel_instr = document._placead_form._sell_instr_type.selectedIndex;
		var del_type = document._placead_form._delivery;
		var postage = document._placead_form._instr_postage;
		if (instrument.length < 3) {
			err[j] = 'Enter the Instrument name';
			j++;
		}
		if (instrument.detail < 3) {
			err[j] = 'Enter description of the instrument';
			j++;
		}
		if (sel_instr == 0) {
			err[j] = 'Choose an instrument category';
			j++;
		}
		if (del_type != "pick_only" && postage < 1) {
			err[j] = 'Enter postage cost';
			j++;
		}
	}
	if (formtype=="print") {
		var title = document._placead_form._title.value;
		var del_type = document._placead_form._delivery;
		var postage = document._placead_form._instr_postage;

		if (title.length < 3) {
			err[j] = 'Please enter the title of the book or piece';
			j++;
		}
		if (del_type != "pick_only" && postage < 1) {
			err[j] = 'Enter postage cost';
			j++;
		}
	}

 	for (i=0; i < err.length; i++) {
 		error_msg += err[i] + "\n";
 	}
	error_message = error_msg.substr(0,(error_msg.length)-2);
	if (error_message.length > 0) {
		error_msg = "Please check the following:\n" + error_message;
 		alert (error_msg);
	} else {
		document._placead_form.submit();
	}

}  // end function

// free ad scripts

	function showFreeAdDate(){
		var ad_type = document.getElementById('add_type').value; // selectedIndex;
		if (ad_type==3) {
			alert("What's On");
		} else {
			alert("Not what's On");
		}

	}

	function showSampleAdText() {
		window.open('adsampletext.html','sampletextbox','height=450,width=500,location=false,menubar-false,status=false');
	}