/* validacja formularzy stanley-solutions.co.uk */

document.write('<script type="text/javascript" src="lib/advajax.js"></script>');

	var regEmail = /^.+\@.+\..+$/;
	
	
	noweOkno = null;
	
	function pokaz_okno(src, w, h, wm, hm)
	{
		if(window.screen)
		{
			aw=screen.availWidth;
			ah=screen.availHeight;
		}
		
		if(noweOkno==null || noweOkno.closed){
    properties=
			"left=" + (aw-w)/2 + ","
			+"top=" + (ah-h)/2 + ","
			+"screenX=" + (aw-w)/2 + ","
			+"screenY=" + (ah-h)/2 + ","
			+"width=" + w + ","
			+"height=" + h + ","
			+"innerWidth=" + w + ","
			+"innerHeight=" + h + ","
			+"toolbar=no,"
			+"location=no,"
			+"directories=no,"
			+"status=no,"
			+"menubar=no,"
			+"scrollbars=no,"
			+"resizable=no"
			
       noweOkno = window.open(src,'info',properties);
		}
	};
	
/*
###########################################
##############FORM AD HOC
###########################################
*/

	function change_email()
	{
		document.forms[0].email2.value = document.forms[0].email.value;
	}
	
	function change_contact()
	{
		document.forms[0].contact_name2.value = document.forms[0].contact_name.value;
	}
	
	function change_no_contact()
	{
		if(document.forms[0].no_contact.checked == true)
		{
			document.forms[0].contact_name2.disabled = true;
			document.forms[0].contact_name2.value = "N/A";
		}
		else
		{
			document.forms[0].contact_name2.disabled = false;
			document.forms[0].contact_name2.value = "";
		}
	}
	
	function change_na()
	{
		if(document.forms[0].n_a.checked == true)
		{
			document.forms[0].company_name.disabled = true;
			document.forms[0].company_name.value = "N/A";
		} else
		{
			document.forms[0].company_name.disabled = false;
			document.forms[0].company_name.value = "";
		}
	}
	
	
	
	function change_need_invoice()
	{
		if(document.forms[0].need_invoice.checked == true)
		{
			document.forms[0].full_name.disabled = false;
			document.forms[0].line_1.disabled = false;
			document.forms[0].line_2.disabled = false;
			document.forms[0].town.disabled = false;
			document.forms[0].country.disabled = false;
			document.forms[0].postcode.disabled = false;
		} else {
			document.forms[0].full_name.disabled = true;
			document.forms[0].line_1.disabled = true;
			document.forms[0].line_2.disabled = true;
			document.forms[0].town.disabled = true;
			document.forms[0].country.disabled = true;
			document.forms[0].postcode.disabled = true;
		}
	}
	
	function calc_words()
	{
		var text = document.form_adhoc.text.value;
		var total = document.form_adhoc.text.value.length;
		var stawka = document.form_adhoc.stawka.value;
		var count = 1;
		var vat = 1;
			
			for (var i = 0; i < total; i++)
			{
				if ((text.charAt(i) == ' ') && (text.charAt(i+1) != '') && (text.charAt(i+1) != ' ')) count++;
			}
			
		if (total == 0 ) {count = 0; vat = 0;}
		
		document.form_adhoc.word_count.value = count;
		
		document.form_adhoc.charge.value = Math.round((count*stawka+vat)*100)/100;
		
		document.form_adhoc.charge_vat.value = Math.round((document.form_adhoc.charge.value*document.form_adhoc.stawka_vat.value)*100)/100;
	}
	
	function valid_adhoc()
	{
		if (!regEmail.test(document.form_adhoc.email.value)) { alert("Correct the field: E-mail!"); return false; }
		if (document.form_adhoc.contact_name.value.length <= 0) { alert("Correct the field: Your name!"); return false; }
		if (document.form_adhoc.telephone.value.length > 0 && (isNaN(document.form_adhoc.telephone.value))) { alert("Correct the field: Telephone!"); return false; }
		
		if (document.form_adhoc.contact_name2.value.length <= 0 && document.form_adhoc.no_contact.checked == false) { alert("Correct the field: Contact name!"); return false; }
		if (document.form_adhoc.company_name.value.length <= 0 && document.form_adhoc.n_a.checked == false) { alert("Correct the field: Company name!"); return false; }
		
		if (document.form_adhoc.need_invoice.checked == true)
		{
			if (document.form_adhoc.full_name.value.length <= 0) { alert("Correct the field: Full name / Trading name!"); return false; }
			if (document.form_adhoc.line_1.value.length <= 0) { alert("Correct the field: Address line #1!"); return false; }
			if (document.form_adhoc.town.value.length <= 0) { alert("Correct the field: Town!"); return false; }
			
			if (document.form_adhoc.postcode.value.length <= 0) { alert("Correct the field: Post code!"); return false; }
		}
		
		if (document.form_adhoc.subject.value.length <= 0) { alert("Correct the field: Subject!"); return false; }
		if (document.form_adhoc.text.value.length <= 0) { alert("Correct the field: Text!"); return false; }
		if (document.form_adhoc.confirm.checked == false) { alert("You must agree to terms and conditions of this service!"); return false; }
		
		return true;
	}
