///////////////////////////////////////////////////////////////////////////////////////
// admin functions
function UpdateTracking(val) {
	for(i=0; i<val; i++){
		document.getElementById('tracking_value['+i+']').value=document.getElementById('tracking['+i+']').value;
		document.getElementById('item_number_value['+i+']').value=document.getElementById('item_number['+i+']').value;
		document.getElementById('address_number_value['+i+']').value=document.getElementById('address_number['+i+']').value;
	}

}

function ValidateSearchAff() {
	if (!document.getElementById('aff_search').value) {
		alert('Please enter the affiliate id to search.');
		document.getElementById('aff_search').focus();
		return false;
	}
}

function ApproveAff(val) {
	var conf=confirm('Please confirm that you wish to approve this application');
	if(conf){
		window.location="includes/transact.php?appaff="+val;
		return true;
	}else{
		return false;
	}
}

function DeclineAff(val) {
	var conf=confirm('Please confirm that you wish to decline this application!');
	if(conf){
		window.location="includes/transact.php?decaff="+val;
		return true;
	}else{
		return false;
	}
}

function ShowEmailField(val) {
	if(val=='1'){
		document.getElementById('email_field').style.visibility='visible';
	}else if(val=='0'){
		document.getElementById('recp_email').value='';
		document.getElementById('email_field').style.visibility='hidden';
	}
}

function DeleteTests() {
	var confirm_cancel=confirm('Are you sure you wish to delete this testamonial?\nThis action cannot be undone and will remove the testamonial from the database.');
	if(confirm_cancel==1){
		return true;
	}else{
		return false;
	}
}

function ValidateTestEdits() {
	if (!document.getElementById('test_name').value) {
		alert('Please enter a name for this testamonial.');
		document.getElementById('test_name').focus();
		return false;
	}
	if (!document.getElementById('test_location').value) {
		alert('Please enter the location for this testamonial.');
		document.getElementById('test_location').focus();
		return false;
	}
	if (!document.getElementById('test_text').value) {
		alert('Please enter the text for this testamonial.');
		document.getElementById('test_text').focus();
		return false;
	}
}

function ValidateSearchOrder() {
	if (!document.getElementById('order_search').value && !document.getElementById('email_search').value) {
		alert('Please enter the order number or email address you would like to search.');
		return false;
	}
}

function AdminShowPersonals(val) {
	if(document.getElementById('personals_'+val).style.display=='none'){
			var x=document.getElementsByTagName("div").length;
			for(var y=0; y<x; y++){
				if(document.getElementsByTagName("div")[y].className=='hide_me'){
					document.getElementsByTagName("div")[y].style.display='none';
				}
				if(document.getElementsByTagName("div")[y].className=='view_me'){
					document.getElementsByTagName("div")[y].innerHTML='View Personalisations'
				}
			}
		document.getElementById('peronal_text_'+val).innerHTML='Hide Personalisations'
		$(document).ready(function() {
			$("#personals_"+val).animate({ height: 'show', opacity: 'show' }, 'slow');
			return true;
		});
	}else{
		document.getElementById('peronal_text_'+val).innerHTML='View Personalisations'
		$(document).ready(function() {
			$("#personals_"+val).animate({ height: 'hide', opacity: 'hide' }, 'slow');
			return true;
		});
	}
}

function ValidateConfig() {
	if (!document.getElementById('admin_email').value) {
		alert('Please enter the admin email address.');
		document.getElementById('admin_email').focus();
		return false;
	}
	if (!document.getElementById('printer_email').value) {
		alert('Please enter the printer email address.');
		document.getElementById('printer_email').focus();
		return false;
	}
	if (!document.getElementById('reminder_days').value) {
		alert('Please enter the amount of reminder days.');
		document.getElementById('reminder_days').focus();
		return false;
	}
	if (!document.getElementById('remove_order_days').value) {
		alert('Please enter the amount of removal days.');
		document.getElementById('remove_order_days').focus();
		return false;
	}
}

