var errors=0;

new function() {
       // $.fn.validate = validate() {};
    $.fn.validate = {
        init: function(o) {
          if(o.name == 'website') { this.website(o) };
         if(o.name == 'email') { this.email(o) };

          if(o.name == 'password') { this.password(o) };
          if(o.name == 'confirmpass') { this.confirmpass(o) };

	  if(o.name == 'name') { this.name(o) };
          if(o.name == 'mobile') { this.mobile(o) };
          if(o.name == 'redirect') { this.redirect(o) };
          if(o.name == 'mailmessage') { this.mailmessage(o) };
          if(o.name == 'smsmessage') { this.smsmessage(o) };
          if(o.name == 'signature') { this.signature(o) };
          if(o.name == 'details') { this.details(o) };

          if(o.name == 'keywords') { this.keywords(o) };
          if(o.name == 'logo') { this.logo(o) };

          if(o.name == 'actype') { this.actype(o) };
        },
        website: function(o) {
           if (o.value.length>0) {
             doValidate(o);
            } else {
             doError(o,'website is mandatory');
            };
        },
  email: function(o) {
          var email  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
           if (o.value.match(email)) {
              doValidate(o);
            } else {
              doError(o,'not a valid email');
            };
        },
        password: function(o) {
          var pass = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
           if (!o.value.match(pass)) {
	if(o.value.length < 5)
	{
             doError(o,'Minimum password length is 5');
	}
	else
            doSuccess(o);

            } 
		
	else {
             doError(o,'no special characters allowed');
            };
        },
      confirmpass: function(o) {
          var pass = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
       if (!o.value.match(pass)) {
	if(o.value.length < 5)
	{
             doError(o,'Minimum password length is 5');
	}
	else if(o.value != $("#password").val())
	{
             doError(o,'Password and Confirm password sholud match');

	}else

            doSuccess(o);

            } 
		
	else {
             doError(o,'no special characters allowed');
            };
        },
      

  name: function(o) {
         if (o.value.length>0) {
              doSuccess(o);
            } else {
              doError(o,'Name is mandatory');
            };
        },

mobile: function(o) {
		
         if (o.value.length >=10) {
		 if(o.value.match(/^\d+$/))
		 {
              doSuccess(o);

		 }
		 else
	               doError(o,'Mobile number is invalid');

            } else {
              doError(o,'Mobile number is invalid');
            };
        },
  redirect: function(o) {
         if (o.value.length>0) {
              doSuccess(o);
            } else {
              doError(o,'Redirect URL is mandatory');
            };
        },

  mailmessage: function(o) {
         if (o.value.length>0) {
              doSuccess(o);
            } else {
              doError(o,'Mail Message L is mandatory');
            };
        },

      smsmessage: function(o) {
         if (o.value.length>0) {
              doSuccess(o);
            } else {
              doError(o,'SMS Message is mandatory');
            };
        },
      signature: function(o) {
         if (o.value.length>0) {
              doSuccess(o);
            } else {
              //doError(o,'Redirect URL is mandatory');
            };
        },
      details: function(o) {
         if (o.value.length>0) {
              doSuccess(o);
            } else {
             // doError(o,'Redirect URL is mandatory');
            };
        },
 keywords: function(o) {
         if (o.value.length>0) {
              doSuccess(o);
            } else {
              //doError(o,'Redirect URL is mandatory');
            };
        },

     logo: function(o) {
         if (o.value.length>0) {
              doSuccess(o);
            } else {
             //doError(o,'Redirect URL is mandatory');
            };
        },
      redirect: function(o) {
         if (o.value.length>0) {
              doSuccess(o);
            } else {
              doError(o,'Redirect URL is mandatory');
            };
        },
		


      
     };

     function doSuccess(o) {
              $('#' + o.id + '_img').html('<img src="images/accept.gif" border="0" style="float:left;" />');
              $('#' + o.id + '_li').removeClass("error");
              $('#' + o.id + '_msg').html("");
              $('#' + o.id + '_li').addClass("success");
     }

     function doError(o,m) {
	     errors=1;
//	     $("#submit").attr('disabled','true');
              $('#' + o.id + '_img').html('<img src="images/exclamation.gif" border="0" style="float:left;" />');
              $('#' + o.id + '_li').addClass("error");
              $('#' + o.id + '_msg').html(m);
              $('#' + o.id + '_li').removeClass("success");
     }
     //private helper, validates each type after check
     function doValidate(o) {
        	$('#' + o.id + '_img').html('<img src="images/loading.gif" border="0" style="float:left;" />');
		var data=null;
		if( o.id =="website" )
		data={ operation:"checkWeb",website: o.value};
		else if( o.id =="email" )
		{
		data={ operation:"checkMail",email: o.value};
		}

        	$.post('db/dbget.php',data , function(out) {
                        if (parseInt(out) == 0)
                  	{
                  	  doSuccess(o);
                  	}
                  	else
                  	{
                          doError(o,"Already Exists");
                  	}
                  });
    };

};
$.fn.match = function(m) {
	$('#' + this.get(0).id + '_img').html('<img src="images/loading.gif" border="0" style="float:left;" />');
	if ($(this).get(0).val() == $(m.match).val()) {
          $('#' + this.get(0).id + '_img').html('<img src="images/accept.gif" border="0" style="float:left;" />');
          $(m.error).removeClass("error");
          $(m.error).addClass("success");
          $('#' + this.get(0).id + '_msg').html("");
        } else {
          $('#' + this.get(0).id + '_img').html('<img src="images/exclamation.gif" border="0" style="float:left;" />');
          $(m.error).addClass("error");
          $(m.error).removeClass("success");
          $('#' + this.get(0).id + '_msg').html("The passwords don't match, please try again");
        };
};

