String.prototype.trim = function () 
{  
	return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
};

/* create a new XMLHTTPRequest */
var xmlHttp = false;

try 
{
	xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} 
catch (e) 
{
	try 
	{
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e2) {
		xmlHttp = false;
	}
}

if (!xmlHttp && typeof XMLHttpRequest != 'undefined') 
{
	xmlHttp = new XMLHttpRequest();
}

function AJAX_vServerResponse()
{
	if (xmlHttp.readyState == 4) 
	{
		var response = xmlHttp.responseText.split('|||');	
		var result = new String(response[0]);
		result = result.trim();
		if (result == 'true')
		{
			window.location.reload();
		}
		else if (result == 'alert')
		{
			var message = new String(response[1]);
			message = message.trim();
			alert(message);
		}
		else
			for (var i=1; i<response.length;i+=2) 
			{ 
				var element = new String(response[i]);
				var value = new String(response[i+1]);
				element = element.trim();
				value = value.trim();
				try 
				{
			    	document.getElementById(element).innerHTML = value;
			    }
			    catch (e)
			    {
			    	document.getElementById(element).value = value;			    
			    }
			}				
	}
}

function AJAX_vRequest(thisaction)
{
	var url;    
	if (thisaction == 'act_make_contact')
	{
		//* get the value of the element that we want to submit    
		var user = document.getElementById("usercontact").value;
		var email = document.getElementById("useremail").value;
		var phone = document.getElementById("userphone").value;	
		// we expect at least 2 arguments for these actions		
		var thisdealer = arguments[1]; 		
		var thisstockcode = arguments[2];
		//* build the url with the element and value
		url = "ajaxactions.php?ms="+ new Date().getTime() + "&action=" + escape(thisaction) + "&code="+ escape(thisstockcode)+ "&dealer="+ escape(thisdealer) + "&user="+ escape(user)+ "&phone="+ escape(phone)+ "&email="+ escape(email);    	
	}
	else if ((thisaction == 'act_update_stock') || (thisaction == 'act_change_detected'))
	{
		var thisdealer = arguments[1]; 		
		var thisstockcode = arguments[2];
		var thiskeyword = arguments[3];		
		var thiselement = arguments[4];		
		var value = document.getElementById(thiselement).value;
		//* build the url with the element and value
		url = "ajaxactions.php?ms="+ new Date().getTime() + "&action="+escape(thisaction)+"&code="+ escape(thisstockcode)+ "&dealer="+ escape(thisdealer)+ "&keyword="+escape(thiskeyword)+"&value="+escape(value);    	
	}
	else if (thisaction == 'act_newspaper_send')
	{
		// we expect at least 2 arguments for these actions		
		var thisdealer = arguments[1]; 		
		var thisstockcode = arguments[2];
		var thisemail = arguments[3];		
		var email = document.getElementById(thisemail).value;
		//* build the url with the element and value
		url = "ajaxactions.php?ms="+ new Date().getTime() + "&action=" + escape(thisaction) + "&code="+ escape(thisstockcode)+ "&dealer="+ escape(thisdealer)+ "&email="+escape(email);    
	
	}
	else if (thisaction == 'act_reminder')
    {
        var thisemail = arguments[1];
        var email = document.getElementById(thisemail).value;
        //* change button next
		document.getElementById('btn_reminder').value = 'Please wait...';        
	  	//* build the url with the element and value
	  	url = "ajaxactions.php?ms="+ new Date().getTime() + "&action=" + escape(thisaction) + "&email="+ escape(email);    
    }
    else if (thisaction == 'act_login')
    {
        var thislogin = arguments[1];
        var thispassword = arguments[2];            
    	//* get the value of the element that we want to submit    
		var login = document.getElementById(thislogin).value;
		var password = document.getElementById(thispassword).value;
        //* change button next
		document.getElementById('btn_access').value = 'Validating...';
	  	//* build the url with the element and value
	  	url = "ajaxactions.php?ms="+ new Date().getTime() + "&action=" + escape(thisaction) + "&username=" + escape(login) + "&password" + "=" + escape(password);
    }    
	else if ((thisaction == 'act_build_photo') || (thisaction == 'act_logout'))
	{
		//* build the url
		url = "ajaxactions.php?ms="+ new Date().getTime() + "&action=" + escape(thisaction);    	
	}
	else
	{
		// we expect at least 2 arguments for these actions		
		var thisdealer = arguments[1]; 		
		var thisstockcode = arguments[2];
		//* build the url
		url = "ajaxactions.php?ms="+ new Date().getTime() + "&action=" + escape(thisaction) + "&code="+ escape(thisstockcode)+ "&dealer="+ escape(thisdealer);    
	}		
 	//* open connection
  	xmlHttp.open("GET",url,true); 
  	//* setup the callback function
  	xmlHttp.onreadystatechange = AJAX_vServerResponse;
  	//* send request
  	xmlHttp.send(null);
}