/*
###########################################
##############FORM TELEPHONE EMAIL
###########################################
*/
	function calc_words_left()
	{
		var text = document.form_telmail.text.value;
		var total = document.form_telmail.text.value.length;
		var stawka = document.form_telmail.stawka.value;
		var count = 1;
		var charge = 1;
		var pula = document.form_telmail.pula.value;
		pula++;
		var full_count = document.form_telmail.pula.value;
			
			for (var i = 0; i < total; i++)
			{
				if ((text.charAt(i) == ' ') && (text.charAt(i+1) != '') && (text.charAt(i+1) != ' ')) count++;
				if (count == pula) {count = 0; charge++};
			}
			
		document.form_telmail.word_left.value = full_count - count;
		
		document.form_telmail.charge.value = charge*stawka;
		

		if (total == 0)
		{
			document.form_telmail.word_left.value = document.form_telmail.pula.value;
			document.form_telmail.charge.value = stawka;
		}
		
		document.form_telmail.charge_vat.value = Math.round((document.form_telmail.charge.value*document.form_telmail.stawka_vat.value)*100)/100;
		
		}
	

	function valid_telmail()
	{
		if (!regEmail.test(document.form_telmail.email.value)) { alert("Correct the field: E-mail!"); return false; }
		if (document.form_telmail.contact_name.value.length <= 0) { alert("Correct the field: Your name!"); return false; }

		if (document.form_telmail.contact_name2.value.length <= 0 && document.form_telmail.no_contact.checked == false) { alert("Correct the field: Contact name!"); return false; }
		if (document.form_telmail.company_name.value.length <= 0 && document.form_telmail.n_a.checked == false) { alert("Correct the field: Company name!"); return false; }

		
		if (document.form_telmail.tel_na.checked == true)
		{
		if (document.form_telmail.address.value.length <= 0) { alert("Correct the field: Contact's address!"); return false; }
		
		} else 
		{
		if (document.form_telmail.telephone.value.length < 1 || (isNaN(document.form_telmail.telephone.value))) { alert("Correct the field: Telephone!"); return false; }

		}
		
		if (document.form_telmail.need_invoice.checked == true)
		{
			if (document.form_telmail.full_name.value.length <= 0) { alert("Correct the field: Full name / Trading name!"); return false; }
			if (document.form_telmail.line_1.value.length <= 0) { alert("Correct the field: Address line #1!"); return false; }
			if (document.form_telmail.town.value.length <= 0) { alert("Correct the field: Town!"); return false; }
			
			if (document.form_telmail.postcode.value.length <= 0) { alert("Correct the field: Post code!"); return false; }
		}
		
		if (document.form_telmail.subject.value.length <= 0) { alert("Correct the field: Subject!"); return false; }
		if (document.form_telmail.text.value.length <= 0) { alert("Correct the field: Text!"); return false; }
		if (document.form_telmail.confirm.checked == false) { alert("You must agree to terms and conditions of the service!"); return false; }
		
		return true;
	}

	function change_tel_na()
	{
		if(document.forms[0].tel_na.checked == true)
		{
			document.forms[0].telephone.disabled = true;
			document.forms[0].telephone.value = "I do not know";
			document.forms[0].address.disabled = false;
		} else
		{
			document.forms[0].telephone.disabled = false;
			document.forms[0].telephone.value = "";
			document.forms[0].address.disabled = true;
		}
	}

/*
###########################################
##############FORM CONTACT
###########################################
*/

	function valid_form_contact()
	{
		if (document.form_contact.contact_name.value.length <= 0) { alert("Correct the field: Contact name!"); return false; }

		if (document.form_contact.email.value.length == 0 && document.form_contact.telephone.value.length == 0)
		{
			alert("Correct the field: E-mail OR Telephone!"); return false;
		} else
		{
			if (document.form_contact.email.value.length > 0)
			{
				if (!regEmail.test(document.form_contact.email.value)) { alert("Correct the field: E-mail!"); return false;}
			
			} else 
			{
		if (document.form_contact.telephone.value.length < 1 || (isNaN(document.form_contact.telephone.value))) { alert("Correct the field: Telephone!"); return false; }
			}
		}
		
	if (document.form_contact.comments.value.length <= 0) { alert("Correct the field: Your enquiry!"); return false; }

		
		return true;
	}

