function check_username(obj){
	if(obj.value){
		$.ajax({
			url:'http://www.baytechwebdesign.com/include/verify.php?verify='+obj.value,
			success:function(msg){
				if(msg=='OK'){
					$('#verify_info').html('');
				}else{
					$('#verify_info').html('<font color="red">' + msg + '</font>');
				}
			}
		});
	}else{
		$('#verify_info').html('<font color="red">Verification Code is required.</font>');
	}
}

function check_submit(e)
{
	var msg;
	var textAll = "An Error occurred. Please make sure that you have completed this form.";
	var textname="";
	var textlastname="";
	var textcompany="";
	var textphone="";
	var textemail="";
	var texttimeline="";
	var textbudget="";
	var textverify="";
	var verify_info = document.getElementById("verify_info");
	var verify_img = document.getElementById("verify_img");
	var now=new Date();
	
	
	if ((e.fre_firstname.value == null) || (e.fre_firstname.value == "") || isblank(e.fre_firstname.value)) { textname = "First Name is Required.\n";}
	
	if ((e.fre_lastname.value == null) || (e.fre_lastname.value == "") || isblank(e.fre_lastname.value)) { textlastname = "Last Name is Required.\n";}
	
	if ((e.fre_companyname.value == null) || (e.fre_companyname.value == "") || isblank(e.fre_companyname.value)) { textcompany = "Please fill None if no company.\n";}

	if ((e.fre_phonenumber.value == null) || (e.fre_phonenumber.value == "") || isblank(e.fre_phonenumber.value)) { textphone = "Phone Number is Required.\n";}
	
	
	if (textphone=="") {
	  if (!checkphone(e.fre_phonenumber.value)) { 
	     textphone = "An Error occurred. Please enter a correct phone number. For example: 1-555-555-1212 \n";
	  }
	}
	
	if ((e.fre_email.value == null) || (e.fre_email.value == "") || isblank(e.fre_email.value)) { textemail = "E-mail address is Required.\n";}
	
	if (textemail=="") {
	  if (!CheckEmail(e.fre_email.value)) { 
	     textemail = "An Error occurred. Please enter a correct email address. For example: someone@email.com \n";
	  }
	}
	
	if ((e.project_timeline.value == null) || (e.project_timeline.value == "") || isblank(e.project_timeline.value)) { texttimeline = "Estimated Launch Date is Required.\n";}
	
	if ((e.estimated_budget.value == null) || (e.estimated_budget.value == "") || isblank(e.estimated_budget.value)) { textbudget = "Estimated Budget is Required.\n";}
	
//	if ((e.verify.value == null) || (e.verify.value == "") || isblank(e.verify.value)) { textverify = "Verification Code is Required.\n";}
//		else{
////			alert(d.verify.value);return false;
//			var obj = document.getElementById("verify");
//			check_username(obj);
//		}
//	if(verify_info.innerHTML){
////		verify_img.src = "include/GD.php#"+now.getTime();
//		textverify = "Verification Code is Error.\n";
//	}
	

	if (e.fre_firstname.style) e.fre_firstname.style.backgroundColor = textname != "" ? '#FFCC66':'#FFFFFF';
	if (e.fre_lastname.style) e.fre_lastname.style.backgroundColor = textlastname != "" ? '#FFCC66':'#FFFFFF';
	if (e.fre_companyname.style) e.fre_companyname.style.backgroundColor = textcompany != "" ? '#FFCC66':'#FFFFFF';
	if (e.fre_phonenumber.style) e.fre_phonenumber.style.backgroundColor = textphone != "" ? '#FFCC66':'#FFFFFF';
	if (e.fre_email.style) e.fre_email.style.backgroundColor = textemail != "" ? '#FFCC66':'#FFFFFF';
	if (e.project_timeline.style) e.project_timeline.style.backgroundColor = texttimeline != "" ? '#FFCC66':'#FFFFFF';
	if (e.estimated_budget.style) e.estimated_budget.style.backgroundColor = textbudget != "" ? '#FFCC66':'#FFFFFF';
//	if (e.verify.style) e.verify.style.backgroundColor = textverify != "" ? '#FFCC66':'#FFFFFF';
	
	if ((textname == "") && (textlastname == "") && (textcompany == "") && (textphone == "") && (textemail == "") && (texttimeline == "") && (textbudget == "")) { return true; }
	
	if (textname && textlastname && textcompany && textphone && textemail && texttimeline && textbudget) {
		msg = textAll;
	} else {
		msg = textname + textlastname + textcompany + textphone + textemail + texttimeline + textbudget;
	}		
	
	alert(msg);
	return false;
}


function isblank(s)
{
	for(var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

function checkphone(str)
{
    for( var i=0; i < str.length; i++) {
        var ch = str.substring( i, i+1 );
        if( (ch < "0" || ch > "9") && ch != "-" && ch != "." && ch != " " && ch != "(" && ch != ")" && ch != "x" && ch != "X" && ch != "+") 
            return false;              
    }    
    return true;
}

function CheckEmail(strEmail) {
	var i;
	var getChar;

	if ((Left(strEmail, 1) == "@") || (Right(strEmail, 1) == "@") )
		return false;
	
		getChar = 0;
	for (i = 0 ; i < strEmail.length ; i++ ) {
		if (strEmail.charAt(i)=='@'){
			getChar++;
		}
	}	    

	if ( getChar != 1 )
		return false;

	var email1;
	var email2;
	var temail;
	
	temail = strEmail.split('@')
	email1 = temail[0].replace(/\s/g, "");
	email2 = temail[1].replace(/\s/g, "");	
	
	var cemail;
	cemail = strEmail;
	cemail = cemail.replace(/\@/g, "");	
	cemail = cemail.replace(/\./g, "");	
	
	if (cemail.length == 0)
		return false;
	
	var allow;
	allow = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-";
	for(i = 0 ; i < cemail.length; i++ )
		if (allow.indexOf(cemail.charAt(i)) < 0 )
			return false;

	if (Left(email1, 1) == '.')
		return false;

	
	if (Left(email2, 1) == '.' || Right(email2, 1) == '.' || email2.replace(/\./g, "").length == email2.length)
		return false;

	if (email2.indexOf("..") >= 0 )
		return false;

	if (Left(email2, 1) == '-' || Right(email2, 1) == '-')
		return false;

	if (Left(email2, 1) == '_' || Right(email2, 1) == '_')
		return false;
	
	return true;
}

function Left( sourceStr, charIdx ) {
	if((sourceStr==null) || (sourceStr=="")) return "";
	return sourceStr.substring(0, charIdx);
}

function Right( sourceStr, charIdx ) {	
	if((sourceStr==null) || (sourceStr=="")) return "";
	return sourceStr.substring(sourceStr.length-charIdx, sourceStr.length);
}