function ValidatePrices() {
	if (!document.getElementById('GB_1').value) {
		alert('Please enter the price for this novel.');
		document.getElementById('GB_1').focus();
		return false;
	}
	if (!document.getElementById('ZA_1').value) {
		alert('Please enter the price for this novel.');
		document.getElementById('ZA_1').focus();
		return false;
	}
	if (!document.getElementById('GB_2').value) {
		alert('Please enter the price for this novel.');
		document.getElementById('GB_2').focus();
		return false;
	}
	if (!document.getElementById('ZA_2').value) {
		alert('Please enter the price for this novel.');
		document.getElementById('ZA_2').focus();
		return false;
	}
}

function ValidatePostage() {
	if (!document.getElementById('GB_1p').value) {
		alert('Please enter the price for this postage.');
		document.getElementById('GB_1p').focus();
		return false;
	}
	if (!document.getElementById('ZA_1p').value) {
		alert('Please enter the price for this postage.');
		document.getElementById('ZA_1p').focus();
		return false;
	}
	if (!document.getElementById('GB_2p').value) {
		alert('Please enter the price for this postage.');
		document.getElementById('GB_2p').focus();
		return false;
	}
	if (!document.getElementById('ZA_2p').value) {
		alert('Please enter the price for this postage.');
		document.getElementById('ZA_2p').focus();
		return false;
	}
	if (!document.getElementById('GB_3p').value) {
		alert('Please enter the price for this postage.');
		document.getElementById('GB_3p').focus();
		return false;
	}
	if (!document.getElementById('ZA_3p').value) {
		alert('Please enter the price for this postage.');
		document.getElementById('ZA_3p').focus();
		return false;
	}
}

function ButtonBG(id,bg){
	document.getElementById(id).style.backgroundColor=bg;
}


function validateVoucherSubmit() {
	if (!document.getElementById('cert_number').value || !document.getElementById('cert_pass').value) {
		alert('Please enter both the voucher number and validation code to proceed.');
		return false;
	}
}
function validateVoucherPromo() {
	if (!document.getElementById('recp_name').value) {
		alert('Please enter the recipient\'s full name.');
		document.getElementById('recp_name').focus();
		return false;
	}
	if (!document.getElementById('send_name').value) {
		alert('Please enter the sender\'s full name.');
		document.getElementById('send_name').focus();
		return false;
	}
	if (!document.getElementById('add_photo_1').checked && !document.getElementById('add_photo_2').checked) {
		alert('Please make a choice for the "Add a Photo" option.');
		return false;
	}
	if (!document.getElementById('send_type_0').checked && !document.getElementById('send_type_1').checked) {
		alert('Please make a choice to print or send the voucher via email.');
		return false;
	}
	if (document.getElementById('send_type_1').checked && !document.getElementById('recp_email').value) {
		alert('Please enter the email address for the recipient of the voucher.');
		return false;
	}
	if (document.getElementById('send_type_1').checked && !document.getElementById('recp_email').value.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/)) {
		alert("The email address entered does not appear to be valid.\nPlease re-enter the email address.");
		document.getElementById('recp_email').focus();
		return false;
	}
}

function AddAffComments() {
	var message=document.getElementById('message').value;
	if(!message){
		alert('Please enter the comments you would like to add for this affiliate.');
		return false;
	}
	parent.document.getElementById('msg').value=message;
	parent.document.form2.submit();
}

///////////////////////////////////////////////////////////////////////////////////////
function ValidateAffiliate() {
	if (!document.getElementById('aff_company').value) {
		alert('Please enter your company name.');
		document.getElementById('aff_company').focus();
		return false;
	}
	if (!document.getElementById('aff_fname').value) {
		alert('Please enter a contact name.');
		document.getElementById('aff_fname').focus();
		return false;
	}
	if (!document.getElementById('aff_sname').value) {
		alert('Please enter a contact surname.');
		document.getElementById('aff_sname').focus();
		return false;
	}
	if (!document.getElementById('aff_url').value) {
		alert('Please enter the URL (website address) of your website.');
		document.getElementById('aff_url').focus();
		return false;
	}
	if (!document.getElementById('aff_email').value) {
		alert('Please enter your email address.');
		document.getElementById('aff_email').focus();
		return false;
	}
	if (!document.getElementById('aff_email').value.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/)) {
		alert("The email address entered does not appear to be valid.\nPlease re-enter the email address.");
		document.getElementById('aff_email').focus();
		return false;
	}
	if (!document.getElementById('aff_office').value) {
		alert('Please enter your company telephone number.');
		document.getElementById('aff_office').focus();
		return false;
	}
	if (!document.getElementById('tac').checked) {
		alert('Please accept the terms and conditions.');
		return false;
	}
}

