// BEGIN AJAX
function getHTTPObject() {
	var xmlhttp;
	if(window.XMLHttpRequest){
		xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject){
    		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    		if (!xmlhttp){
        		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    		}
    
	}
  	return xmlhttp;
}



/*
	Funtion Name=requestInfo 
	Param = url >> Url to call : id = Passing div id for multiple use ~ as a seprator for eg. div1~div2 :
	redirectPage >> if you like to redirect to other page once the event success then 
	the response text = 1 and the redirectPage not left empty
*/

function requestInfo(url,id,redirectPage) {
	var http = getHTTPObject(); // We create the HTTP Object      
	var temp=new Array();
	http.open("GET", url, true);
	http.onreadystatechange = function() {
		if (http.readyState == 4) {
			if(http.status == 200) {
				var results = http.responseText;
				if(redirectPage == "" || results != "1") {
					var temp = id.split("~"); // To display on multiple div 
					//alert(temp.length);
					var r = results.split("~"); // To display multiple data into the div 
					//alert(temp.length);
					if(temp.length>1) {
						for(i=0;i<temp.length;i++) {	
							//alert(temp[i]);						
							document.getElementById(temp[i]).innerHTML=r[i];
						}
					} else {
						document.getElementById(id).innerHTML = results;
					}	
				}else{
					//alert(results);
					window.location.href=redirectPage;			
				}
			} 
  		}
	};
	http.send(null);
}

//--------------------------------------------------------------------------------

function RequestInfoToFormElements(url,id,redirectPage,myform) {
	var http = getHTTPObject(); // We create the HTTP Object 	
	var temp=new Array();
	http.open("GET", url, true);
	http.onreadystatechange = function() {
		if (http.readyState == 4) {
			if(http.status == 200) {
				var results = http.responseText;
				if(redirectPage == "" || results != "1") {
					var temp = id.split("~"); // To display on multiple div alert(temp.length);					
					var r = results.split("~"); // To display multiple data into the div 					
					if(temp.length>1) {
						for(i=0;i<temp.length;i++) {	
					                //alert(temp[i]+"="+r[i]);
							//document.getElementById(temp[i]).value=r[i];
							if(document.forms[myform].elements[temp[i]]){
								document.forms[myform].elements[temp[i]].value=r[i];								
							}							
						}
					} else {
						//document.getElementById(id).value = results;
						document.forms[myform].elements[id].value=results;
					}	
				}else{
					//alert(results);
					window.location.href=redirectPage;			
				}
			} 
  		}
	};
	http.send(null);
}
/*
	FrunMyPHP(url,id) Me permite mandar a correr algun programita en el servidor
        y si id en != null entonces asigno el resultado al value de elelmento.
	Ejemplo:
--------------------
	<form name="myForm">
	Name: <input type="text"
	onkeyup="runMyPHP('time.php?a=1&b=2','time');" name="username" />
	Time: <input type="text" name="time" id="time" />
	</form>
---------------------
	time.php
<?php
$a = $_GET["a"];
$b = $_GET["b"];
$c = $a + $b;
$tiempo = "The Number is =".$c;
echo $tiempo;
?>
---------------------
*/

function runMyPHP(url,id) {
	var http = getHTTPObject(); // We create the HTTP Object  
	var temp=new Array();
	http.open("GET", url, true);  
	http.onreadystatechange = function() {
		if (http.readyState == 4) {
			if(http.status == 200) {
				var results = http.responseText;
				//alert(results);				
				if(id != null) {				
					document.getElementById(id).value = results;
				}				
			} 
  		}
	};
	http.send(null);
}
// END AJAX

/*
	Function Name= emptyValidation
	Desc = This function is used to validation for the empty field 
	Param fieldList = This arguments set as a string varialble. you just need to supply the textbox name
	if the textbox is multiple then supply with ~ separator for eg. username~password
*/
function emptyValidation(fieldList) {
		
		var field=new Array();
		field=fieldList.split("~");
		var counter=0;
		for(i=0;i<field.length;i++) {
			if(document.getElementById(field[i]).value=="") {
				document.getElementById(field[i]).style.backgroundColor="#FF0000";
				counter++;
			} else {
				document.getElementById(field[i]).style.backgroundColor="#FFFFFF";	
			}
		}
		if(counter>0) {
				alert("The Field mark as red could not left empty");
				return false;
				
		}  else {
			return true;
		}
		
}



// CART AREA

function save_data_cart(cart_id) {
	
	var regex=/^\d+$/;  // Regular expecion for numbers.
	
	var product=document.getElementById("product").value;
	var quantityNatural=document.getElementById("quantityNatural").value;
	var quantityPVC=document.getElementById("quantityPVC").value;
	
	var checkValidation = emptyValidation('quantityNatural~quantityPVC');

	if (!regex.test(quantityNatural) || !regex.test(quantityPVC)) checkValidation= false;

	if(checkValidation==true) {
		requestInfo('showTableCart.php?mode=save_new'+'&product='+product+'&quantityNatural='+quantityNatural+'&quantityPVC='+quantityPVC+'&cart_id='+cart_id,'showTable','');
	
	
	} else {
		alert("..please use only numerics on the edit fields");
	}

}

function update_data_cart(cart_id) {

	var regex=/^\d+$/;  // Regular expecion for numbers.
			
	var excel_id=document.getElementById("tag_excel_id").value;
	var quantityNatural=document.getElementById("quantityNatural").value;
	var quantityPVC=document.getElementById("quantityPVC").value;

	var checkValidation = emptyValidation('quantityNatural~quantityPVC');

	if (!regex.test(quantityNatural) || !regex.test(quantityPVC)) checkValidation= false;
 
	if(checkValidation==true) {
		requestInfo('showTableCart.php?mode=update_data'+'&excel_id='+excel_id+'&quantityNatural='+quantityNatural+'&quantityPVC='+quantityPVC+'&cart_id='+cart_id,'showTable','');

	
	} else {
		alert("..please use only numerics on the edit fields");
	}
	
}
	
	
function confirmLink(theLink)
{
   
	var is_confirmed = confirm('Are you sure to delete this record?\n\nThis will permanently delete the Record!');
	if (is_confirmed) {
		theLink.href += '';
	}

	return is_confirmed;
}
// END CART

// IMAGES AREA
// IMAGE ON DEMAND

function mydoTooltip(evt,image,texto,curBgColor,curFontColor) {
	if (!tooltip) return;
	if (t1) clearTimeout(t1);	if (t2) clearTimeout(t2);
	tipOn = true;
	// set colors if included in messages array	
	if(!curBgColor) curBgColor = tipBgColor;
	if(!curFontColor) curFontColor = tipFontColor;
	
	if (ie4||ie5||ns5) {
		var tip = startStr + image + midStr + '<span style="font-family:' + tipFontFamily + '; font-size:' + tipFontSize + '; color:' + curFontColor + ';">' + texto + '</span>' + endStr;
		tipcss.backgroundColor = curBgColor;
	 	tooltip.innerHTML = tip;
	}
	if (!tipFollowMouse) positionTip(evt);
	else t1=setTimeout("tipcss.visibility='visible'",100);
}




