function focusFirstElement(){
	if (document.developer_upload){
		document.developer_upload.first_name.focus();
	}
}
function validate(form){

	if(validateForm(form)){
		form.submit();
	}
}
function oldEnough(form, message){
	//must be 13 years old in order to submit games.	
	var dateEntered = new Date(form.year.value, form.month.value, form.day.value);
	var overDate = new Date();
	
	//needs better calculation of leap years. also buggy, off by one month.
	overDate.setDate(overDate.getDate( ) - 13*365);
	if (daysBetween(dateEntered, overDate) > 0){
		return true;
	} else {
		alert (message);
		return false;
	}
}

function daysBetween(date1, date2) {
    var DSTAdjust = 0;
    // constants used for our calculations below
    oneMinute = 1000 * 60;
    var oneDay = oneMinute * 60 * 24;
    // equalize times in case date objects have them
    date1.setHours(0);
    date1.setMinutes(0);
    date1.setSeconds(0);
    date2.setHours(0);
    date2.setMinutes(0);
    date2.setSeconds(0);
    // take care of spans across Daylight Saving Time changes
    if (date2 > date1) {
        DSTAdjust = 
            (date2.getTimezoneOffset( ) - date1.getTimezoneOffset( )) * oneMinute;
    } else {
        DSTAdjust = 
            (date1.getTimezoneOffset( ) - date2.getTimezoneOffset( )) * oneMinute;    
    }
    
    //var diff = Math.abs(date2.getTime( ) - date1.getTime( )) - DSTAdjust;
    var diff = date2.getTime( ) - date1.getTime( ) - DSTAdjust;
    
    return Math.ceil(diff/oneDay);
}
function checkSuffix(gameName, suffix){
	var sub = gameName.length;
	if(gameName.substring(sub-4, sub)==suffix){
		return true;
	} else {
		return false;
	}
}
function confirmGameFileType(elem, message){
	if(checkSuffix(elem.value, ".swf") || checkSuffix(elem.value, ".dcr") || checkSuffix(elem.value, ".rar") || checkSuffix(elem.value, ".zip")){ 
		return true;
	} else {
	    alert(message);
        elem.focus( );
    	elem.select( );
   	}
}
function confirmImageFileType(elem, message){
	//return true;
	if(elem.value=="" || checkSuffix(elem.value, ".jpg")){
		return true;
	} else {
		alert(message);
        elem.focus( );
    	elem.select( );
	}

}
function validateForm(form) {
    if (isNotEmpty(form.first_name, "Please fill in your first name.")) {
        if (isNotEmpty(form.last_name, "Please fill in your last name.")) {
            if (isNotEmpty(form.e_mail, "Please fill in your e-mail address.")) {
                if (isEMailAddr(form.e_mail)) {
                	 if (isNotEmpty(form.game_title, "Please fill in a game title.")) {
                	 	if (isNotEmpty(form.width, "Please fill in a game width.")) {
                	 		if (isNumber(form.width, "Enter only numbers into the width field.")) {
                	 			if (isNotEmpty(form.height, "Please fill in a game height.")) {                   	 			           	 				
			                		if (isNumber(form.height, "Enter only numbers into the height field.")) {
			                			if (isNotEmpty(form.game_filename, "Please select a game file to upload.")) {
			                				if (confirmGameFileType(form.game_filename, "Please select a game file ending in .swf, .dcr, .rar or .zip")){
			                					if(confirmImageFileType(form.game_image, "Please select a thumbnail file ending in .jpg")){
					                				if(isChecked(form.terms, "Please agree to our Terms & Conditions.")){             					
					                					return true;
					                				}
				                				}
			                				}
			           					}
									}	
								}			           
							}	
           				}		            
	                }
				}	               
            }
        }
    }
    return false;
}

// validates that the field value string has one or more characters in it
function isNotEmpty(elem, message) {
	//alert("elem.value is: " + elem.value);
    var str = elem.value;
    if(str == null || str.length == 0) {
        alert(message);
        elem.focus( );
    	elem.select( );
        
        return false;
    } else {

        return true;
    }
}
   
// validates that the entry is a positive or negative number
function isNumber(elem, message) {
    var str = elem.value;
    var oneDecimal = false;
    var oneChar = 0;
    // make sure value hasn't cast to a number data type
    str = str.toString( );
    for (var i = 0; i < str.length; i++) {
        oneChar = str.charAt(i).charCodeAt(0);
        
        /*positive integers only!
        // OK for minus sign as first character
        if (oneChar =  = 45) {
            if (i =  = 0) {
                continue;
            } else {
                alert("Only the first character may be a minus sign.");
                return false;
            }
        }
        // OK for one decimal point
        if (oneChar =  = 46) {
            if (!oneDecimal) {
                oneDecimal = true;
                continue;
            } else {
                alert("Only one decimal is allowed in a number.");
                return false;
            }
        }
        */
        // characters outside of 0 through 9 not OK
        if (oneChar < 48 || oneChar > 57) {
            alert(message);
            return false;
        }
    }
    return true;
}
   
// validates that the entry is 16 characters long
function isLen16(elem) {
    var str = elem.value;
    if (str.length != 16) {
        alert("Entry does not contain the required 16 characters.");
        return false;
    } else {
        return true;
    }
}
   
// validates that the entry is formatted as an email address
function isEMailAddr(elem) {
    var str = elem.value;
    str = str.toLowerCase( );
    if (str.indexOf("@") > 1) {
        var addr = str.substring(0, str.indexOf("@"));
        var domain = str.substring(str.indexOf("@") + 1, str.length);
        // at least one top level domain required
        if (domain.indexOf(".") == -1) {
            alert("Verify the domain portion of the email address.");
            elem.focus( );
			elem.select( );
            
            return false;
        }
        // parse address portion first, character by character
        for (var i = 0; i < addr.length; i++) {
            oneChar = addr.charAt(i).charCodeAt(0);
            // dot or hyphen not allowed in first position; dot in last
            if ((i == 0 && (oneChar == 45 || oneChar == 46))  || 
                (i == addr.length - 1 && oneChar == 46)) {
                alert("Verify the user name portion of the email address.");
            	elem.focus( );
				elem.select( );
            
                return false;
            }
            // acceptable characters (- . _ 0-9 a-z)
            if (oneChar == 45 || oneChar == 46 || oneChar == 95 || 
                (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
                continue;
            } else {
                alert("Verify the user name portion of the email address.");
                elem.focus( );
				elem.select( );
            
                return false;
            }
        }
        for (i = 0; i < domain.length; i++) {
            oneChar = domain.charAt(i).charCodeAt(0);
            if ((i == 0 && (oneChar == 45 || oneChar == 46)) || 
                ((i == domain.length - 1  || i == domain.length - 2) && oneChar == 46)) {
                alert("Verify the domain portion of the email address.");
                elem.focus( );
				elem.select( );
            
                return false;
            }
            if (oneChar == 45 || oneChar == 46 || oneChar == 95 || 
                (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
                continue;
            } else {
                alert("Verify the domain portion of the email address.");
                elem.focus( );
				elem.select( );
            
                return false;
            }
        }
        return true;
    }
    alert("The email address may not be formatted correctly. Please verify.");
    elem.focus( );
	elem.select( );
            
    return false;
}

// validate that the user made a selection other than default
function isChecked(checkbox, message) {
    if (!checkbox.checked) {
        alert(message);
        return false;
    } else {
        return true;
    }
}

					