//<!-- Begin Proximita JS code

var base_url =  "http://192.168.1.101/MPLecommerce/";
//var base_url =  "http://www.microkan.com.php4-2.websitetestlink.com/";
//var base_url =  "http://www.pressureswitch.com/ecommerce/index.php/";
var partsAd =0;

/*Put enabled or not the fields in the selectionShop view.
*/


function showQuantityBox(i,totalParts){

	if(	document.getElementById('rangeValue['+i+']').disabled == true){
		document.getElementById('partQuantity['+i+']').disabled=false;
		document.getElementById('rangeValue['+i+']').disabled=false;
		document.getElementById('submitButton').disabled=false;
	}
	else{

		document.getElementById('partQuantity['+i+']').disabled=true;
		document.getElementById('rangeValue['+i+']').disabled=true;
		document.getElementById('partQuantity['+i+']').value="";
		document.getElementById('rangeValue['+i+']').value="";
		document.getElementById('partPrice['+i+']').value="";
	}
	
	var disabled = 0;
	if(document.getElementById('submitButton').disabled==false){
		for(var a=1;a<=totalParts;a++){
			if(document.getElementById('partQuantity['+a+']').disabled==true){
				disabled++;
			}
		}	
	}
	
	if(disabled == totalParts){
		document.getElementById('submitButton').disabled=true;
	}
}

/*  to put disabled the "add" button in the initial view*/
function disableAddButton(totalChecked){
	//alert(totalChecked);
	var disabled = 0;

	for(var a=1;a<=totalChecked;a++){
		if(document.getElementById('checkBox1['+a+']').checked==true){
			disabled++;
		}
	}	

	if(document.getElementById('submitButton').disabled=true && disabled >0){
		document.getElementById('submitButton').disabled=false;

	}
	else{
		document.getElementById('submitButton').disabled=true;
			//	document.getElementById('submitButton').class='blueButtonDisabled';
	}
	
}


function updateValue(element,quantity,i,stock,check){

	//alert(element+"  dddd   "+quantity+"  ddddd   "+i +"   stock "+stock);	

	if(check == 1){		
	}
	
	var temp = new Array();
	temp = element.split(';');//split in elements the big string
	
	var size=temp.lenght;

	var realPrice = 0;
	var tempPrice = 0;
	var correctPrice = 0;
	
	correctPrice = temp[0].split(',');
	

	if(quantity>stock){//to display the a stock error message
			
		if((stock)== 0){
			alert("This part is out of stock, please use the contact form to request  part.");
				document.getElementById('partQuantity['+i+']').disabled=true;
				document.getElementById('rangeValue['+i+']').disabled=true;
				document.getElementById('partQuantity['+i+']').value="";
			//	document.getElementById('rangeValue['+i+']').value="";
				document.getElementById('checkShop['+i+']').checked = false;
				
				
						/*if(document.getElementById('pk_idPart['+i+']').checked==true){
							document.getElementById('pk_idPart['+i+']').checked = false;
						}*/
			return false;
		}
			alert(" The maximun quantity added should be less or equal to "+stock);
			document.getElementById('partQuantity['+i+']').value=stock;
			
			
			
			
			document.getElementById('partPrice['+i+']').value="";
			document.getElementById('partPriceR['+i+']').value="";
			/*alert(correctPrice[3]+"  "+correctPrice[2]+" "ame+correctPrice[1]+" "+correctPrice[0]);
			setPrice(correctPrice[3],i,stock);
			setPrice(correctPrice[3],i,stock);*/
			
			for (var a in temp){//split each line, to know the range and the price.
		priceLine = temp[a].split(',');		
		tempPrice = priceLine[3];
			if(priceLine[0]>0){
				if((parseInt(stock) > parseInt(priceLine[1])) && (parseInt(stock) < parseInt(priceLine[2]))){//this check taht the quantity is between a range
					realPrice = parseInt(stock)*parseInt(priceLine[3]);
					//alert(realPrice+" i->"+i+" stocl->"+stock);
					setPrice(realPrice,i,stock);
				}
				if((parseInt(stock)==parseInt(priceLine[1])) || (parseInt(stock)==parseInt(priceLine[2]))){//this check the border values of the range for the quantity
					realPrice = stock*priceLine[3];
					setPrice(realPrice,i,stock);
					//alert(realPrice+" i->"+i+" stocl->"+stock+"   2d if");
				}
			}		
		}
			
	
	}else{

		/*if(!(quantity>0) ){//if the user add a wrong value the system will put the minimun value to the buy
			alert("Introduce a value plus than cero please");
			document.getElementById('partQuantity['+i+']').value=1;
			document.getElementById('partPrice['+i+']').value=correctPrice[3];//add the first price
			setPrice(correctPrice[3],i,1);
		}*/
	
		for (var a in temp){//split each line, to know the range and the price.
			priceLine = temp[a].split(',');		
			tempPrice = priceLine[3];
			if(priceLine[0]>0){
				if((parseInt(quantity) > parseInt(priceLine[1])) && (parseInt(quantity) < parseInt(priceLine[2]))){//this check taht the quantity is between a range
					realPrice = parseInt(quantity)*parseInt(priceLine[3]);
					setPrice(realPrice,i,quantity);
									//	alert(realPrice+" i->"+i+" quantity->"+quantity+"   quantity if");
				}
				if((parseInt(quantity)==parseInt(priceLine[1])) || (parseInt(quantity)==parseInt(priceLine[2]))){//this check the border values of the range for the quantity
					realPrice = quantity*priceLine[3];
					setPrice(realPrice,i,quantity);
									//	alert(realPrice+" i->"+i+" quantity->"+quantity+"   quantity 2 if");
				}
			}		
		}
	}
	//  for add checks in the shopSelection view(those checks are becose this parts will be added in the cart)
	if(check == 1){
		//alert("!!");
				document.getElementById('pk_idPart['+i+']').value="checked";
	}

}

