var press = true;
var total = 0;
var order = new Array();
var model  = new Array("m171","m173","mfliptip","m170","m138","m176");
var prices = new Array(9.9,9.9,9.9,6,5.9,15.2);
var x;
var total = 0;
var order_array ='';

function takeaction() {

// DOUBLE CLICK PREVENT
	if (!press) {
		return false;
	}
	else {
		press = false;
	}
	
// CHECK TOTAL
	if (total == 0) {
		text2error('total_price');
		text2error('total_price_text');
		press = true;
		return false;
	}
	else {
		text2normal('total_price');
		text2normal('total_price_text');
		var totalsum = total;
	}

// CHECK NAME
	if ($('name').value.blank()) {
		input2error('name');
		$('name').focus();
		press = true;
		return false;
	}
	else {
		input2normal('name');
		var name = $('name').value;
	}

// CHECK PHONE
	if ($('phone').value.blank()) {
		input2error('phone');
		$('phone').focus();
		press = true;
		return false;
	}
	else {
		input2normal('phone');
		var phone = $('phone').value;
	}

// CHECK EMAIL
	if ($('email').value.blank()) {
		input2error('email');
		$('email').focus();
		press = true;
		return false;
	}
	else {
		input2normal('email');
		var email = $('email').value;
	}

// CHECK CITY
	if ($('city').value.blank()) {
		input2error('city');
		$('city').focus();
		press = true;
		return false;
	}
	else {
		input2normal('city');
		var city = $('city').value;
	}

// CHECK ZIP
	if ($('zip').value.blank()) {
		input2error('zip');
		$('zip').focus();
		press = true;
		return false;
	}
	else {
		input2normal('zip');
		var zip = $('zip').value;
	}

// CHECK ADDRESS
	if ($('address').value.blank()) {
		input2error('address');
		$('address').focus();
		press = true;
		return false;
	}
	else {
		input2normal('address');
		var address = $('address').value;
	}
	
// AJAX
	var url = './submitorder.php';
	var pars = {'name' : name, 'phone' : phone, 'email' : email, 'city' : city, 'zip' : zip, 'address' : address,'total' : totalsum,'order_array' : order_array};
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: pars, 
			onComplete: function(transport) {

			// HIDE FORM
				$('order_table').style.display = 'none';
				$('form_table').style.display = 'none';
				$('order_btn_shell').style.display = 'none';
				$('orderstatus').style.display = 'inline';
				$('tracker').insert(transport.responseText);
				press = true;
			
			}
		}
	);
}

function boost_q(modelid)
{
	total = 0;
	order = new Array();

	var max = 5;
	var el = ge('qty'+modelid);
	var x;
	var q=0;
	var tmp=0;
	var model_price=0;
	order_array='';
	
	try { el.selectedIndex = el.selectedIndex+1; } catch (err) { }
		
	for (x in model)
  	{
	   if(q = getValue('qty'+model[x]) && model[x])
	   { 	  
	   	el = ge('qty'+model[x]); 
 	   	total = roundNumber(total + (el.selectedIndex*prices[x]),2,false);
 	   	total_display = roundNumber(total,2,true);
 	   	model_price = roundNumber(el.selectedIndex*prices[x],2,true);
 	   	setInnerHtml('total_price_'+model[x],model_price+' лв.');

 	   	if(el.selectedIndex>0)
 	   	{
 	   		if(el.selectedIndex==1) { tmp = ' брой - '; } else { tmp = ' броя - '; }
			order_array += model[x]+'='+el.selectedIndex+',';
 	   	}
	   }//if
  	}//for
  	setInnerHtml('total_price',total_display+' лв.');
  

// CLEAR ERROR
	if (total > 0) {
		text2normal('total_price');
		text2normal('total_price_text');
	}

}//boost_q


function input2normal(id) {
	$(id).style.borderColor = '#83715C';
	$(id).style.backgroundColor = '#FFFFFF';
}

function input2active(id) {
	$(id).style.borderColor = '#FEC211';
}

function input2error(id) {
	$(id).style.backgroundColor = '#FFF3CF';
	$(id).focus();
}

function text2error(id) {
	$(id).style.color = '#C70E0E';
}

function text2normal(id) {
	$(id).style.color = '#453A33';
}

function statushide(status) {
	$(status).style.display = 'none';

}

function statusshow(status) {
	$(status).style.display = 'inline';
	statushide.delay(5,status)
}

function getValue(elementId)
{	
	if (document.getElementById(elementId) != undefined)
	{
		return  document.getElementById(elementId).value;
	}else
	{
		return false;
	}
}

function setValue(elementId,value)
{	
	if (document.getElementById(elementId) != undefined)
	{
		document.getElementById(elementId).value=value;
	}else
	{
		return false;
	}
}


function setInnerHtml(elementId,value)
{	
	if (document.getElementById(elementId) != undefined)
	{
		document.getElementById(elementId).innerHTML=value;
	}else
	{
		return false;
	}
}

function ge(elementId)
{	
	if (document.getElementById(elementId) != undefined)
	{
		return  document.getElementById(elementId);
	}else
	{
		return false;
	}
}

function roundNumber(num, dec,display) {
	if (num == 0) {
		return 0;
	}
	var res = 0;
	res = num;
	try
	{
		res = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec)
		s = new String(res);		
		if(s.indexOf('.') < 0) { s += '.00';} //return parseFloat(s);
		if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
		if(display)
		{
			return s;
		}else
		
		{
			return parseFloat(s);
		}
	}catch(x) { return 0; }	
}

window.onload = function() {
	boost_q(false);
};