
//JavaScript functions for validating form

function validData(){
	
	var error_string = "";
	
	//check textarea
	if (document.stories.mystory.value == '') {
		error_string += "Your Story.\n";

		document.getElementById("mystory").style.color="#FF0000";

	}else{	

		document.getElementById("mystory").style.color="#000";

	}



	// check the name fields
	if ((document.stories.first_name.value == '')|| (document.stories.first_name.value.length <=1)){
		error_string += "First Name.\n";
		document.getElementById("first").style.color="#FF0000";
	}else{	
		document.getElementById("first").style.color="#000";
	}
	
	if ((document.stories.last_name.value == '')|| (document.stories.last_name.value.length <=1)){
		error_string += "Last Name.\n";
		document.getElementById("last").style.color="#FF0000";
	}else{	
		document.getElementById("last").style.color="#000";
	}
	
	//check address field
	if ((document.stories.address1.value =='')||(document.stories.address1.value.length <=1)){
		error_string += "Address.\n";
		document.getElementById("addr").style.color="#FF0000";
	}else{	
		document.getElementById("addr").style.color="#000";		
	}
	
	//check city field
	if ((document.stories.city.value =='')||(document.stories.city.value.length <=1)){
		error_string += "City.\n";
		document.getElementById("cty").style.color="#FF0000";
	}else{	
		document.getElementById("cty").style.color="#000";		
	}
	
	/*check state information
	if(document.stories.state.selectedIndex <0){
		error_string += "State.\n";
		document.getElementById("ste").style.color="#FF0000";
	}else{	
		document.getElementById("ste").style.color="#000";		
	}
*/	
	/* check zipcode
	
		if (checkZip(document.stories.zip.value)== true){
			error_string += "Zipcode.\n";
		document.getElementById("zipC").style.color="#FF0000";
	}else{	
		document.getElementById("zipC").style.color="#000";			
		}
*/		

	//check country field
	if ((document.stories.country.value =='')||(document.stories.country.value.length <=1)){
		error_string += "Country.\n";
		document.getElementById("country").style.color="#FF0000";
	}else{	
		document.getElementById("country").style.color="#000";		
	}

	
	//check phone
		
			if ((document.stories.phone.value != "") && (checkPhone(document.stories.phone.value) == true)){
			error_string += "Phone Number may not be valid.\n";
		document.getElementById("phoneNum").style.color="#FF0000";
	}else{	
		document.getElementById("phoneNum").style.color="#000";			
		}	
	
	//check e-mail
		
		if (checkEmail(document.stories.email.value) == true){
			error_string += "E-mail address.\n";	
		document.getElementById("eml").style.color="#FF0000";
	}else{	
		document.getElementById("eml").style.color="#000";			
		}
	
	//check website
	
		if ((document.stories.website.value != "") && (isURL(document.stories.website.value) == true)){
			error_string +="Website address.\n";
		document.getElementById("ws").style.color="#FF0000";
	}else{	
		document.getElementById("ws").style.color="#000";			
		}
	
	if(error_string ==""){
		return true;
	}
	else{
		error_string = "Please double-check the following entries: \n" +error_string;
		alert(error_string);
		return false;
	}
}
	
	
	
function checkEmail(addy){

	var emailFilter = /^[a-z][\w\.]*@[\w\.]+\.[a-z]{2,3}/i;
	var illegalChars = /[\(\)\<\>\,\;\:\\\"[\]]/;
	
	
	if((addy == "") || (!(emailFilter.test(addy))) || (addy.match(illegalChars))){
		return true;
	}
}

function checkZip(zipcode){
	var i;
	var legalChars = "0123456789()-+ " 
	
	if((zipcode=="") || (!(zipcode.length<=10))|| (!(zipcode.length>=5))){
		return true;
	}
	
	for (i =0; i <= zipcode.length -1; i++) {
		if (legalChars.indexOf(zipcode.charAt(i)) == -1) {
		
		return true;
		}
	}
}
		
function checkPhone(numString){
	
	var i;
	var regex=/^s*\d{3}\s*?-?\d{4}$/;	
	var regexA=/^\(?\d{3}\)?-?\s*\d{3}\s*?-?\d{4}$/;

	
	if((!(regex.test(numString))) && (!(regexA.test(numString))) ){
		return true;
	}	
	
	
}



function isURL (url) {
 	 var urlPattern = /^(?:(?:ftp|https?):\/\/)?(?:[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\.)+(?:com|edu|biz|org|gov|int|info|mil|net|name|museum|coop|aero|[a-z][a-z])\b(?:\d+)?(?:\/[^;"'<>()\[\]{}\s\x7f-\xff]*(?:[.,?]+[^;"'<>()\[\]{}\s\x7f-\xff]+)*)?/i;

	 if (!(urlPattern.test(url))){
		 return true;
	 }
}