/*
###########################################
##############QUOTE FORM
###########################################
*/

		function change_sample()
	{
		if(document.forms[0].thisis[1].checked == true)
		{
			document.forms[0].sample_word.disabled = false;
		}
		else
		{
			document.forms[0].sample_word.disabled = true;
		}
	}
	
		function change_date()
	{
		if(document.forms[0].time[4].checked == true)
		{
			document.forms[0].hour.disabled = false;
			document.forms[0].day.disabled = false;
			document.forms[0].month.disabled = false;
		}
		else
		{
			document.forms[0].hour.disabled = true;
			document.forms[0].day.disabled = true;
			document.forms[0].month.disabled = true;
		}
	}
	
	
	function valid_quote()
	{
		if (document.form_quote.thisis[0].checked == false && document.form_quote.thisis[1].checked == false) { alert("Choose the type document!"); return false; }
		
		if (document.form_quote.thisis[1].checked == true)
		{
			if (document.form_quote.sample_word.value.length == 0 || (isNaN(document.form_quote.sample_word.value))) {alert("Correct the field: Number of words!"); return false;}
		}
		
		if (document.form_quote.format[0].checked == false && document.form_quote.format[1].checked == false && document.form_quote.format[2].checked == false) { alert("Choose the format!"); return false; }
		
		if (!regEmail.test(document.form_quote.email.value)) { alert("Correct the field: E-mail!"); return false; }
		if (document.form_quote.contact_name.value.length <= 0) { alert("Correct the field: Contact name!"); return false; }
		
		if (document.form_quote.confirm.checked == false) { alert("You must agree to terms and conditions of this service!"); return false; }

		return true;
	}
	
