function getAjax(form_name, url) {
	send_message = getInputs(form_name);
	ajax_function(after, "POST", url, "", send_message);
	//url = "classes/modules/filters/script.php";
	//ajax_function(after_post, "POST", url, "", send_message);
}

function update_cart(form_name)
{
	if (isSAvisible()) {
		stat = document.checkout.shipping_state.value;
	} else {
		stat = document.checkout.billing_state.value;
	}
	url = "cart.php?target=checkout&ajax=Y&state="+stat;
	getAjax(form_name,url);
}

function afiseaza_actualizeaza() {
	 bonus_products();
	document.getElementById("mesaj_actualizare").style.display = "";
}

function isSAvisible() {
	if ( document.getElementById("shipping_info").style.display == "none" )
		return false;
	return true;
}

function showShippingAddress(form) {
	if (isSAvisible()) {
		document.getElementById("shipping_info").style.display = "none";
		document.getElementById("completare_facturare").style.display = "";
		document.getElementById('cart_form').submit();
	}
	else {
	 	document.getElementById("shipping_info").style.display = "";
		document.getElementById("completare_facturare").style.display = "none";
		document.getElementById('cart_form').submit();
	}
}

function after(responseText) {
	cartDiv = document.getElementById("main_cart");
	cartDiv.innerHTML = responseText;
}

function getInputs(form_name) {
	myForm = document.getElementById(form_name);
	if (!myForm) { myForms = document.getElementsByName(form_name); myForm = myForms[0]; }
	if (!myForm) return "";
	string = "";
	for (var i=0;i<myForm.length;i++) {
			name = myForm.elements[i].name;
			value = myForm.elements[i].value;
			string += name+"="+value;
			if (i!=myForm.length-1) string+= "&";
		}
	return string;
}

function continueCheckout()
{
	if (!document.checkout.payment_id) {
		alert("No payment methods are available! Please contact the store administrator.");
		return false;
	}

	pass = false;
	if (document.checkout.payment_id.length && document.checkout.payment_id.length > 0) {
		for (i = 0; i < document.checkout.payment_id.length; i++) {
			if (document.checkout.payment_id[i].checked == true) {
				pass = true;
				break;
			}
		}

	} else {
		pass = document.checkout.payment_id.checked;
	}

	if (pass == false) {
		alert("To proceed with checkout, you need to select a payment method.");
		return false;
	}
	
	return checkContactInformation();
}

function checkContactInformation() {
	if (document.checkout.login) {
		email = document.checkout.login.value;
		modify = document.checkout.modify.values;
		pass = document.checkout.password.value;
		rpass = document.checkout.confirm_password.value;
		fname = document.checkout.billing_firstname.value;
		lname = document.checkout.billing_lastname.value;
	}
	
	
	phone = document.checkout.billing_phone.value;
	address = document.checkout.billing_address.value;
	city = document.checkout.billing_city.value;
	state = document.checkout.billing_state.selectedIndex;
	
	if (document.checkout.billing_person[0].checked) person = "fizica";
	else person = "juridica";
	
	cnp = document.checkout.billing_cnp.value;
	iddata = document.checkout.billing_iddata.value;
	
	company = document.checkout.billing_company.value;
	ssaddress = document.checkout.billing_ssaddress.value;
	regno = document.checkout.billing_regno.value;
	cui = document.checkout.billing_cui.value;
	iban = document.checkout.billing_cIBAN.value;
	bank = document.checkout.billing_bank.value;
	
	
	//alert(state);
	//alert(cstate);
	if (document.checkout.login) {
		if (email == "") {
			alert("Pentru a finaliza comanda trebuie sa completati campul - Email.");
			return false;
		}
		if (fname == "") {
			alert("Pentru a finaliza comanda trebuie sa completati campul - Prenumele.");
			return false;
		}
		if (lname == "") {
			alert("Pentru a finaliza comanda trebuie sa completati campul - Numele.");
			return false;
		}
		if (modify == "true") {
			if (pass == "" || rpass == "" ) {
				alert("Pentru a finaliza comanda trebuie sa completati campul - Parola si confirmare parola.");
				return false;
			}
		}
	}

	
	if (phone == "") {
		alert("Pentru a finaliza comanda trebuie sa completati campul - Telefon.");
		return false;
	}
	if (address == "") {
		alert("Pentru a finaliza comanda trebuie sa completati campul - Adresa.");
		return false;
	}
	if (city == "") {
		alert("Pentru a finaliza comanda trebuie sa completati campul - Oras.");
		return false;
	}
	
	if (state == 0) {
		alert("Pentru a finaliza comanda trebuie sa completati campul - State(Judet).");
		return false;
	}
	
	if (cnp == "") {
		alert("Pentru a finaliza comanda trebuie sa completati campul - CNP.");
		return false;
	}
	if (iddata == "") {
		alert("Pentru a finaliza comanda trebuie sa completati campul - CI buletin.");
		return false;
	}
	
	if (person == "juridica" && company == "") {
		alert("Pentru a finaliza comanda trebuie sa completati campul - Firma.");
		return false;
	}
	
	if (person == "juridica" && ssaddress == "") {
		alert("Pentru a finaliza comanda trebuie sa completati campul - Adresa.");
		return false;
	}
	if (person == "juridica" && regno == "") {
		alert("Pentru a finaliza comanda trebuie sa completati campul - Nr.reg.Reg.Com.");
		return false;
	}
	if (person == "juridica" && cui == "") {
		alert("Pentru a finaliza comanda trebuie sa completati campul - CUI.");
		return false;
	}
	if (person == "juridica" && iban == "") {
		alert("Pentru a finaliza comanda trebuie sa completati campul - IBAN.");
		return false;
	}
	if (person == "juridica" && bank == "") {
		alert("Pentru a finaliza comanda trebuie sa completati campul - Banca.");
		return false;
	}		
	return true;
}

