/* Javascript for the productDetail page */
function getProductPrice(iProductKey, iProductName)
{		
   var url = "call/calculateproduct.php?quantity=1&productkey=" + iProductKey + "&productname=" + iProductName;
   var xmlhttp;
   var async = false;
   if (window.ActiveXObject)   // ActiveX version
   {
     xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");  // Internet Explorer 
   }
   else if (window.XMLHttpRequest)     // Object of the current windows
   { 
     xmlhttp = new XMLHttpRequest();     // Firefox, Safari, ...
   } 

   xmlhttp.open("GET", url, async);
   xmlhttp.send(url);
   
   //alert(url);
   //alert(xmlhttp.responseText);
   
   return xmlhttp.responseText;
}


function calculateProduct(iKey, oProductTargetId, oTotalTargetId, noCalculate)
{
	if (!noCalculate) {
		var configMargin = document.getElementById("CONFIG_MARGIN").value;
		
		//var prodWidth = document.getElementById("PROD_STANDARD_WIDTH").value;
		//var prodLength = document.getElementById("PROD_STANDARD_LENGTH").value;
		var prodGlass = document.getElementById("PROD_GLASS").value;
		var prodQuantity = document.getElementById("PROD_QUANTITY").value;
		var prodCustLength = document.getElementById("PROD_CUSTOM_LENGTH").value.replace(',','.');
		var prodCustWidth = document.getElementById("PROD_CUSTOM_WIDTH").value.replace(',','.');
		var productName = '';
		var productKey = '';
		
		var responseText = '';
		
		var productSalePrice = 0;
		var productPrice = 0;
		var totalPrice = 0;
		
		switch (prodGlass) {
			case '1':
				prodGlassName = ' AR';
				break;
			case '2':
				prodGlassName = ' VG';
				break;
			case '3':
				prodGlassName = ' RW';
				prodGlass = 1;
				break;
			case '4':
				prodGlassName = ' GGWW';
				prodGlass = 1;
				break;
			default:
				prodGlassName = '';
				break;
		}
		
		if (prodQuantity != "" && IsNumeric(prodQuantity) == true) {
			if (prodCustLength > 0 && prodCustWidth > 0) {
				productName = document.getElementById("NEW_PROD_NAME").value+' L:'+prodCustLength+' B:'+prodCustWidth+prodGlassName;
				productKey = iKey + prodCustLength + prodCustWidth + prodGlass;
			} else {
				document.getElementById('PROD_ERROR_TR').style.display = '';
				document.getElementById('PROD_ERROR').innerHTML = 'De lengte of breedte is niet ingevuld..';
				document.getElementById('PROD_PRICE').innerHTML = '0,00';
				document.getElementById('PROD_TOTAL').innerHTML = '0,00';
				return false;
			}
			
			responseText = getProductPrice(productKey, productName);
			
			productSalePrice = (parseFloat(responseText.replace(',', '.')));
			
			if( productSalePrice == 0 ) {
				//Find regular size switcht L B..
				productName = document.getElementById("NEW_PROD_NAME").value+' L:'+prodCustWidth+' B:'+prodCustLength+prodGlassName;
				productKey = iKey + prodCustWidth + prodCustLength + prodGlass;
				
				responseText = getProductPrice(productKey, productName);
				
				productSalePrice = (parseFloat(responseText.replace(',', '.')));
			}
			
			if( productSalePrice == 0 ) {
				//Find special price..
				productName = document.getElementById("NEW_PROD_NAME").value+' L:'+prodCustLength+' B:'+prodCustWidth+prodGlassName;
				productKey = iKey.substr(0,2) + 1 + iKey.substr(2,99) + prodGlass;
				
				responseText = getProductPrice(productKey, productName);
				
				productSalePrice = (parseFloat(responseText.replace(',', '.')));
			}
			
			document.getElementById('PROD_CODE').innerHTML = productKey;
			
			if (productSalePrice > 0) {	
    			if (((prodCustLength <= 60) && (prodCustWidth <= 80)) || ((prodCustLength <= 80) && (prodCustWidth <= 60))) {
                    document.getElementById('PROD_ERROR_TR').style.display = 'none';
                    document.getElementById('PROD_ERROR').innerHTML = '';
    			} else {
    			    
    				document.getElementById('PROD_ERROR_TR').style.display = '';
    				document.getElementById('PROD_ERROR').innerHTML = 'Maten boven 60x80 kunnen extra worden belast met verzendkosten';
    				//document.getElementById(oProductTargetId).innerHTML = '0,00';
    				//document.getElementById(oTotalTargetId).innerHTML = '0,00';
    				
    				//return false;
    				
    				productSalePrice = productSalePrice + 7.50;
    			}
			
				productPrice = productSalePrice * configMargin;
				totalPrice = productPrice * prodQuantity;
				
				productPrice = productPrice.toFixed(2)+'';
				totalPrice   = totalPrice.toFixed(2)+'';
				productSalePrice = productSalePrice.toFixed(2)+'';
				
				if (!productPrice.split('.')[1] === undefined || productPrice.split('.')[1].length == 0)
					productPrice = productPrice + '.00';
				else if (productPrice.split('.')[1].length == 1)
					productPrice = productPrice + '0';
				
				if (!productSalePrice.split('.')[1] === undefined || productSalePrice.split('.')[1].length == 0)
					productSalePrice = productSalePrice + '.00';
				else if (productSalePrice.split('.')[1].length == 1)
					productSalePrice = productSalePrice + '0';
				
				if (!totalPrice.split('.')[1] === undefined || totalPrice.split('.')[1].length == 0)
					totalPrice = totalPrice + '.00';
				else if (totalPrice.split('.')[1].length == 1)
					totalPrice = totalPrice + '0';
				
				document.getElementById(oProductTargetId).innerHTML = productPrice.replace('.',',');
				document.getElementById(oTotalTargetId).innerHTML = totalPrice.replace('.',',');
				
				document.getElementById("NEW_PROD_CODE").value = productKey;
				document.getElementById("PROD_NAME").innerHTML = productName;
				document.getElementById("PROD_SALE_PRICE").value = productSalePrice;
			} else {
				document.getElementById('PROD_ERROR_TR').style.display = '';
				document.getElementById('PROD_ERROR').innerHTML = 'Er is geen prijs gevonden, u kunt contact opnemen met <a href="mail:info@fotolijsten.nl">info@fotolijsten.nl</a>.';
				document.getElementById(oProductTargetId).innerHTML = '0,00';
				document.getElementById(oTotalTargetId).innerHTML = '0,00';
			}
		}
	} else {
		var prodPrice = document.getElementById("PROD_PRICE").innerHTML;
		var prodQuantity = document.getElementById("PROD_QUANTITY").value;
		totalPrice = parseFloat((prodPrice.replace(',','.'))) * prodQuantity;
		var mPrice = '' + totalPrice.toFixed(2);
		document.getElementById(oTotalTargetId).innerHTML = mPrice.replace('.',',');
	}
}

