Event.observe(window, 'load', function() {
  	Event.observe('signupform', 'submit', processOrder);
});


function processOrder(eventid){
	eventid.stop();
	
	var reason = "";
	reason += validateEmail($('email'));
	reason += validatePhone($('phone'));
	reason += validateEmpty($('firstname'), "First Name");
	reason += validateEmpty($('lastname'), "Last Name");
	reason += validateEmpty($('address'), "Address");
	reason += validateEmpty($('city'), "City");
	reason += validateEmpty($('state'), "State");
	reason += validateEmpty($('zipcode'), "Zipcode");
	reason += validateEmpty($('phone'), "Phone Number");
	reason += validateEmpty($('typeofproducts'), "Products/Services Being Sold");
	reason += validateEmpty($('transaction_size'), "Estimiated Average Transaction Size");	
	reason += validateEmpty($('monthly_volume'), "Estimated Monthly Volume");	
	reason += validateEmpty($('taxid'), "Federal Tax ID or SSN");	
	reason += validateEmpty($('businesstype'), "Business Type");	
	reason += validateEmpty($('passcode1'), "Password");	
	reason += validateEmpty($('passcode2'), "Confirm Password");	
	reason += validatePassword($('passcode1'),$('passcode2'),"Your Passwords Do NOT Match");	
	
	if (reason != "") {
		alert("Some fields need correction:\n" + reason);
	} else {
		var myNewAjax = new Ajax.Request('/signup/process.php', { method: 'post', parameters: Form.serialize('signupform')
			, onSuccess: function (originalResponse) {
			JSONresponse = originalResponse.responseJSON;
			location.href=JSONresponse.statusURL;
			}
		});	

	}
}

function validateEmpty(fld,theinfo) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "--  " + theinfo + " is empty.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validatePassword(pw1,pw2,theinfo) {
    var error = "";
 
    if (pw1.value != pw2.value) {
        pw1.style.background = 'Yellow'; 
        pw2.style.background = 'Yellow'; 
        error = "--  " + theinfo + "\n"
    } else {
        pw1.style.background = 'White';
        pw2.style.background = 'White';
    }
    return error;  
}

function trim(s) {
  return s.replace(/^\s+|\s+$/, '');

}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "--  Email address is empty.\n";
    } else if (!emailFilter.test(tfld)) {
        fld.style.background = 'Yellow';
        error = "-- Email address is invalid.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "-- Email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    
   if (fld.value == "") {
        error = "--  Phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "-- Phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "-- Phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    }
    return error;
}
