////////////////////////////////////////////////////////////
//	AJAX JavaScript file for the Vitruvian Man
//	Built by Josh Axelman RankLab Interactive
//	For Recovery Advocates 8/16/2008
////////////////////////////////////////////////////////////
   
   var http_request = false;
   function makePOSTRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }
////////////////////////////////////////////////////////////
   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById('VMan').innerHTML = result; // set the vitrivian man content
		 } else {
            alert('There was a problem with the request.');
         }
      }
   }
   
////////////////////////////////////////////////////////////

	// This is the AJAX virtruvian function. I deprecated this by changing the name
	function vitruvianAJAX(id) {
		var poststr = "content_id=" + id; // create the post string
		makePOSTRequest('vitruvian.php', poststr); // make the request
	} // end function

////////////////////////////////////////////////////////////
	// this function is very similiar. We don't need to make an ajax call, the data is on the page
	function vitruvian(id) {
		// lets just the value of the Vman div to the value of the corresponding content
		document.getElementById('VMan').innerHTML = document.getElementById(id).innerHTML; 
	} // end function 


////////////////////////////////////////////////////////////
//	We need to clear the forms name values when on focus //

	function clearFocus(str){
		var field = document.getElementById(str);
		if (field.value == str) {
			field.value = '';
		}
	} // end function
////////////////////////////////////////////////////////////

	function setBlur(str){
		var field = document.getElementById(str);
		if (field.value == ''){
			field.value = str;
		}
	}




//*****************************************EMAIL VALIDATION*************************************************
	function JACheckEmail(str){// we must check to see if the email is formated properly.
			var at="@";
			var dot=".";
			var lat=str.indexOf(at);
			var lstr=str.length;
			var ldot=str.indexOf(dot);
			if (str.indexOf(at)==-1){
			   return false;
			}

			if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
			   return false;
			}

			if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
			   return false;
			}

			 if (str.indexOf(at,(lat+1))!=-1){
				return false;
			 }

			 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
				return false;
			 }

			 if (str.indexOf(dot,(lat+2))==-1){
				return false;
			 }
			
			 if (str.indexOf(" ")!=-1){
				return false;
			 }
			return true;					
	}
//*****************************************END EMAIL*********************************************************




//*****************************************FORM VALIDATION***************************************************
	function JACheckForm(){//validate the form submit based on name email or phone
			var name, email, phone, error, errorTxt;
			name = document.form1.Name.value;
			email = document.form1.Email.value;
			phone = document.form1.Phone.value;
			// initialize the error. for re-submition 
			document.getElementById("formerror").innerHTML ='';
			error = 0;
			errorTxt ='';

			if (name == ""){
				errorTxt = 'Name is a required field.<br />';
				error = 1;
			}
			

			
			
			if (email != "" && JACheckEmail(email)==false){
				errorTxt = errorTxt + 'A valid email address is required.<br />';
				error = 1;
			}

			if (email == ""){
				errorTxt = errorTxt + 'A valid email address is required.<br />';
				error = 1;
			}
			// if there was an error, print it out and don't post
			if (error == 1){
				document.getElementById("formerror").innerHTML = 'All fields require valid information.<br />';
				return false;
			}
		}


//*****************************************FORM VALIDATION***************************************************
	function JACheckFormMini(){//validate the form submit based on name email or phone
			var name, email, phone, error, errorTxt;
			name = document.form1.name.value;
			email = document.form1.email.value;
			
			// initialize the error. for re-submition 
			document.getElementById("nameerror").innerHTML ='Name<br /><input type="text" name="name" value="' + name + '" id="name" />';
			document.getElementById("emailerror").innerHTML ='Email<br /><input type="text" name="email" value="' + email + '" id="email" />';
			error = 0;

			if (name == ""){
				document.getElementById("nameerror").innerHTML ='Name: <strong>required</strong><br /><input type="text" name="name" id="name" />';
				error = 1;
			}

			
			if (email != "" && JACheckEmail(email)==false){
				document.getElementById("emailerror").innerHTML ='Email: <strong>valid email required</strong><br /><input type="text" name="email" id="email" />';
				error = 1;
			}

			if (email == ""){
				document.getElementById("emailerror").innerHTML ='Email: <strong>valid email required</strong><br /><input type="text" name="email" id="email" />';
				error = 1;
			}
			// if there was an error, print it out and don't post
			if (error == 1){
				return false;
			}
		}




//*****************************************FORM VALIDATION***************************************************
	function JACheckFormFriend(){//validate the form submit based on name email or phone
			var name, email, youremail,yourname, error, errorTxt;
			name = document.form1.name.value;
			email = document.form1.email.value;
			youremail = document.form1.youremail.value;
			yourname = document.form1.youremail.value;
			// initialize the error. for re-submition 
			document.getElementById("formerror").innerHTML ='';
			error = 0;
			errorTxt ='';

			if (name == ""){
				errorTxt = 'Name is a required field.<br />';
				error = 1;
			}

			
			if (yourname == ""){
				errorTxt = 'Name is a required field.<br />';
				error = 1;
			}
			

			
			if (email != "" && JACheckEmail(email)==false){
				errorTxt = errorTxt + 'A valid email address is required.<br />';
				error = 1;
			}

			if (email == ""){
				errorTxt = errorTxt + 'A valid email address is required.<br />';
				error = 1;
			}


			
			if (youremail != "" && JACheckEmail(youremail)==false){
				errorTxt = errorTxt + 'A valid email address is required.<br />';
				error = 1;
			}

			if (youremail == ""){
				errorTxt = errorTxt + 'A valid email address is required.<br />';
				error = 1;
			}

			
			// if there was an error, print it out and don't post
			if (error == 1){
				document.getElementById("formerror").innerHTML = 'All fields are required!';
				return false;
			}
		}

//*****************************************END FORM*************************************************************


///////////BOOKMARK ////////////////////


function bookmarksite(title,url){
if (window.sidebar) // firefox
	window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
} 
else if(document.all)// ie
	window.external.AddFavorite(url, title);
}


//////////////////////////////////////////