var myregvalid1 = 0;
var myregvalid2 = 0;
var myregvalid3 = 0;
var myregvalid4 = 0;

$(document).ready(function(){
						   
				
				$(".form-register").show();
				$(".check-email").hide();
				$("#btnSubmit").hide();
			

		
            $(".formContainer input[type=text]").focus(function(){
																

				if($(this).attr("id") =="txtEmailAddress"){
				$("#txtEmailAddressdiv").css("color","grey");
				$("#txtEmailAddressdiv").css("border","1px solid #A8D3FF");
				$("#txtEmailAddressdiv").css("background-color","#ffffff");
				$("#txtEmailAddressdiv").html("<img src='/images/info.gif' alt='ok' width='16' height='16' />&nbsp;Email must be valid!")
				}
				
				if($(this).attr("id") =="txtPassword"){
				$("#txtPassworddiv").css("color","grey");
				$("#txtPassworddiv").css("border","1px solid #A8D3FF");
				$("#txtPassworddiv").css("background-color","#ffffff");
				$("#txtPassworddiv").html("<img src='/images/info.gif' alt='ok' width='16' height='16' />&nbsp;Please enter a valid password between 5 and 25 characters!")
				}
				
				if($(this).attr("id") =="txtRetypePassword"){
				$("#txtRetypePassworddiv").css("color","grey");
				$("#txtRetypePassworddiv").css("border","1px solid #A8D3FF");
				$("#txtRetypePassworddiv").css("background-color","#ffffff");
				$("#txtRetypePassworddiv").html("<img src='/images/info.gif' alt='ok' width='16' height='16' />&nbsp;Please retype your password!")
				}
            }).blur(function(){
				
			if(myregvalid1 == 0 || myregvalid2 == 0 || myregvalid3 == 0 || myregvalid3 == 0){$("#btnSubmit").hide();}

			// start txtEmailAddress check
			// validate the username and other fields after leaving the field, see the onchange event for dynamic validations
			if($(this).attr("id") =="txtEmailAddress"){
			var myformvalue = $(this).attr("value");
				// lets check to see if this is a unique username!
				if(myformvalue !=""){
				var dataString = 'myformvalue='+ myformvalue;
					$.ajax({
						type: "POST"
						,url: "/comps/users.cfc/?method=function_checkemail"
						,data: dataString
						,success: function(returninfo){// successfull lets show some user info
							if (returninfo != ""){
							a=new Array();
							a=returninfo.split('|');
								if (a[1] == 0){
									$("#txtEmailAddressdiv").css("color","#C60000");
									$("#txtEmailAddressdiv").css("background","FFC0CB");
									$("#txtEmailAddressdiv").html("<img src='/images/error-red.gif' alt='ok' width='16' height='16' />&nbsp;")
									.append(a[0]);
									myregvalid1 = 0;
									if(myregvalid == 0){$("#btnSubmit").hide();}
									//$("#txtEmailAddress").focus();
								}
								if (a[1] == 1){
									$("#txtEmailAddressdiv").css("color","green");
									$("#txtEmailAddressdiv").html("<img src='/images/check.png' alt='ok' width='16' height='16' />&nbsp;")
									.append(a[0]);
									myregvalid1 = 1;
									if(myregvalid1 == 1 && $("#txtEmailAddress").attr("value")!="" && $("#txtPassword").attr("value")!="" && $("#txtRetypePassword").attr("value")!=""){
										$("#btnSubmit").show();
									} else {
										$("#btnSubmit").hide();
									} 
								}
								//alert(myregvalid1);
							}
						}						
						,error: function(returninfo){
							if (returninfo != ""){
								a=new Array();
								a=returninfo.split('|');
								if (a[1] == 0){
									$("#txtEmailAddressdiv").css("color","#C60000");
									$("#txtEmailAddressdiv").css("background","FFC0CB");
									$("#txtEmailAddressdiv").html("<img src='/images/error.gif' alt='ok' width='16' height='16' />&nbsp;")
									.append(a[0]);
									//$("#txtEmailAddress").focus();
								}
							}
						}						
					});
				}		
			}
		// end txtEmailAddress check
				
		// start txtPassword check
			// validate the username and other fields after leaving the field, see the onchange event for dynamic validations
			if($(this).attr("id") =="txtPassword"){
			var myformvalue = $(this).attr("value");
				// lets check to see if this is a unique username!
				if(myformvalue !=""){
				var dataString = 'myformvalue='+ myformvalue;
					$.ajax({
						type: "POST"
						,url: "/comps/users.cfc/?method=function_checkpassword"
						,data: dataString
						,success: function(returninfo){// successfull lets show some user info
							if (returninfo != ""){
							a=new Array();
							a=returninfo.split('|');
								if (a[1] == 0){
									$("#txtPassworddiv").css("color","#C60000");
									$("#txtPassworddiv").css("background","FFC0CB");
									$("#txtPassworddiv").html("<img src='/images/error-red.gif' alt='ok' width='16' height='16' />&nbsp;")
									.append(a[0]);
									myregvalid2 = 0;
									if(myregvalid2 == 0){$("#btnSubmit").hide();}
								}
								if (a[1] == 1){
									$("#txtPassworddiv").css("color","green");
									$("#txtPassworddiv").html("<img src='/images/check.png' alt='ok' width='16' height='16' />&nbsp;")
									.append(a[0]);
									myregvalid2 = 1;
									if(myregvalid2 == 1 && myregvalid1 == 1 && $("#txtEmailAddress").attr("value")!="" && $("#txtPassword").attr("value")!="" && $("#txtRetypePassword").attr("value")!=""){
										$("#btnSubmit").show();
									} else {
										$("#btnSubmit").hide();
									} 
								}
							}
						}						
						,error: function(returninfo){
							if (returninfo != ""){
								a=new Array();
								a=returninfo.split('|');
								if (a[1] == 0){
									$("#txtPassworddiv").css("color","#C60000");
									$("#txtPassworddiv").css("background","FFC0CB");
									$("#txtPassworddiv").html("<img src='/images/error.gif' alt='ok' width='16' height='16' />&nbsp;")
									.append(a[0]);
								}
							}
						}						
					});
				}		
			}
		// end txtPassword check

		// start txtRetypePassword check
			// validate the username and other fields after leaving the field, see the onchange event for dynamic validations
			if($(this).attr("id") =="txtRetypePassword"){
			var passwordA = $("#txtPassword").attr("value");
			var passwordB = $("#txtRetypePassword").attr("value");
				// lets check to see if this is a unique username!
				if(passwordB !=""){
				var dataString = 'passwordA='+ passwordA +'&passwordB='+passwordB ;
					$.ajax({
						type: "POST"
						,url: "/comps/users.cfc/?method=function_recheckpassword"
						,data: dataString
						,success: function(returninfo){// successfull lets show some user info
							if (returninfo != ""){
							a=new Array();
							a=returninfo.split('|');
								if (a[1] == 0){
									$("#txtRetypePassworddiv").css("color","#C60000");
									$("#txtRetypePassworddiv").css("background","FFC0CB");
									$("#txtRetypePassworddiv").html("<img src='/images/error-red.gif' alt='ok' width='16' height='16' />&nbsp;")
									.append(a[0]);
									myregvalid3 = 0;
									if(myregvalid3 == 0){$("#btnSubmit").hide();}
								}
								if (a[1] == 1){
									$("#txtRetypePassworddiv").css("color","green");
									$("#txtRetypePassworddiv").html("<img src='/images/check.png' alt='ok' width='16' height='16' />&nbsp;")
									.append(a[0]);
									myregvalid3 = 1;
									if(myregvalid3 == 1 && myregvalid2 == 1 && myregvalid1 == 1 && $("#txtEmailAddress").attr("value")!="" && $("#txtPassword").attr("value")!="" && $("#txtRetypePassword").attr("value")!=""){
										$("#btnSubmit").show();
									} else {
										$("#btnSubmit").hide();
									}

								}
							}
						}						
						,error: function(returninfo){
							if (returninfo != ""){
								a=new Array();
								a=returninfo.split('|');
								if (a[1] == 0){
									$("#txtRetypePassworddiv").css("color","#C60000");
									$("#txtRetypePassworddiv").css("background","FFC0CB");
									$("#txtRetypePassworddiv").html("<img src='/images/error.gif' alt='ok' width='16' height='16' />&nbsp;")
									.append(a[0]);
									//$("#txtRetypePassword").focus();
								}
							}
						}						
					});
				}		
			}
		// end txtRetypePassword check


			$(this).parent().find(".info").css("display", "none");
            });

				$("#btnSubmit").click(function() {   


		// start user insert
				var dataString = 'MYFORMVALUE='+ $("#txtEmailAddress").attr("value") +'|'+ $("#txtPassword").attr("value") +'|'+ $("#txtName").attr("value");
					$.ajax({
						type: "POST"
						,url: "/comps/users.cfc/?method=function_adduser"
						,data: dataString
						,success: function(returninfo){// successfull lets show some user info
							if (returninfo != ""){
							a=new Array();
							a=returninfo.split('|');
								if (a[1] == 0){
									$(".form-register").show();
									$(".check-email").hide();
									//$("#txtEmailAddressdiv").css("color","#C60000");
									//$("#txtEmailAddressdiv").css("background","FFC0CB");
									//$("#txtEmailAddressdiv").html("<img src='/images/error-red.gif' alt='ok' width='16' height='16' />&nbsp;")
									//.append(a[0]);
								}
								if (a[1] == 1){
									$(".form-register").hide();
									$(".check-email").show();
									//$("#txtEmailAddressdiv").css("color","green");
									//$("#txtEmailAddressdiv").html("<img src='/images/check.png' alt='ok' width='16' height='16' />&nbsp;")
									//.append(a[0]);
									//if($("#txtEmailAddress").attr("value")!="" && $("#txtPassword").attr("value")!="" && $("#txtRetypePassword").attr("value")!=""){
									//	$("#btnSubmit").show();
									//}
								}
							}
						}						
						,error: function(){
							alert("error");
						}						
					});
		// end user insert












					//alert($(txtEmailAddress).attr("value"));
				   
				  }); 
			
			
            });

function validateForm()
        {
			var retCode = true;
			var formsubmits = 0;
		// this will check to see if all text fields are not blank
            $(".formContainer input[type=text]").each(function(){
                var text = $(this).attr("value");
				var fieldname = $(this).attr("id");
                if (text == "" && $(this).attr("id") !="txtName")
                {
					$("#"+fieldname+"div").css("color","#C60000");
					$("#"+fieldname+"div").css("background","#FFC0CB");
					$("#"+fieldname+"div").html("<img src='/images/error-red.gif' alt='ok' width='16' height='16' />&nbsp;")
					.append("This Field is required!");
					formsubmits = formsubmits+1;
                }
            });
			if (formsubmits == 0){
				return retCode;
			} 
        }

        function clearForm()
        {
            $(".formContainer input[type=text]").each(function(){
                $(this).parent().find(".error").css("display", "none");
            });
       }
