// JavaScript Document
// 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 checkForm(thisForm) {
	var theForm = document.forms[0];
	var alertText = "";
	var editForm = false;
	delete firstFocus;
	alertText = "The highlighted fields are missing some information.\nPlease click OK to enter some missing information.\n\nIf you still have problems,\nyou can email adoptions@huskyrescue.org.\n\nPhone numbers are formatted with Numbers, -, ().";


	for(i=0; i<theForm.elements.length; i++){ 
		//if (theForm.getElementById('Fence_Yard').options[theForm.getElement
		//if (theForm.elements[i].name == "Fence_Height" || theForm.elements[i].name == "Fence_Type"){
		//} else if (theForm.elements[i].name == "Pet_Put_To_Sleep_Reason") {
		//} else if (theForm.elements[i].name == "Train_Crate_Reason") {
		//} else if (theForm.elements[i].name == "Train_Basic_Obedience_Reason") {
		//}
		
		//ensure the number is formatted properly
		//if (theForm.elements[i].name == "Phone_Day" || theForm.elements[i].name == "Phone_Evening") {
		//	var Phone = theForm.elements[i].name;
		
		//	if (checkInternationalPhone(Phone.value)==false) {
		//		firstFocus = theForm.elements[i];
		//	}
		//}
	
		if (!theForm.elements[i].getAttribute('required')) {
				
			if (theForm.elements[i].name == "ApplicationID") {
				editForm = true;
			} 
			else if((theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea") && theForm.elements[i].value == ""){
				theForm.elements[i].className = "form_field_required";
				if (typeof(firstFocus) == "undefined") firstFocus = theForm.elements[i];
			}
			else if(theForm.elements[i].type == "checkbox"){
				theForm.elements[i].className = "form_field_required";
				if (typeof(firstFocus) == "undefined") firstFocus = theForm.elements[i];
			}
			else if(theForm.elements[i].type == "select-one"){			
				if (theForm.elements[i].options[theForm.elements[i].selectedIndex].text == 0) {
					theForm.elements[i].className = "form_field_required";
					if (typeof(firstFocus) == "undefined") firstFocus = theForm.elements[i];
				} else {
					theForm.elements[i].className = "form_field_notrequired";
				}
			} else{
				theForm.elements[i].className = "form_field_notrequired";
			}
			
		} else if (theForm.elements[i].getAttribute('required')) {
			if (theForm.elements[i].getAttribute('required') != "false") { 
				requiredField = eval(document.getElementsByName(theForm.elements[i].getAttribute('required')));
				// if (requiredField.options[requiredField.selectedIndex].value == "Y") alert ("awesome");
			}
		}
	}

	if (typeof(firstFocus) != "undefined" && editForm == false) {
		firstFocus.focus();
		alert(alertText);
	} else {
		theForm.submit();
	}
}

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);
}

function ValidateForm(){
	if (theForm.elements[i].name == "Phone_Day" || theForm.elements[i].name == "Phone_Evening") {
		var Phone = theForm.elements[i].name;

		if (checkInternationalPhone(Phone.value)==false) firstFocus = theForm.elements[i];
	}
 }
