bgcolor_error = "#f2ddd6";

function pretty_validate(formid){

	var message="";

	// var formobj = document.getElementById(formid);
	// var linkarr = document.getElementsByTagName("input");  

	var linkarr = formid.elements;

	for (var i = 0; i < linkarr.length; i++) {  

            // f_class = linkarr[i].getAttribute("class");
			f_class = linkarr[i].className;
			f_rel = linkarr[i].getAttribute("rel");
            f_title = linkarr[i].getAttribute("title");
			f_value = linkarr[i].value;

			if(f_class!=null && (f_class.indexOf("validate")>-1) ){
				linkarr[i].style.backgroundColor="#fff";
				
				if(formid.check_phone.value == 1){
					formid.phone_1.style.backgroundColor = "#fff";
					formid.phone_2.style.backgroundColor = "#fff";
					formid.phone_3.style.backgroundColor = "#fff";
				}
			}

			if(f_rel!=null && f_rel.indexOf("zipcode")>-1 && isZip(f_value)==false){
				message = message + "The " + f_title + " field is not a valid zipcode\n";
				linkarr[i].style.backgroundColor=bgcolor_error;
			}

			else if(f_rel!=null && f_rel.indexOf("phone")>-1){
				tmpval = clean_phone(f_value); 
				linkarr[i].value = tmpval;
				if(isPhone(tmpval)==false){
					message = message + "The " + f_title + " field is not a valid phone number\n";
					linkarr[i].style.backgroundColor=bgcolor_error;
				}
			}
			
			else if(f_rel!=null && f_rel.indexOf("email")>-1 && validemail(f_value)==false){
				message = message + "The " + f_title + " field is not a valid email address\n";
				linkarr[i].style.backgroundColor=bgcolor_error;
			}
			
			else if(f_rel!=null && f_rel.indexOf("rfp")>-1 && validrfp(f_value)==false){
				message = message + "Please choose a valid file format of:  DOC, XLS, or PDF.\n";
				linkarr[i].style.backgroundColor=bgcolor_error;
			}

			else if(f_rel!=null && f_rel.indexOf("numeric")>-1 && (isInteger(f_value)==false || f_value=="") ){
				message = message + "The " + f_title + " field must be a valid number\n";
				linkarr[i].style.backgroundColor=bgcolor_error;
			}
			
			else if(f_rel!=null && f_rel.indexOf("confirm")>-1){
				
				f_name = linkarr[i].getAttribute("id");
				c_name = f_name.substr(1, f_name.length - 1);

				if( (linkarr[i].value != document.getElementById(c_name).value) || linkarr[i].value == ""){
				message = message + "" + linkarr[i].getAttribute("title") + " does not match your " + document.getElementById(c_name).getAttribute("title") + ".\n";
				linkarr[i].style.backgroundColor = bgcolor_error;
				}
				
			}
			
			else if(f_rel!=null && f_rel.indexOf("notempty")>-1 && f_value==""){
				message = message + "" + f_title + " is a required field.\n";
				linkarr[i].style.backgroundColor=bgcolor_error;
			}

			else if(f_rel!=null && f_rel.indexOf("valid_date")>-1 && isDate(f_value)==false){
				message = message + "" + f_title + " is not a valid date, the date format should be : mm/dd/yyyy\n";
				linkarr[i].style.backgroundColor=bgcolor_error;
			}

           	
			

	}
	
	if(formid.check_phone.value == 1){
	if( ! ( (formid.phone_1.value.length == 3 && isInteger(formid.phone_1.value)) && (formid.phone_2.value.length == 3 && isInteger(formid.phone_2.value)) && (formid.phone_3.value.length == 4 && isInteger(formid.phone_3.value)) ) ){

		message = message + "Phone number is not a valid.\n";
		
		formid.phone_1.style.backgroundColor = bgcolor_error;
		formid.phone_2.style.backgroundColor = bgcolor_error;
		formid.phone_3.style.backgroundColor = bgcolor_error;
	}
	}
	
	
	if(message!=""){
		alert("Please check the following fields:\n" + message);
		return false;
	}
	
	return true;
	
}

function validrfp(rfpv){
	
	rfp = rfpv.toLowerCase();
	
	if(rfp.indexOf(".doc") > 0){
		return true;
	}
	
	if(rfp.indexOf(".xls") > 0){
		return true;
	}
	
	if(rfp.indexOf(".pdf") > 0){
		return true;
	}
	
	return false;
	
}

function closewindow(){
	mobj = document.getElementById('TB_OVERLAY');
	mobj.style.display = "none";
	mobj.innerHTML = "";
	
	document.getElementById("contest_state").style.display = "inline";
} 

function validemail(emailform){
		if(emailform.length>5){
			if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailform)) {
				return true;
			} else {
				return false;			
			}
		} else {
			return false;		
		}
	return false;
}

function isZip(sText){
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

	znum = clean_phone(sText);
	
	if(znum.length!=5){
		return false;
	} else {
		return true;
	}

   /*
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1){
         IsNumber = false;
         }
      }

   return IsNumber;
   */
   
}

function isPhone(sText){
	pnum = clean_phone(sText);
	
	if(pnum.length<10){
		return false;
	}
   return true;
}

function auto_jump(formobj, num, nextobj){
	if(formobj.value.length == num){
		nextobj.focus();
	}
}


/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

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 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++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}


function clean_phone(sText){
		var ValidChars = "0123456789";
		var IsNumber=true;
		var Char;
		var theNumber = "";

		for (i = 0; i < sText.length && IsNumber == true; i++) { 
			Char = sText.charAt(i); 
			if (ValidChars.indexOf(Char) != -1) {
				theNumber = theNumber + "" + Char;
			}
		}
		
		return theNumber;
}