function checkShippingAddress() {
	
	shipping_phone = document.checkout.shipping_phone.value;
	shipping_address = document.checkout.shipping_address.value;
	shipping_city = document.checkout.shipping_city.value;
	shipping_state = document.checkout.shipping_state.selectedIndex;
	
	if (isSAvisible()){
			if (shipping_phone == "") {
				alert("Pentru a finaliza comanda trebuie sa completati campul - Telefon.");
				return false;
			}
			if (shipping_address == "") {
				alert("Pentru a finaliza comanda trebuie sa completati campul - Adresa.");
				return false;
			}
			if (shipping_city == "") {
				alert("Pentru a finaliza comanda trebuie sa completati campul - Oras.");
				return false;
			}
	
			if (shipping_state == 0) {
				alert("Pentru a finaliza comanda trebuie sa completati campul - State(Judet).");
				return false;
			}
	}
	else {
		
		
		billing_phone = document.checkout.billing_phone.value;
		billing_address = document.checkout.billing_address.value;
		billing_city = document.checkout.billing_city.value;
		billing_state = document.checkout.billing_state.selectedIndex;
		
		if (shipping_phone=="" && shipping_address=="" && shipping_city == "" && shipping_state == 0)
			return true;
		
		if (shipping_phone != billing_phone || shipping_address != billing_address || shipping_city != billing_city || shipping_state != billing_state ) {
			answer = confirm("Ati completat adresa de livrare si este diferita de cea de facturare! Doriti sa continuati ?");
			if (answer) return true;
			else {
				showShippingAddress(document.checkout);
				return false;
			}
		}
	}
}

function checkCNP() {
	cnp = document.checkout.billing_cnp.value;
	for (i=0; i<cnp.length; i++)
		if (cnp.charCodeAt(i)<48 || cnp.charCodeAt(i) > 57) {
			alert("Campul CNP trebuie sa fie numeric!");
			return false;
		}
	return true;
}

function CheckoutSubmit()
{
    if (continueCheckout() == false) return;
	if (checkShippingAddress() == false) return;
	if (checkCNP() == false) return;
	if (checkContactInformation() == false) return;
	var Element = document.getElementById("submit_order_button");
    if (Element) {
        Element.style.display = 'none';
    }

    var Element = document.getElementById("submiting_process");
    if (Element) {
        Element.style.display = "";
        Element.innerHTML = "<b>Comanda ta se trimite acum...</b>";
    }
	
	//window.scroll(0, 100000);
    document.checkout.submit();
}

function modifySsaddress() {
	document.checkout.billing_ssaddress.value = document.checkout.billing_address.value;
}

function changePerson(fizic) {
	if (fizic) {
		if (document.checkout)
			document.checkout.billing_person[0].checked = true;
		if (document.profile_form) 
			document.profile_form.billing_person[0].checked = true;
		if (document.registration_form)
			document.registration_form.billing_person[0].checked = true;
		document.getElementById("info_PF").style.display = "";
		document.getElementById("info_PJ").style.display = "none";
		document.getElementById("tp_fizica").style.display = "";
		document.getElementById("tp_juridica").style.display = "none";
	} else {
		if (document.checkout)
			document.checkout.billing_person[1].checked = true;
		if (document.profile_form) 
			document.profile_form.billing_person[1].checked = true;
		if (document.registration_form)
			document.registration_form.billing_person[1].checked = true;
		document.getElementById("info_PF").style.display = "none";
		document.getElementById("info_PJ").style.display = "";
		document.getElementById("tp_fizica").style.display = "none";
		document.getElementById("tp_juridica").style.display = "";
	}
}

function changeState(state, prefix)
{	
	obj = document.getElementById(prefix+'_custom_state_body');
	if (obj && state) {
		obj.style.display = (state.value == -1) ? "" : "none";
	}
	/*if (prefix == "billing_state") {
		if (state.value == document.checkout.initialIndexbilling_state.value)  {
			return;
		}
		else document.checkout.initialIndexbilling_state.value = state.value;
	}
	else {
		if (state.value == document.checkout.initialIndexshipping_state.value) {
			return;
		} 
		else document.checkout.initialIndexshipping_state.value = state.value;
			
	}*/
	if (document.getElementById('cart_form')) {
		if (((prefix=="billing" && !isSAvisible())||prefix == "shipping")&&state.value != 0)
			document.getElementById('cart_form').submit();
	}
}

function bonus_products() {
var i=0;
while (1!=0) {
	iput = document.getElementById("produs"+i);
	if (iput) {
		pret = iput.value;
		if (pret == "0") {
			document.getElementById("amount["+i+"]").value = document.getElementById("amount["+(i-1)+"]").value;
		}
		i++;		
	} else break;
}
}

function checkPaymentGC() {
var chosen = "";
len = document.checkout.payment_id.length
    
for (i = 0; i <len; i++) {
    if (document.checkout.payment_id[i].checked) {
	chosen = document.checkout.payment_id[i].value;
    }
}

if (chosen == "gift_certificate") {
    document.getElementById("gc_div").style.display = "";
    document.getElementById("gc_div").innerHTML = "Cod Tichet Cadou&nbsp;&nbsp; <font class='Star'>*</font> <input type='text' size=20 name='gcid'>&nbsp;&nbsp;<a href='javascript: CheckoutSubmit();'><font color=red>Valideaza Tichet Cadou</font></a><br><br>Acum te rog introdu cu atentie datele contului, Valideaza tichetul si Finalizeaza Comanda";
}
else {
    document.getElementById("gc_div").style.display = "none";
    document.getElementById("gc_div").innerHTML = "";

}
}