function postFormData()
{
var postData="operation=add";
postData += "&website="+parseURL($("#website").val());
postData += "&email="+$("#email").val();
postData += "&password="+$("#password").val();
postData += "&name="+$.URLEncode($("#name").val());
postData += "&mobile="+$("#mobile").val();
postData += "&redirect="+$.URLEncode($("#redirect").val());
postData += "&mailmessage="+$.URLEncode($("#mailmessage").val());
postData += "&smsmessage="+$.URLEncode($("#smsmessage").val());
postData += "&signature="+$.URLEncode($("#signature").val());
postData += "&details="+$.URLEncode($("#details").val());
if(outFile != null)
postData += "&logo="+outFile['filename'];
else
postData += "&logo=";
postData += "&keywords="+$.URLEncode($("#keywords").val());
var acType=0;

if($("#actype0").attr('checked'))
	acType=0;
else
	acType=1;
	
postData += "&actype="+acType;

	jQuery.ajax({type: "POST",url:'db/dbget.php' ,data:postData,
         success: function(data) 
	 {
	 	if(parseInt(data) == 1)
		{
		alert("You have successfully registered to irefer.\n Please login to get API details");
		if(acType == 0)
		{
		self.location="login.php";
		}
		else
		{
		$("#paypal").submit();

		}

		}
		else
		{
			alert("Website is already registered to irefer");
			$("#website").select();

		}
	 
	 },
         async:   false
    	});  



}
function sendFormDetails()
{
	errors=0;
	
  $(".validated input").each(function() {
          $(this).validate.init(this);
  });
  $(".validated textarea").each(function() {
          $(this).validate.init(this);
  });

  
if(errors==1) return false;
if($("#logo").val().length > 0)
{
$("#uploadForm").submit();
}
else
postFormData();










}
$(document).ready(function()
{

$("#actype0").click();
  $(".validated input").blur(function() {
          $(this).validate.init(this);
  });
  
  $(".validated textarea").blur(function() {
          $(this).validate.init(this);
  });

  $(".form li").hover(function(){$(this).addClass("selected");},function(){$(this).removeClass("selected");});
  $(".form li").mouseover(function() {
          $(this).addClass("selected");
  });
  $(".form li").mouseout(function() {
          $(this).removeClass("selected");
  });



$("#actype0").click(function()
		{
		$("#logoText").text("Website Logo:");
		});
$("#actype1").click(function()
		{
		$("#logoText").text("Website Screenshot:");
		});



$(".submitData").click(function()
	{
	sendFormDetails();

	});


 var options = { 
	 beforeSubmit:  startRequest,	   
        success:       showResponse 
    	}; 

	$("#uploadForm").ajaxForm(options);



});
var outFile=null;

function startRequest(formData, jqForm, options) 
{
         $('#logo_img').html('<img src="images/loading.gif" border="0" style="float:left;" />');

}
function showResponse(responseText, statusText)  
{
	      eval('outFile='+responseText);

	      if(outFile['error']== 0)
	      {
              $('#logo_img').html('<img src="images/accept.gif" border="0" style="float:left;" />');
              $('#logo_li').removeClass("error");
              $('#logo_msg').html("");
              $('#logo_li').addClass("success");

	postFormData();

	      }
	      else
	      {

	      }

}
 


