/*********** FORM VERIFICATION ******************/

function validateForm() {
	
	//alert('validate form called');
	
	// loop through all of the elements of class required, and see if they have a value or a selected index.
	
		//alert('Called validateForm()');
	
	  var error = false;
	
	  var all = document.all ? document.all : document.getElementsByTagName('*');
						  
	  for (var e = 0; e < all.length; e++) {
													 
			if (all[e].className.search('required') != -1 ) {
			
				//alert('Found Required: '+all[e].id+'  Type: '+all[e].type+' \nValue: '+all[e].value+' SelectedIndex: '+all[e].selectedIndex);
				
				if (all[e].value == "" || all[e].selectedIndex == 0) {
				
					//alert('Found Unfilled Required');
				
					var bad_id = all[e].id;
					
					if (bad_id == 'email' || bad_id == 'first_name' || bad_id == 'last_name' || bad_id == 'company') {
					
						var highlight_id = 'p_'+bad_id;
					
						error = true;
					
						}	
					else {
					
						var id_array = bad_id.split('_');
						
						var highlight_id = 'p_id_'+id_array[1];
					
						error = true;
					
						}
						
					if (error == true) {
					
						document.getElementById(highlight_id).style.color='#FF0000';
						document.getElementById(bad_id).style.borderColor ='#FF0000';
						document.getElementById(bad_id).style.borderStyle ='solid';
						document.getElementById(bad_id).style.borderWidth ='1px';
						
						}
									
					}  // end if not filled
	
				} // end if required
			  
			}  // end for loop through all.length
			
	// test to ensure that email is a good value.
	
	var extraMessage = "";
	
	var submitted_email = document.getElementById('email').value;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

	if (!filter.test(submitted_email)) {
		
		error = true;
		extraMessage+= '<br><br>Please provide us with a valid email address - for example, <em>name@domain.com</em> - so that we can contact you by email if necessary.';
		document.getElementById('p_email').style.color='#FF0000';
		document.getElementById('email').style.borderColor ='#FF0000';
		document.getElementById('email').style.borderStyle ='solid';
		document.getElementById('email').style.borderWidth ='1px';

		
		}	
	// test to ensure that the phone number is valid.
	//  this is tricky - as phone numbers can be in one or more input fields, and they won't carry the id of phone.
	//  thus, what we have to do is look at all of the p tags in the form object, and then go through each, looking for PHONE in its inner HTML.  Ugh.
	
	var myForm = document.getElementById('user_form');
	
	//alert('myForm: '+myForm);
	
	var paraArray = myForm.getElementsByTagName('p');
	
	//alert('paraArray: '+paraArray);
	//alert('paraArray.length: '+paraArray.length);
	
	for (x=0; x < paraArray.length; x++) {
		
		var pContents = paraArray[x].innerHTML;
		
		//alert('pContents: '+pContents);
		
		if (pContents.search('phone') != -1 || pContents.search('Phone') != -1) {
			
			var paraId = paraArray[x].id;
			var paraIdArray = paraId.split('_');
			var isId = paraIdArray[2]; // why 2?  paragraph tags have the id in the form of 'p_id_XX' where XX is the number.   Arrays are 0 based!
			var answerId = 'answer_'+isId;
			
			var testPhone = document.getElementById(answerId).value;

                        testPhone = testPhone.replace('(','');
                        testPhone = testPhone.replace(')','');
                        testPhone = testPhone.replace('-','');
			
			//alert('test phone value: '+testPhone);
			
			if (checkInternationalPhone(testPhone)==false) {
				
				error = true;
				extraMessage+= '<br><br>Please provide us with a valid phone number in the form ### ###-####.';
				
				document.getElementById(paraId).style.color='#FF0000';
				document.getElementById(answerId).style.borderColor ='#FF0000';
				document.getElementById(answerId).style.borderStyle ='solid';
				document.getElementById(answerId).style.borderWidth ='1px';

				
				
				}
			
			}
		
		}
		
	var yoyo_value = "";
	
	var first_name = document.getElementById('first_name').value;
	var last_name = document.getElementById('last_name').value;
	var total_name = last_name+first_name;
	
	for(x=0; x < total_name.length; x++) {
	
		char_code = total_name.charCodeAt(x);
		
		//alert('name part code: '+char_code);
		
		yoyo_value = yoyo_value + char_code;
	
		} 
	
	yoyo_value = parseInt(yoyo_value / total_name.length);
	
	//alert('Yoyo value: '+yoyo_value);
	
	document.getElementById('yoyo').value = yoyo_value;


	if (error == true) {
	
		document.getElementById('form_error').innerHTML = "You did not fill out one or more required fields on this form.  So that we may serve you better, please fill out the indicated fields before submitting this form."+extraMessage;
		document.getElementById('form_error').style.display='block';
		
			return false;

		}


	}  // end function




// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
	var bracket=3
	strPhone=trim(strPhone)
	if(strPhone.indexOf("+")>1) return false
	if(strPhone.indexOf("-")!=-1)bracket=bracket+1
	if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
	var brchr=strPhone.indexOf("(")
	if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
	if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}


	


