/*
Forms Js Script.
Author:Micah Blu

Mainly form validation
	*Empty fields
	*Email validation
	*password confirmation (expects fields named as 'pass' and 'confirm_pass')
*/
function forms(){
	var aTags = document.getElementsByTagName("a");
	for(var a=0; a < aTags.length; a++){
		//alert(aTags[a]);
			if(aTags[a].className == "del"){
				aTags[a].onclick = function(){
					if(confirm('Are you sure you want to delete this?')){ return true;}
					else { return false; }
				}
		}
	}
	validate();
}
function validate(){
	var forms = document.getElementsByTagName("form");
	var msg = "*Some fields were not completed."; //error msg defaults on empty fields msg
	var error_msg = document.getElementById("error_msg");
		
	for(var i=0;i<forms.length;i++){
		var fields = forms[i].length; //minus 1 for the submit button // we do f-1 becuase the loop starts f as 1 not 0!
		//function to trigger calender pop up on an onfocus form event
		for(var j=0; j<fields; j++){
			//alert(forms[f-1][j].name);
			//alert(forms[f-1][j].className);
			if(forms[i][j].className == "calfield"){
				forms[i][j].onclick = function(){
					popup("includes/calendar.php?el="+this.name, "Publish Calendar", 235, 215, "no", "no");
				}
			}
		}
		forms[i].onsubmit = function(){
			var valid = true;
			var fields = this.length;
			
			for(var j=0; j<fields; j++){
				//alert(this[j].name)
				if(this[j].value == "" && this[j].className == "reqd"){
					this[j].style.backgroundColor = "rgb(255,255,180)";
					valid = false;
				}
				else{
					//for some reason was changing a fieldset's bg to white so make sure element is a form field.
					if(this[j].type != undefined) this[j].style.backgroundColor = "#ffffff";
					//check the email
					if(this[j].name == "email"){
						if(echeck(this[j].value) == false){
							valid = false;
							this[j].style.backgroundColor = "rgb(255,255,180)";
							this[j].focus();
							msg = "Email was invalid";
						}
					}
				}
			}//ends for loop
			
			if(this.pass){
				if(this.pass.name){
					var pass = this.pass;	
					var confirm_pass = this.confirm_pass;	
					if(pass.value != confirm_pass.value){
						msg = "Passwords do not match";
						pass.value=''; pass.style.backgroundColor = "rgb(255,255,180)";
						confirm_pass.value=''; confirm_pass.style.backgroundColor = "rgb(255,255,180)";
						pass.focus();
						valid = false;
					}
				}
			}
			if(!valid) error_msg.innerHTML="<p style='width:100%; padding:5px; border-top:1px solid #cccccc; border-bottom:1px solid #cccccc; background:#ffffcc'>"+msg+"</p>";
			return valid;
		}
	}	
}
function echeck(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(str) == false) return false
	/*
	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;				
}

function validateEmail(field){
	var emailField = field
	if ((emailField.value==null)||(emailField.value=="")){
		emailField.style.backgroundColor = "rgb(255,255,180)";
		emailField.value="Email is required.";
		emailField.focus();
		return false;
	}
	if (echeck(emailField.value)==false){
		emailField.value="Email was Invalid.";
		emailField.style.backgroundColor = "rgb(255,255,180)";
		emailID.focus();
		return false;
	}
	return true;
}
function numVal(field){
	//var PhoneMsgEl = document.getElementById("email_phone_msg");
	if(field.name == "hphone" || field.name == "cphone"){
		//check length 10 digits
		if(field.value.length != 10){
			PhoneMsgEl.style.color = "red";
			PhoneMsgEl.innerHTML = "&nbsp;Phone number(s) must contain 10 digits";
			return false;
		}else{ PhoneMsgEl.innerHTML = "";}
		
		if(/^\d+$/.test(field.value) == false){
			PhoneMsgEl.style.color = "red";
			PhoneMsgEl.innerHTML = "&nbsp;Phone number(s) must only contain numbers";
			return false;
		}else{ PhoneMsgEl.innerHTML = "";}
	}
	return true;
}

function passVal(){
	//start join page's password confirmation
	var pass = document.getElementById("pass");
	var confirm_pass = document.getElementById("confirm_pass");
	var msgEl = document.getElementById("pass_msg");
	var valid = true;
	var illegalChars = /\W/; // allow only letters and numbers

	//first check to see if they match
	if(pass.value != confirm_pass.value){
		msgEl.style.color = "red";
		op_msg = "&nbsp;Passwords do not Match";
		msgEl.innerHTML = op_msg;
		return false;
	}
	//check if they're empty
	if(pass.value == '' || confirm_pass == ''){
		return false;
	}
	if(pass.value.length < 6){
		msgEl.style.color = "red";
		op_msg = "&nbsp;Password is too short";
		msgEl.innerHTML = op_msg;
		return false;
	}
	if(illegalChars.test(pass.value) == true){
		msgEl.style.color = "red";
		op_msg = "&nbsp;Password can only contain numbers and letters";
		msgEl.innerHTML = op_msg;
		return false;
	}	
	//else clear messages and return true
	msgEl.style.color = "#666666";
	msgEl.innerHTML = "&nbsp;Ok";
	return true;
}

function submitForm(){
 var cform = document["contactForm"];
 var error_msg = document.getElementById("error_msg");
 var valid = true;
 //check for empty values
 if(cform["name"].value==""){ valid = false; cform["name"].style.backgroundColor = "rgb(255,255,180)"; }
 if(cform["email"].value==""){ valid = false; cform["email"].style.backgroundColor = "rgb(255,255,180)"; }
 if(cform["message"].value==""){ valid = false; cform["message"].style.backgroundColor = "rgb(255,255,180)"; }

 //if valid is false at this point return with blank fields error message
 //prepare error message and display
 if(!valid){
   var style = "width:100%; padding:5px; border-top:1px solid #cccccc; border-bottom:1px solid #cccccc; background:#ffffcc";
   var msg = "Please fill all required fields";
   error_msg.innerHTML = "<p style='"+style+"'>"+msg+"</p>";
 }
 //move on to email validation *only run this if the email is not empty
 if(cform["email"].value != ""){
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(cform["email"].value) == false){ 
   	 valid = false; 
	 cform["email"].style.backgroundColor = "rgb(255,255,180)"; 
  	 var msg = "Please enter a valid email";
   	 error_msg.innerHTML = "<p style='"+style+"'>"+msg+"</p>";
   }
 }
 //finally if still valid send ajax request and submit form data
 if(valid){
   //prep vars
   var name = cform["name"].value;
   var email = cform["email"].value;
   var message = escape(cform["message"].value);
   
   //before sending let's disable the form
   cform["name"].disabled = true;
   cform["email"].disabled = true;
   cform["message"].disabled = true;
   document.getElementById("submit").disabled = true;
   
   var url= "wp-content/themes/etika-style/contact-proc.php?name="+name+"&email="+email+"&message="+message;
   var el = error_msg;
   ajaxGet(url,el);
 }
 return false;
}

function findPos(obj) {
	/*
	Nifty little method will get the "real" top and left
	positions of any element on the browser screen
	*/
	var curleft = curtop = 0;
	
	if (obj.offsetParent) {
		
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;      
		} while (obj = obj.offsetParent);
	
	}
	return [curleft,curtop];
}