function StripText() {
	if(document.getElementById('lt').value=='Select a link to generate refer code here'){
		document.getElementById('lt').value='';
	}
	
}

function SetURL(val,id) {
	document.getElementById('lt').value='<a href="http://www.personalisednovels.com/index.php?af='+id+'">'+val+'</a>';
	
}

function SetBG() {
	document.getElementById('lrg_png').style.backgroundColor='#069';
	document.getElementById('med_png').style.backgroundColor='#069';
	document.getElementById('sm_png').style.backgroundColor='#069';
}

function SetBG_2() {
	document.getElementById('lrg_png').style.backgroundColor='#FFF';
	document.getElementById('med_png').style.backgroundColor='#FFF';
	document.getElementById('sm_png').style.backgroundColor='#FFF';
}

function tester() {
	var email=document.getElementById('email_address').value;
	var message=document.getElementById('message').value;
	if(!email){
		alert('Please enter the email address that you would like to send this Personalised Preview to.');
		return false;
	}
	if (!document.getElementById('email_address').value.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/)) {
		alert("The email address entered does not appear to be valid.\nPlease re-enter the email address.");
		document.getElementById('email_address').focus();
		return false;
	}
	parent.document.getElementById('email').value=email;
	parent.document.getElementById('msg').value=message;
	parent.document.form_preview.submit();
}

function ValidateContactForm() {
	if (!document.getElementById('name').value) {
		alert('Please enter your name.');
		document.getElementById('name').focus();
		return false;
	}
	if (!document.getElementById('email').value) {
		alert('Please enter your email address.');
		document.getElementById('email').focus();
		return false;
	}
	if (!document.getElementById('email').value.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/)) {
		alert("The email address entered does not appear to be valid.\nPlease re-enter the email address.");
		document.getElementById('email').focus();
		return false;
	}
	if (!document.getElementById('subject').value) {
		alert('Please select a subject for this message.');
		document.getElementById('subject').focus();
		return false;
	}
	if (!document.getElementById('message').value) {
		alert('Please enter your message.');
		document.getElementById('message').focus();
		return false;
	}
}

function CancelCustomTitle() {
	var confirm_cancel=confirm('Are you sure you wish to cancel the Customised Book Cover?\nThis will also remove any Custom Title that has been added');
	if(confirm_cancel==1){
		return true;
	}else{
		return false;
	}
}

function customTitle() {
	if(document.getElementById('cust_title').value){
		if(!document.getElementById('format_with').checked){
			var conf=confirm('A Customised Title is only available with the photo option.\nWould you like to add this option?');
			if(conf){
				document.getElementById('format_with').checked='checked';
				document.getElementById('format_with').focus();
			}else{
				document.getElementById('cust_title').value='';
			}
		}
	}
}

function setCustomTitle() {
	document.getElementById('cust_title').value='';
}

