var req;
var x = -1;
function Initialize(){
    try{
        req=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e){
        try{
            req=new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(oc){
            req=null;
        }
    }
    if(!req&&typeof XMLHttpRequest!="undefined"){
        req=new XMLHttpRequest();
    }
}
function SendQuery(){
    Initialize();

	var comment = trimAll(document.getElementById("comment").value);
	var name = trimAll(document.getElementById("name").value);
	var phone = "";
	if (document.getElementById("phone")){
		var phone = trimAll(document.getElementById("phone").value);
	}
	var email = trimAll(document.getElementById("email").value);
    var url="save_data.php?comment="+comment+"&name="+name+"&phone="+phone+"&email="+email;

	if (comment == "") {
		alert("Please write some feedback before sending it");
		return false;
	}

	if(req!=null){
		req.onreadystatechange = Process;
		req.open("GET", url, true);
		req.send(null);
	}
}
function SendWholesale(){
    Initialize();

	var comment = trimAll(document.getElementById("comment").value);
	var name = trimAll(document.getElementById("name").value);
	var company_name = trimAll(document.getElementById("company_name").value);
	var phone = "";
	if (document.getElementById("phone")){
		var phone = trimAll(document.getElementById("phone").value);
	}
	var email = trimAll(document.getElementById("email").value);
	var url="save_data.php?mailto=" + escape("sales@puremobile.com");
	url = url + "&subject=" + escape("Wholesale Request");
	url = url + "&comment=" + escape(comment);
	url = url + "&name=" + escape(name);
	url = url + "&company_name=" + escape(company_name);
	url = url + "&phone=" + escape(phone);
	url = url + "&email=" + escape(email);

	if (company_name == "") {
		alert("No business name?");
		return false;
	}
	if (name == "") {
		alert("We just need a contact name..");
		return false;
	}
	if (phone == "") {
		alert("Please specify a phone number for us to contact you");
		return false;
	}
	if (email == "") {
		alert("Your email address will not be sold or shared. We need it to send you information regarding the wholesale and volume discounts program");
		return false;
	}
	if (comment == "") {
		alert("No comments? Please tell us a little bit about yourself and your business.");
		return false;
	}
	

	if(req!=null){
		req.onreadystatechange = Process;
		req.open("GET", url, true);
		req.send(null);
	}
}

function SendRMA(){
    	Initialize();

	var comment = trimAll(document.getElementById("comment").value);
	var items = trimAll(document.getElementById("items").value);
	var order = trimAll(document.getElementById("order").value);
	var name = trimAll(document.getElementById("name").value);
	var company_name = trimAll(document.getElementById("company_name").value);
	var phone = "";
	if (document.getElementById("phone")){
		var phone = trimAll(document.getElementById("phone").value);
	}
	var email = trimAll(document.getElementById("email").value);
	var url="save_data.php?mailto=" + escape("rma@puremobile.com,support@puremobile.com");
	url = url + "&subject=" + escape("SO #") + order +" - " + escape("RMA Authorization Request");
	url = url + "&comment=" + escape(comment);
	url = url + "&items=" + escape(items);
	url = url + "&order=" + escape(order);
	url = url + "&name=" + escape(name);
	url = url + "&company_name=" + escape(company_name);
	url = url + "&phone=" + escape(phone);
	url = url + "&email=" + escape(email);

	if (name == "") {
		alert("We need a contact name..");
		return false;
	}
	if (phone == "") {
		alert("Please specify a phone number..");
		return false;
	}
	if (email == "") {
		alert("Your email address will not be sold or shared. We need it to send you information regarding your RMA Request");
		return false;
	}
	if (order == "") {
		alert("The order or invoice number is required");
		return false;
	}
	if (items == "") {
		alert("Please list the item(s) that you are requesting and RMA Authorization for");
		return false;
	}
	if (comment == "") {
		alert("Please let us know the reason for the RMA Authorization Request.");
		return false;
	}
	if (document.getElementById("terms_and_conditions").checked == false){
		alert("When you purchased items from PureMobile, you agreed to our Terms and Conditions, Return Policy and DOA Policy. Please check the checkbox to confirm again that you have read our Return Policy and DOA Policy.");
		return false;
	}
	

	if(req!=null){
		req.onreadystatechange = Process;
		req.open("GET", url, true);
		req.send(null);

	}
}


function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function Process(){
    if (req.readyState == 4){
    // only if "OK"
        if (req.status == 200){
				HideDiv("comment_box");
                ShowDiv("success_box");
        }
        else{
			HideDiv("comment_box");
			ShowDiv("fail_box");
        }
    }
}

function ShowDiv(divid){
	document.getElementById(divid).style.display="block";
}

function HideDiv(divid){
	document.getElementById(divid).style.display="none";
}

