// function for emptying out a dynamic div - usually: target=loadDIV
function emptyDiv(target){
	 document.getElementById(target).innerHTML = '';
}
// JAH DELETE function
function confirmJah(itemName,jahURL,jahDIV){
	var agree=confirm("Are you sure you want to delete "+itemName+"?");
		if (agree){
			
			jah(jahURL,jahDIV,'Processing...');
			//emptyDiv(itemID);
			// document.getElementById(itemID).innerHTML = ''
		}
		else
			return false;
}
// DELETE confirm
function confirmDelete(itemName) {
	return confirm("Are you sure you want to remove "+itemName+"?")
}

// dynamic publishing/loading of info 
function jah(jahURL,jahDIV,loadMsg) {
	//	GET, POST, PUT, and DELETE
    // native XMLHttpRequest object
	var itemDiv = '#' + jahDIV;
 	$(itemDiv).empty().append(loadMsg);
      //document.getElementById(jahDIV).innerHTML = loadMsg;
 	
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = function() {jahDone(jahDIV);};
        req.open('GET', jahURL, true);
        req.send(null);
		
    // IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
		
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function() {jahDone(jahDIV);};
            req.open('GET', jahURL, true);
            req.send();
			 
        }
    }
}    
function jahDone(jahDIV) {
    // only if req is "loaded"
	var itemDiv = '#' + jahDIV;
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            results = req.responseText;
           //document.getElementById(jahDIV).innerHTML = results;
		   $(itemDiv).empty().append(results);
			
        } else {
            //document.getElementById(jahDIV).innerHTML="jah error:\n" + req.statusText;
			$(itemDiv).empty().append("posting error:\n" + req.statusText);
        }
    }
}


//This function sets the value of the given text field
function setTxtField(txtField,val){
	document.getElementById(txtField).value=val;
	return false}

//This function checks the required field and makes sure that user input some data in those and it is not just empty spaces
function CheckReqField(FormID){
 var blnReturn = true;
 var alertMsg = '';
 var thisForm=document.getElementById(FormID);
 for (var i=0;i<thisForm.length;i++){
   var strTemp = Trim(thisForm.elements[i].value);
   if (strTemp == ''){
    blnReturn = false;
	alertMsg='Please fill out all fields before submitting the form.';
   }
   if (blnReturn == true && thisForm.elements[i].name == 'userEmail'){
	    blnReturn = checkEmail(thisForm.elements[i].value);
		alertMsg='Please enter a valid email address.';
   }
 }
 
 if (blnReturn){
  thisForm.submit();
 }
 else{
  alert(alertMsg);
 }
}
//This function takes out all the spaces from a string from both sides and the middle and returns the result.
//It is used in conjunction with the above funtion to make sure the user simply didn't put it spaces in the input text field
function Trim(strInput){
 var strTrimmmed = '';
 for (var i = 0;i<strInput.length; i++){
  if (strInput.charCodeAt(i)!=32){
   strTrimmmed += strInput[i];
  }
 }
 return strTrimmmed;
}


function checkEmail(emailField){
	//emailField =document.getElementById(emailField);
	regexPattern=/^[a-zA-Z_0-9-'\+~]+(\.[a-zA-Z_0-9-'\+~]+)*@([a-zA-Z_0-9-]+\.)+[a-zA-Z]{2,7}$/;
	return (regexPattern.test(emailField));
	
}