/*
###########################################
##############AJAX FUNCTIONS
###########################################
*/

	function send_form_ajax()
	{
		/* dane klienta */
		var email = document.getElementById('email').value;
		var name = document.getElementById('name').value;
		var telephone = document.getElementById('telephone').value;
		var contact_name2 = document.getElementById('contact_name2').value;
		var company_name = document.getElementById('company_name').value;
		var need_invoice = document.getElementById('need_invoice').value;
		var full_name = document.getElementById('full_name').value;
		var line_1 = document.getElementById('line_1').value;
		var line_2 = document.getElementById('line_2').value;
		var town = document.getElementById('town').value;
		var country = document.getElementById('country').value;
		var postcode = document.getElementById('postcode').value;
		var etype = document.getElementById('etype').value;
		var subject = document.getElementById('subject').value;
		var text = document.getElementById('text2').value;
		var word_count = document.getElementById('word_count').value;
		var charge = document.getElementById('charge').value;
		var charge_vat = document.getElementById('charge_vat').value;
		var item_number = document.getElementById('item_number').value;
		
		var business = document.getElementById('business').value;
		var tax = Math.round((charge_vat-charge)*100)/100;
		var amount = charge;
		var item_name ="AD HOC translation";
		
		
		document.getElementById('submit').disabled = true;
		document.getElementById('btn_back').disabled = true;
		document.getElementById('submit').value = "waiting...";
		
		advAJAX.post({
    url: "classes/ajax.php",
    onSuccess : function(obj) {
        /* Ustawienie nowej, pobranej z serwera wartości */
				document.getElementById('submit').value = "waiting...";
				if (obj.requestDone == true) 
				self.setTimeout('window.location="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business='+business+'&item_name='+item_name+'&item_number='+item_number+'&amount='+amount+'&tax='+tax+'&no_shipping=1&return=http%3a%2f%2fwww%2estanley%2dsolutions%2eco%2euk%2findex%2ephp%3fid%3dorder&no_note=1&currency_code=GBP&lc=GB&bn=PP%2dBuyNowBF&charset=UTF%2d8";', 200);
    },
    onError : function(obj) {
				document.getElementById('submit').value = "Buy now!";
				document.getElementById('btn_back').value = "Correct";
				document.getElementById('submit').disabled = false;
				document.getElementById('btn_back').disabled = false;
				alert (' Error server! try later...');
    },
		"what" : "adhoc",
		"email" : email,
		"name" : name,
		"telephone" : telephone,
		"contact_name2" : contact_name2,
		"company_name" : company_name,
		"need_invoice" : need_invoice,
		"full_name" : full_name,
		"line_1" : line_1,
		"line_2" : line_2,
		"town" : town,
		"country" : country,
		"postcode" : postcode,
		"etype" : etype,
		"subject" : subject,
		"text" : text,
		"word_count" : word_count,
		"charge" : charge,
		"charge_vat" : charge_vat,
		"item_number" : item_number
	});

	}
	
	

	function send_form_ajax2()
	{
		/* dane klienta */
		var email = document.getElementById('email').value;
		var name = document.getElementById('name').value;
		var telephone = document.getElementById('telephone').value;
		var address = document.getElementById('address').value;
		var contact_name2 = document.getElementById('contact_name2').value;
		var company_name = document.getElementById('company_name').value;
		var need_invoice = document.getElementById('need_invoice').value;
		var full_name = document.getElementById('full_name').value;
		var line_1 = document.getElementById('line_1').value;
		var line_2 = document.getElementById('line_2').value;
		var town = document.getElementById('town').value;
		var country = document.getElementById('country').value;
		var postcode = document.getElementById('postcode').value;
		var subject = document.getElementById('subject').value;
		var text = document.getElementById('text2').value;
		var charge = document.getElementById('charge').value;
		var charge_vat = document.getElementById('charge_vat').value;
		var item_number = document.getElementById('item_number').value;
		
		var business = document.getElementById('business').value;
		var tax = Math.round((charge_vat-charge)*100)/100;
		var amount = charge;
		var item_name ="Telephone to E-mail translation";
		
		
		document.getElementById('submit').disabled = true;
		document.getElementById('btn_back').disabled = true;
		document.getElementById('submit').value = "waiting...";
		
		advAJAX.post({
    url: "classes/ajax.php",
    onSuccess : function(obj) {
        /* Ustawienie nowej, pobranej z serwera wartości */
				document.getElementById('submit').value = "waiting...";
				if (obj.requestDone == true)
				self.setTimeout('window.location="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business='+business+'&item_name='+item_name+'&item_number='+item_number+'&amount='+amount+'&tax='+tax+'&no_shipping=1&return=http%3a%2f%2fwww%2estanley%2dsolutions%2eco%2euk%2findex%2ephp%3fid%3dorder&no_note=1&currency_code=GBP&lc=GB&bn=PP%2dBuyNowBF&charset=UTF%2d8";', 200);
    },
    onError : function(obj) {
				document.getElementById('submit').value = "Buy now!";
				document.getElementById('btn_back').value = "Correct";
				document.getElementById('submit').disabled = false;
				document.getElementById('btn_back').disabled = false;
				alert (' Error server! try later...');
    },
		"what" : "telmail",
		"email" : email,
		"name" : name,
		"telephone" : telephone,
		"address" : address,
		"contact_name2" : contact_name2,
		"company_name" : company_name,
		"need_invoice" : need_invoice,
		"full_name" : full_name,
		"line_1" : line_1,
		"line_2" : line_2,
		"town" : town,
		"country" : country,
		"postcode" : postcode,
		"subject" : subject,
		"text" : text,
		"charge" : charge,
		"charge_vat" : charge_vat,
		"item_number" : item_number
	});

	}
	
	
	function get_id_files(ID)
	{
		var test_div = window.opener.document.getElementById('test_div');
		
		advAJAX.get({
    url: "classes/ajax_file.php",
	    parameters : {
      "ID" : ID
    },
    onSuccess : function(obj) {
        /* Ustawienie nowej, pobranej z serwera wartości */
				if (obj.requestDone == true) self.setTimeout('self.close()', 200);
				test_div.innerHTML = obj.responseText;
    },
    onError : function(obj) {
				alert(' error server \n try later..');
    }
	});
	}
	
	
	function remove_file(ID, name)
	{
		var test_div = document.getElementById('test_div');
		
		advAJAX.get({
    url: "classes/ajax_file.php",
	    parameters : {
      "ID" : ID,
			"name" : name
    },
    onSuccess : function(obj) {
        /* Ustawienie nowej, pobranej z serwera wartości */
				test_div.innerHTML = obj.responseText;
    },
    onError : function(obj) {
				alert(' error server \n try later..');
    }
	});

	}

/*
#################################################
############# INNE FUNKCJE
#################################################
*/
	
	function valid_form_upload()
	{
		if (document.form_upload.file.value.length == 0) { alert("Choose the file!"); return false; }
		
		document.form_upload.btn_upload.value = "waiting..";
		document.form_upload.btn_upload.disabled = true;
		
		return true;
	}