function validateVoucherSubmit() {
	if (!document.getElementById('cert_number').value || !document.getElementById('cert_pass').value) {
		alert('Please enter both the voucher number and validation code to proceed.');
		return false;
	}
}
function validateVoucher() {
	var not_checked=true;
	var p=document.form_1.postage;
	for(var a=0; a<p.length; a++){
		if(p[a].checked){
			not_checked=false;
		}
	}
	if (!document.getElementById('recp_name').value) {
		alert('Please enter the recipient\'s full name.');
		document.getElementById('recp_name').focus();
		return false;
	}
	if (!document.getElementById('send_name').value) {
		alert('Please enter the sender\'s full name.');
		document.getElementById('send_name').focus();
		return false;
	}
	if (!document.getElementById('add_photo_1').checked && !document.getElementById('add_photo_2').checked) {
		alert('Please make a choice for the "Add a Photo" option.');
		return false;
	}
	if (!document.getElementById('send_type_0').checked && !document.getElementById('send_type_1').checked) {
		alert('Please make a choice to print or send the voucher via email.');
		return false;
	}
	if (document.getElementById('send_type_1').checked && !document.getElementById('recp_email').value) {
		alert('Please enter the email address for the recipient of the voucher.');
		return false;
	}
	if (document.getElementById('send_type_1').checked && !document.getElementById('recp_email').value.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/)) {
		alert("The email address entered does not appear to be valid.\nPlease re-enter the email address.");
		document.getElementById('recp_email').focus();
		return false;
	}
	if (!document.getElementById('country').value) {
		alert('Please select the coutry that the novel will be sent to.');
		document.getElementById('country').focus();
		return false;
	}
	if (not_checked) {
		alert('Please select the postal service that you would like the recipient to have.');
		return false;
	}
	if (!document.getElementById('email').value) {
		alert('Please enter the sender\'s email address.');
		document.getElementById('email').focus();
		return false;
	}
	if (!document.getElementById('email').value.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/)) {
		alert("The email address entered does not appear to be valid.\nPlease re-enter the email address.");
		document.getElementById('email').focus();
		return false;
	}
}

function validateAnnounce(val) {
	if (!document.getElementById('recp_name').value) {
		alert('Please enter the recipient\'s full name.');
		document.getElementById('recp_name').focus();
		return false;
	}
	if (!document.getElementById('send_name').value) {
		alert('Please enter the sender\'s full name.');
		document.getElementById('send_name').focus();
		return false;
	}
	if (!document.getElementById('novel_title').value) {
		alert('Please select a novel title.');
		document.getElementById('novel_title').focus();
		return false;
	}
	
	if(val=='1'){
		if(!document.getElementById('email').value) {
			alert('Please enter the email address you would like to send this announcement to.');
			document.getElementById('email').focus();
			return false;
		}else if(!document.getElementById('email').value.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/)) {
			alert("The email address entered does not appear to be valid.\nPlease re-enter the email address.");
			document.getElementById('email').focus();
			return false;
		}else{
			document.form_1.target='_self';
			document.form_1.action='announcement.php'
		}
	}

}

function validateVoucherPreview() {
	var not_checked=true;
	var p=document.form_1.postage;
	for(var a=0; a<p.length; a++){
		if(p[a].checked){
			not_checked=false;
			document.getElementById('hide_postage_id').value=p[a].value;
		}
	}
	if (!document.getElementById('recp_name').value) {
		alert('Please enter the recipient\'s full name.');
		document.getElementById('recp_name').focus();
		return false;
	}
	if (!document.getElementById('send_name').value) {
		alert('Please enter the sender\'s full name.');
		document.getElementById('send_name').focus();
		return false;
	}
	if (!document.getElementById('add_photo_1').checked && !document.getElementById('add_photo_2').checked) {
		alert('Please make a choice for the "Add a Photo" option\nas this will determine the voucher value and cost.');
		return false;
	}
	if (!document.getElementById('country').value) {
		alert('Please select the coutry that the novel will be sent to\nas this will determine the voucher value and cost.');
		document.getElementById('country').focus();
		return false;
	}
	if (not_checked) {
		alert('Please select the postal service that you would like the recipient to have.');
		return false;
	}
	if (document.getElementById('add_photo_1').checked) {
		document.getElementById('novel_type').value=1;
	}
	if (document.getElementById('add_photo_2').checked) {
		document.getElementById('novel_type').value=2;
	}
	document.getElementById('hide_recp_name').value=document.getElementById('recp_name').value;
	document.getElementById('hide_send_name').value=document.getElementById('send_name').value;
	document.getElementById('hide_occasion').value=document.getElementById('occasion').value;
	document.getElementById('hide_message_body').value=document.getElementById('message_body').value;
}

function formatChangeEdit(id) {
	document.getElementById('format_change').value=id;
}

function formatChange(id) {
	window.location='personal.php?bookid='+id;
}