/*
*	Validate the value, and display and error message if is nessesary
*/
function setPrice(realPrice,i,quantity){
	
					//	alert(realPrice+" i->"+i+" stocl->"+quantity+"   in the real function");
	
	if(!quantity>0){
		alert("The values should be numbers above zero.");
	}
	else{
		price = realPrice;
		document.getElementById('partPrice['+i+']').value=price;
		document.getElementById('partPriceR['+i+']').value=realPrice;
		
		document.getElementById('quantity_'+i).value=quantity;		
		
		var unit_value = realPrice/quantity; //problema con decimales
		
		document.getElementById('amount_'+i).value=unit_value;
		
		partsAd++;
		updateSubtotal();	
	}
}

/**
*   Update the total price of the buy
*/
function updateSubtotal(){
	
	var total = 0;
	var numberParts = document.getElementById('numberParts').value;
	document.getElementById('subTotal').value=0;
	for(var i=1;i<=numberParts;i++){
		total = total + parseFloat(document.getElementById('partPrice['+i+']').value);	
		document.getElementById('subTotal').value=total;
	}

}

/*
* Calculate the suttotal value by user peticion
*/
function calculateSubtotal(){
		
	updateSubtotal();	
	
		
			
}

/*
*    Validate the quantities when the user try to finish the buy
*/

/*function validate(i) {
	alert(document.getElementById('partQuantity['+i+']').value);
	exit;
	for (var i in temp)
	document.getElementById('partQuantity['+i+']').;
	
	if (!(form.partQuantity.value.length > 0))
    {
        alert("Introduce valid values please")
        form.pwd.focus()
        return false
    }
}*/

/*
*    http://blog.smaldone.com.ar/2006/12/11/ajax-no-es-ajax/
*/

/*
*  Return a variable with the number of quantities added
*/
function purchaseParts(){
	return partsAd;
}

/*
*	Control the flow between complete the buy and continue with the payment view
*/
function completeBuy(partsQuantity){
	//alert(purchaseParts()+ "  --> "+ partsQuantity);
	var contFlow = 1;
	var value = 0;
	var numberParts = document.getElementById('numberParts').value;
	
	var total = 0;
	for(var i=1;i<=numberParts;i++){
		total = parseFloat(document.getElementById('partPrice['+i+']').value);	
		if(!total > 0){
			alert("Please introduce all the quantities");
			contFlow = 2;
			return false;	
		}
	}
	if(contFlow == 1){
			document.getElementById('validProcess').value="1";
			return true; 		
	}
/*		value =parseFloat(document.getElementById('partPriceR['+i+']').value);
		alert(value+ "  ->#  "+partQuantity);
		if(value > 0){
			alert("Please introduce all the quantities");
			contFlow = 2;
			return false;	
		}
	}*/
}

/*
*		To check that before press the add button in the shop view(Select series)
*		the user must press a check.
*/
function seriesAdded(totalSeries){
	//alert(totalSeries);
	return true;
	var contFlow = 1;
	var total = "";
	for(var i=0;i<seriesNumber;i++){
		total = (document.getElementById('partModel['+i+']').value);	
		alert(total);
		if(total != 'checked'){
			alert("Select a Series before pressing the 'Add' button.");
			contFlow = 2;
			return false;	
		}
	}
	if(contFlow == 1){//the flow should continue
			return true; 		
	}
}