function submit_free_ch(_form){
	
	if(_form.name.value == "" || _form.email.value == ""){
		alert("Please fill out name and email to proceed");
		return false;
	}
	if(!echeck(_form.email.value)){
		alert("Please enter a valid email");	
		return false;
	}else{
		//all is good!
		closeDialog();
		var url = _form.action + "?name="+_form.name.value+"&email="+_form.email.value;
		window.open(url, "Lessons From Prison Chapter 21", "", "");
		return false;
	}
}

function promptInfo(){
	var dbox = document.createElement("div");
	dbox.id = "dialogbox";
	dbox.style.position = "absolute";
	dbox.style.top = "0px";
	dbox.style.width = "100%";
	dbox.style.padding = "10px";
	dbox.style.background = "#f2f2f2";
	
	var inner = "<div style='width:300px; margin:0px auto'><p>Please remit the following info to access the free chapter</p>\n";
	
	inner += "<form action='wp-content/themes/etika-style/process_free_ch.php' name='userInfoForm' method='post' onsubmit='return submit_free_ch(this)'>\n";
	inner += "<p><label>Name</label> <input type='text' name='name' value='' /></p> \n";
	inner += "<p><label>Email</label> <input type='text' name='email' value='' /></p> \n";
	inner += "<p> <input type='submit' name='submit' value='Get Free Chapter' /> or <a href='Javascript: closeDialog()'>Close this</a></p> \n";
	inner += "</form></div>\n";
	
	dbox.innerHTML = inner;
	
	document.body.appendChild(dbox);
}

function closeDialog(){
	var dbox = document.getElementById("dialogbox");
	
	document.body.removeChild(dbox);
}