bgcolor_error = "#f2ddd6";

function pretty_validate(formid){

	var message="";

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

	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(f_rel!=null && f_rel.indexOf("zipcode")>-1 && isZip(f_rel)==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("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){
				//	message = message + "" + linkarr[i].getAttribute("title") + " does not match your <b>" + document.getElementById(c_name).getAttribute("title") + "</b>.<br>";
				//	linkarr[i].style.backgroundColor="#cbecd4";
				// }
				
			}
			
			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("validdate")>-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(message!=""){
	
		alert("Please check the following fields:\n" + message);
		
		/*
		
	
		message = message + "<div style=\"text-align: right;\"><img src=\"images_chock/ok_button.gif\" vspace=\"8\"></div><div style=\"font-style: italic; text-align: center;\">* All Missing or Incorrect Fields Are Highlighted In Green.</div>";
	
		var checkTB = document.getElementById("TB_OVERLAY");
		if (checkTB == null){
			var newP = document.createElement("div");
			newP.setAttribute("id", "TB_OVERLAY");
			var p2 = document.getElementsByTagName('body')[0];
			p2.appendChild(newP);
		}
		delete checkTB;

		document.getElementById("contest_state").style.display = "none";
	
		tb = document.getElementById("TB_OVERLAY");
		tb.style.display = "block";
		tb.setAttribute("onclick", "closewindow();");
		tb.innerHTML = "<div id=\"TB_WINDOW\" onclick=\"closewindow();\"><div id=\"TB_WINDOWBAR\"></div><div id=\"TB_CONTENT\">" + message  + "</div><div id=\"TB_WINDOWFOOT\"></div></div><div id=\"TB_CLOSE\" onclick=\"closewindow();\">&nbsp;</div>";

		*/

		return false;
		
	}
	
	return true;
	
}

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;

	if(sText.length!=5){
		return false;
	}

 
   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){
	if(sText.length<7){
		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=2010;

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