function buttonAvailable(){
	document.getElementById('submitButton').disabled='false';
}

/*
*	to validate the data in the shopSelection view.
*/
function rangePriceValidator(ranges,numberParts,partNames){

	//alert(ranges+"  rrr "+i);
	var temp = new Array();
	temp = ranges.split(';');//split in elements the big string
	
	var names = new Array();
	names = partNames.split(';');//split in elements the big string
	
	var message = "Please introduce valid number in the range field for the parts:\n";
	var qMessage = "The quantity values should be numbers above zero.\n";
	var error = 0;
	
	var forQuantity = parseInt(numberParts);
	var countRange = 0;
	var countAble = 0;
	var countPosPrices = 0;
	for(var i=0;i<forQuantity;i++){
		elements = temp[i].split(',');//split to get the min and max posible values for the range
		var a = i+1;
		
	//	range 	 = parseFloat(document.getElementById('rangeValue['+a+']').value);	//get the value give by the user
		quantity = parseFloat(document.getElementById('partQuantity['+a+']').value);	//get the quantity give by the user
		
		minValue = parseFloat(elements[1]);
		maxValue = parseFloat(elements[2]);
		
		/*if((range>maxValue) || (range<minValue) || (!range>0) && (document.getElementById('rangeValue['+a+']').disabled == false)){
			message = message+" - "+names[a]+"\n";
			error = 1;			
		}*/
		
		if((!quantity>0 || quantity < 0) && (document.getElementById('partQuantity['+a+']').disabled == false)){
			qMessage = qMessage+" - "+names[a]+"\n";
			error = 2;	
			
		}
		
	//	if(range > 0){ countRange++;}// gcheck if the range was introduced
		if(quantity > 0){ countPosPrices++;}// gcheck if the range was introduced
	//	if(document.getElementById('rangeValue['+a+']').disabled == false  ){countAble++;}//get the quantity of able ranges

	}
	
	if(error == 1){//to display the error message
		alert(message);
		return false;	
	}
	
	if(error == 2){//to display the error message
		alert(qMessage);
		return false;	
	}
	
/*	if((countAble - countRange) != 0){//control if is needed to introduce more values
		alert("Please complete all the ranges values for the selected parts.");
		return false;
	}*/
	
	alert(countPosPrices+" number of introduced, "+countAbleQuantity+"  able quantitites")
	return false;
	if((countAbleQuantity - countPosPrices) != 0){//control if is needed to introduce more prices
		alert("Please complete all the quantity fields for the selected parts.");
		return false;
	}

	//updateSubtotal();
	return true;
}


/*
*	Check that the user add a good range value, 
*/ 
function validateRange(value, min, max){
	//alert(value+ '--> min:'+min+'  -->max:'+max);
	var realValue = 0;
	realValue = parseFloat(value);
	realMin = parseFloat(min);
	realMax = parseFloat(max);
	
	//alert(value+ '--> min:'+min+'  -->max:'+max);
	//alert(realValue+ '--> min:'+realMin+'  -->max:'+realMax);
	if(!realValue > 0){
		alert("Introduce a valid number please");
	}
	
	if((realValue>realMax) || (realValue<realMin)){//principal range control
		alert("The value should be between "+realMin+" and "+realMax);
		if(realValue > realMax){
			document.getElementById('rangeValue['+i+']').value=realMax;//complete the field with the max posible value
		}
		else if(realValue < realMin){
			document.getElementById('rangeValue['+i+']').value=realPrice;	
		}
	}
}

/*
*		Put disabled the shiping fields
*
*/
function enabledShipInfo(){
	

	//shipSame
	if(document.getElementById('shipSame').checked == true){
		//alert("precionado");
		document.getElementById('shipName').disabled = true;
		document.getElementById('shipFirstName').disabled = true;
		document.getElementById('shipLastName').disabled = true;
		document.getElementById('shipAdress1').disabled = true;
		document.getElementById('shipAdress2').disabled = true;
		document.getElementById('shipCity').disabled = true;
		document.getElementById('shipState').disabled = true;
		document.getElementById('shipZip').disabled = true;
		document.getElementById('shipCountry').disabled = true;
	}
	else{ 
		document.getElementById('shipName').disabled = false;
		document.getElementById('shipFirstName').disabled = false;
		document.getElementById('shipLastName').disabled = false;
		document.getElementById('shipAdress1').disabled = false;
		document.getElementById('shipAdress2').disabled = false;
		document.getElementById('shipCity').disabled = false;
		document.getElementById('shipState').disabled = false;
		document.getElementById('shipZip').disabled = false;
		document.getElementById('shipCountry').disabled = false;
	}
	return true;
}