//  check for valid numeric strings	
function IsNumeric(strString) {
	var strValidChars = "0123456789.-";
	var strChar;
	var blnResult = true;
	
	if (strString.length == 0) return false;
	
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
   {
   strChar = strString.charAt(i);
   if (strValidChars.indexOf(strChar) == -1)
      {
      blnResult = false;
      }
   }
	return blnResult;
}

function toggleSize(iKey) {
	document.getElementById('PROD_PRICE').innerHTML                  = '0,00';
	document.getElementById('PROD_TOTAL').innerHTML                  = '0,00';
	document.getElementById('PROD_ERROR_TR').style.display           = 'none';
	document.getElementById('PROD_ERROR').innerHTML                  = '';
	calculateProduct(iKey, 'PROD_PRICE', 'PROD_TOTAL');
//	if (document.getElementById('PROD_STANDARD_WIDTH').style.display == 'none') {
//		document.getElementById('PROD_CUSTOM_WIDTH').style.display    = 'none';
//		document.getElementById('PROD_CUSTOM_WIDTH').value            = ''
//		document.getElementById('PROD_CUSTOM_LENGTH').style.display   = 'none';
//		document.getElementById('PROD_CUSTOM_LENGTH').value           = '';
//		document.getElementById('PROD_STANDARD_WIDTH').style.display  = '';
//		document.getElementById('PROD_STANDARD_LENGTH').style.display = '';
//		document.getElementById('toggle_size_link').innerHTML         = 'Speciale maten..';
//	} else {
//		document.getElementById('PROD_CUSTOM_WIDTH').style.display    = '';
//		document.getElementById('PROD_CUSTOM_LENGTH').style.display   = '';
//		document.getElementById('PROD_CUSTOM_LENGTH').value           = document.getElementById('PROD_STANDARD_LENGTH').value;
//		document.getElementById('PROD_CUSTOM_WIDTH').value            = document.getElementById('PROD_STANDARD_WIDTH').value;
//		document.getElementById('PROD_STANDARD_WIDTH').style.display  = 'none';
//		document.getElementById('PROD_STANDARD_WIDTH').selectedIndex  = 0;
//		document.getElementById('PROD_STANDARD_LENGTH').style.display = 'none';
//		document.getElementById('PROD_STANDARD_LENGTH').selectedIndex = 0;
//		document.getElementById('toggle_size_link').innerHTML         = 'Standaard maten..';
//		
//		calculateProduct(iKey, 'PROD_PRICE', 'PROD_TOTAL');
//	}
}