function textCounter(field,cntfield,maxlimit) {
	if (field.value.length > maxlimit)	{
		field.value = field.value.substring(0, maxlimit);
		alert('Please note: The maximum length for the personalised message is '+ maxlimit + ' characters');
	} else {
		cntfield.value = field.value.length;
	}
}

function sameSex() {
	window.location='books.php?type=ss';
}

function giftVoucher() {
	window.location='voucher.php';
}

function giftAnnouncement() {
	window.location='announcement.php';
}

function giftAnnouncement() {
	window.location='announcement.php';
}

function sideHelp() {
	window.location='help.php';
}

function showSub(id) {
	document.getElementById(id).style.display='block';
}

function hideSub(id) {
	document.getElementById(id).style.display='none';
}

function makeDate(){
	if(document.getElementById('day').value && document.getElementById('month').value && document.getElementById('year').value){
		var date = new Date(document.getElementById('year').value, document.getElementById('month').value-1, document.getElementById('day').value, 00,00,00);
		var new_date=date.toLocaleString();
		var date_parts=new_date.split(" ");
		ShowDate(date_parts[0],date_parts[1],date_parts[2]);
	}
	
}

function ShowDate(day,month,year) {
	switch (day) {
		case "1":
		day=day+'st';
		break;
		case "2":
		day=day+'nd';
		break;
		case "3":
		day=day+'rd';
		break;
		case "21":
		day=day+'st';
		break;
		case "22":
		day=day+'nd';
		break;
		case "23":
		day=day+'rd';
		break;
		case "31":
		day=day+'st';
		break;
		default:
		day=day+'th';
	}
	document.getElementById('show_date').innerHTML=('('+day+ ' ' +month+ ' ' +year+')');
}

function voucherHigh(bg_colour) {
	document.getElementById('gv').style.backgroundColor=bg_colour;
}

function announceHigh(bg_colour) {
	document.getElementById('ga').style.backgroundColor=bg_colour;
}

function HighlightPreview(id1, id2) {
	document.getElementById(id1).style.backgroundColor='#EEE9D2';
	document.getElementById(id2).style.backgroundColor='#EEE9D2';
}

function ReturnHighlight(bg, id1, id2) {
	document.getElementById(id1).style.backgroundColor=bg;
	document.getElementById(id2).style.backgroundColor=bg;
}

function SubmitForm(form_name) {
	eval("document." + form_name + ".submit();")
}

function ValidateGiftAmount() {
	if (document.getElementById('gift_cert').value == '0') {
	alert('Please select an amount for this gift voucher.');
	document.getElementById('gift_cert').focus();
	return false;
	} else if (document.getElementById('gift_cert').value != '0') {
		document.form_gc.submit();
	}
}

function NoCheckout() {
	alert('Please add postal details to proceed to the checkout.');
}

function showIndexDesc(val_1, val_2) {
	var x=document.getElementsByTagName("div").length;
	for(var y=0; y<x; y++){
		if(document.getElementsByTagName("div")[y].className=='hide_me'){
			document.getElementsByTagName("div")[y].style.display='none';
		}
	}
	document.getElementById('novel_'+val_1).style.display='block';
}

function ShowRedeemer() {
	if (document.getElementById('redeem').checked) {
		document.getElementById('redeemer').style.display='block';
	} else {
		document.getElementById('redeemer').style.display='none';
	}
}

function ValidateFormPay() {
	document.form1.submit();
	document.getElementById('submit_payment').disabled=true;
}

function ValidateProfile() {
	if (!document.getElementById('user').value) {
	alert('Please enter your user name.');
	document.getElementById('user').focus();
	return false;
	}
	if (!document.getElementById('pass').value) {
	alert('Please enter your password.');
	document.getElementById('pass').focus();
	return false;
	}
}

function ValidateUpload() {
	if (!document.getElementById('new_image').value) {
		alert('Please select the image you wish to upload.');
		document.getElementById('new_image').focus();
		return false;
	}else{
		 $(document).ready(function() {
			 	$("#mask").show('fast');
			  	$("#progress").animate({ height: 'show', opacity: 'show' }, 'slow');
			  	return true;
		 });
		
	}
}

