////////////////////////////////////////////////////////////////////////////////////
// 						       JAVASCRIPT FUNCTIONS								  //
////////////////////////////////////////////////////////////////////////////////////
// trim: Gets rid of leading/trailing spaces.
function trim(strToTrim) {
	return (strToTrim.replace(/^\s+|\s+$/g, ''));
}

function send_email(strAddress) {
	location.href = "mai" + "lto:" + strAddress + "@" + "txtmy" + "meds.com";
}

////////////////////////////////////////////////////////////////////////////////////
// 						 AJAX LOADING AND FORM SUBMISSION 						  //
////////////////////////////////////////////////////////////////////////////////////

// getxmlhttp: Creates and returns an XMLHttp Object (cross-browser).
function getxmlhttp (){
	//Create a boolean variable to check for a valid Microsoft active x instance.
	var xmlhttp = false;
	//Check if we are using internet explorer.
	try {
		//If the javascript version is greater than 5.
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		//If not, then use the older active x object.
		try {
		//If we are using internet explorer.
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			//Else we must be using a non-internet explorer browser.
			xmlhttp = false;
		}
	}
	// If not using IE, create a
	// JavaScript instance of the object.
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

// makerequest: Loads a page into an element (typically a div).
function makerequest(serverPage, objID) {
	xmlhttp = getxmlhttp ();
	var obj = document.getElementById(objID);
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			obj.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
}

// processajax: Function to process an XMLHttpRequest.  Used to submit information to a page.
function processajax (serverPage, obj, getOrPost, str){
	//Get an XMLHttpRequest object for use.
	xmlhttp = getxmlhttp ();
	if (getOrPost == "get"){
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				obj.innerHTML = xmlhttp.responseText;
			}
		}
	xmlhttp.send(null);
	} else {
		xmlhttp.open("POST", serverPage, true);
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				obj.innerHTML = xmlhttp.responseText;
			}
		}
	xmlhttp.send(str);
	}
}

// getformvalues: Returns all objects in a form's values into a string.
// 				  We validate the form in another function before this is called.
function getformvalues (fobj){
	var strRet = "";
	var val;
	//Run through a list of all objects contained within the form.
	for(var i = 0; i < fobj.elements.length; i++){
		strRet += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
	}
	//Then return the string values.
	return strRet;
}

function submitform (theform, serverPage, objID){
	var strFormValues = getformvalues(theform);
	//If the validation is ok.
	obj = document.getElementById(objID);
	processajax (serverPage, obj, "post", strFormValues);
}

////////////////////////////////////////////////////////////////////////////////////
// 						    JAVASCRIPT FORM VALIDATION							  //
////////////////////////////////////////////////////////////////////////////////////

// validatePhone: Validates phone number.
function validatePhone(strNumber, msgobjID) {
	// checks to see if phone num valid, and updates debug message accordingly.
	var strMsg = "";
	
	var strStripped = strNumber.replace(/[\(\)\.\-\ ]/g, '');
	//strip out acceptable non-numeric characters
	if (isNaN(parseInt(strStripped))) {
	   if(strNumber.indexOf('-', 0) > 0){
			strMsg += "Please remove any (-) dashes! ";
		} else if(strNumber.indexOf('.', 0) > 0){
			strMsg += "Please remove any (.) periods! ";
		} else {
			strMsg += "Please remove any invalid characters. ";
		}
	}
	if(strStripped.length > 10){
		strMsg += "Must be only 10 digits! ";
	}
	if(strNumber.length < 10){
		strMsg += "Must be at least 10 digits! ";
	}
	document.getElementById(msgobjID).innerHTML = strMsg;
	if(strMsg == ""){ 
		return true;
		} else { 
		return false;
	}
}



// validatePills: Validates pill text message.
function validatePills(strPills, msgobjID)  {
	var strMsg = "";
	if(strPills.length > 50){
		strMsg += "Must be less than 50 characters! ";
	}
	if(strPills.length == 0){
		strMsg += "No pills entered! ";
	}
	document.getElementById(msgobjID).innerHTML = strMsg;
	if(strMsg == ""){ 
		return true;
		} else { 
		return false;
	}
}

// validateReminderForm: Returns whether a form is ready for submission, and returns T/F.
function validateReminderForm(formID) {
	var blnRet = true;
	blnRet = validatePhone(getElementByID('phone').value, 'msgPhone');
	blnRet = validatePills(getElementByID('pills').value, 'msgPills');
	return Boolean(blnRet);
}