function submitDetailPage(redirect, noCalculate, origProdCode) {
	var startDate;
	var newDate;

	if (redirect) {
		window.document.location = window.document.location+'&sc=true';
		return;
	}
	
	if( noCalculate == false && window.document.getElementById('PROD_SALE_PRICE').value.replace(',','.') == 0 ) {
		calculateProduct(origProdCode, 'PROD_PRICE', 'PROD_TOTAL', noCalculate);
	}
	
	startDate = new Date();
	
	while( noCalculate == false && window.document.getElementById('PROD_SALE_PRICE').value.replace(',','.') == 0 )
	{
		newDate = new Date();
		if( startDate.getTime() + 15000 > newDate.getTime() || document.getElementById('PROD_ERROR_TR').style.display == '' )
		{
			alert("Er kon geen prijs worden opgehaald.");
			return;
		}
	}
	
	if (window.document.getElementById('PROD_SALE_PRICE').value.replace(',','.') > 0) {
		if (window.document.getElementById('PROD_QUANTITY').value > 0) {
			var url = window.document.location+'&sc=addProduct'+'&productname='+window.document.getElementById('PROD_NAME').innerHTML+'&productprice='+window.document.getElementById('PROD_SALE_PRICE').value+'&productamount='+window.document.getElementById('PROD_QUANTITY').value+'&newproductcode='+window.document.getElementById('PROD_CODE').innerHTML;
			var xmlhttp;
			var async = false;
			if (window.ActiveXObject)   // ActiveX version
			{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");  // Internet Explorer 
			}
			else if (window.XMLHttpRequest)     // Object of the current windows
			{ 
				xmlhttp = new XMLHttpRequest();     // Firefox, Safari, ...
			} 
			
			xmlhttp.open("GET", url, async);
			xmlhttp.send(url);
			
			alert('Product is toegevoegd! Klik op orders en vervolgens actieve bestelling om deze te bekijken.');
		} else
			alert('Vul eerst het aantal producten in.');
	} else
		alert('Haal eerst de correcte prijs op.');
}

function processCart (action, extraTextCount) {
	var orderComment = '';
	
	if ($('commentText').value != '')
		orderComment = '&orderComment=' + escape($('commentText').value);
		
    orderComment = orderComment.replace(/%0A/g, '\%20');
		
	if (action == 'save') {
		url = document.location+''; document.location=url.split('&sc=')[0]+'&sc=saveCart'+orderComment
	} else if (action == 'settle') {
		url = document.location+''; document.location=url.split('&sc=')[0]+'&sc=settleCart'+orderComment
	}
}

function saveOrderComment (comment, sessionId) {
	 var url = "call/saveOrderComment.php?orderComment=" + escape(comment) + "&sessionId=" + sessionId;
   var xmlhttp;
   var async = false;
   if (window.ActiveXObject)   // ActiveX version
   {
     xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");  // Internet Explorer 
   }
   else if (window.XMLHttpRequest)     // Object of the current windows
   { 
     xmlhttp = new XMLHttpRequest();     // Firefox, Safari, ...
   } 

   xmlhttp.open("GET", url, async);
   xmlhttp.send(url);
}

function saveProductAmount(productAmount, productCode, productName, sessionId) {
	 var url = "call/saveProductAmount.php?productAmount=" + escape(productAmount) + "&productCode=" + escape(productCode) + "&productName=" + escape(productName) + "&sessionId=" + sessionId;
   var xmlhttp;
   var async = false;
   if (window.ActiveXObject)   // ActiveX version
   {
     xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");  // Internet Explorer 
   }
   else if (window.XMLHttpRequest)     // Object of the current windows
   { 
     xmlhttp = new XMLHttpRequest();     // Firefox, Safari, ...
   } 

   xmlhttp.open("GET", url, async);
   xmlhttp.send(url);
   return true;
}

function saveOrderComment (comment, sessionId) {
	 var url = "call/saveOrderComment.php?orderComment=" + escape(comment) + "&sessionId=" + sessionId;
   var xmlhttp;
   var async = false;
   if (window.ActiveXObject)   // ActiveX version
   {
     xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");  // Internet Explorer 
   }
   else if (window.XMLHttpRequest)     // Object of the current windows
   { 
     xmlhttp = new XMLHttpRequest();     // Firefox, Safari, ...
   } 

   xmlhttp.open("GET", url, async);
   xmlhttp.send(url);
}