function ValidateGC() {
	if (!document.getElementById('name').value) {
	alert('Please enter the recipients name.');
	document.getElementById('name').focus();
	return false;
	}
	if (!document.getElementById('email').value) {
	alert('Please enter the recipients email.');
	document.getElementById('email').focus();
	return false;
	}
}

function ValidateCert() {
	if (!document.getElementById('cert_number').value) {
	alert('Please enter the Voucher Number to proceed.');
	document.getElementById('cert_number').focus();
	return false;
	}
	if (!document.getElementById('cert_pass').value) {
	alert('Please enter the Voucher Validator to proceed.');
	document.getElementById('cert_pass').focus();
	return false;
	}	
}

function ValidateAddressOpt() {
	if (!document.getElementById('address_opt_0').checked && !document.getElementById('address_opt_1').checked) {
	alert('Please select the option you would like to use for the address section.');
	return false;
	}
}

function ValidateAddressOptAdd() {
	if (!document.getElementById('address_opt_0').checked && !document.getElementById('address_opt_1').checked) {
	alert('Please select the option you would like to use for the address section.');
	return false;
	}
	if (!document.getElementById('address_opt_0').checked) {
		$(document).ready(function() {
			 $("#new_address").animate({ height: 'show', opacity: 'show' }, 'medium');
			 return true;
		});
	}
	if (!document.getElementById('address_opt_1').checked) {
	window.location="cart.php"
	return true;
	}
}

function ValidateAddress(form_num) {
	var address_count=document.getElementById('address_count').value;
	var not_checked=new Array();
	var p=new Array();
	var address='Shipping Address ';
	
	if(form_num==0){
		var form_val=1;
	}else{
		var form_val=parseInt(form_num);
		address_count=form_val;
	}
	
	for(var x=form_val; x<=address_count; x++){
		not_checked[x]=true
		p[x]=eval('document.form_1.postage_'+x+';');
		for(var a=0; a<p[x].length; a++){
			if(p[x][a].checked){
				not_checked[x]=false;
			}
		}
		if (!document.getElementById('fname['+x+']').value) {
			alert(address+x+'\nPlease enter the first name to proceed.');
			document.getElementById('fname['+x+']').focus();
			return false;
		}
		if (!document.getElementById('fname['+x+']').value) {
			alert(address+x+'\nPlease enter the first name to proceed.');
			document.getElementById('fname['+x+']').focus();
			return false;
		}
		if (!document.getElementById('sname['+x+']').value) {
			alert(address+x+'\nPlease enter the surname to proceed.');
			document.getElementById('sname['+x+']').focus();
			return false;
		}
		if (!document.getElementById('email['+x+']').value) {
			alert(address+x+'\nPlease enter an email address to proceed.');
			document.getElementById('email['+x+']').focus();
			return false;
		}
		if (!document.getElementById('email['+x+']').value.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/)) {
			alert("The email address entered does not appear to be valid.\nPlease re-enter the email address.");
			document.getElementById('email['+x+']').focus();
			return false;
		}
		if (!document.getElementById('address1['+x+']').value) {
			alert(address+x+'\nPlease enter an address to proceed.');
			document.getElementById('address1['+x+']').focus();
			return false;
		}
			if (!document.getElementById('city['+x+']').value) {
			alert(address+x+'\nPlease enter the town or city to proceed.');
			document.getElementById('city['+x+']').focus();
			return false;
		}
		if (!document.getElementById('province['+x+']').value) {
			alert(address+x+'\nPlease enter the province, county or state to proceed.');
			document.getElementById('province['+x+']').focus();
			return false;
		}
		if (!document.getElementById('country['+x+']').value) {
			alert(address+x+'\nPlease select a country to proceed.');
			document.getElementById('country['+x+']').focus();
			return false;
		}
		if (!document.getElementById('post_code['+x+']').value) {
			alert(address+x+'\nPlease enter a post code to proceed.');
			document.getElementById('post_code['+x+']').focus();
			return false;
		}
		for(var y=0; y<not_checked.length; y++){
			if (not_checked[y]) {
				alert(address+x+'\nPlease select the postal service that you would like to use.');
				return false;
			}
		}
	}
}