﻿var submitted = false;

//$(document).ready(function(){
//    var forms = $('form.jquery_form').get();
//	
//	for(var i = 0; i < forms.length; i++) {	
//	    
//	    $('#' + forms[i].id + ' .jquery_submit').click(function() {
//	        var formid = this.parentNode.parentNode.id;
//	        
//	        var container = $('#' + formid + '-container');
//	        var validator = $('#' + formid).validate({
//	    	    errorContainer: container,
//	    	    errorLabelContainer: $("ul", container),
//	    	    wrapper: 'li',
//	    	    meta: "validate"	        
//	        });
//	        if(validator.form()) {
//	            eval($('#' + this.parentNode.parentNode.id + ' .function').attr('for') + '("' + formid + '")');
//	        }
//	    });
//	}
//});

//Method that gets called on step 1 of the post a project page
function submitRequest(formid) {
   
   if($('#email').val() != $('#confirmEmail').val()) {
      document.getElementById('email').className = 'required email text error';
      document.getElementById('confirmEmail').className = 'required email text error';
   }
   else {
    
       var ourForm = $('#' + formid);
       var d = '{"method":"' + formid + '"';
       var showtypes = ', "showType" : [';
       var projectName = ', "projectName" : [';
       var clientName = ', "clientName" : [';
       var inputs = [];
       var showtype = [];
       var projectname = [];
       var clientname = [];
       
       $(':input', ourForm).each(function() {
            if(this.attributes["varname"] != null && this.attributes["varname"].value == "showType") {
                if(this.checked) {
                    var name = (showtype.length > 0) ? "showType" + showtype.length : "showType";
                    
                    showtypes += '"' + escape(this.value) + '",';
                    showtype[showtype.length] = escape(this.value);
                }
            }
            else if(this.attributes["varname"] != null && this.attributes["varname"].value == "projectName") {
                if(this.value.length > 0) {
                    var name = (projectName.length > 0) ? "projectName" + projectName.length : "projectName";
                    
                    projectName += '"' + escape(this.value) + '",';
                    projectname[projectname.length] = escape(this.value);
                }
            }
            else if(this.attributes["varname"] != null && this.attributes["varname"].value == "clientName") {
                if(this.value.length > 0) {
                    var name = (clientName.length > 0) ? "clientName" + clientName.length : "clientName";
                    
                    clientName += '"' + escape(this.value) + '",';
                    clientname[clientname.length] = escape(this.value);
                }
            }
            else {
                d += ',"' + this.name + '":"' + escape(this.value) + '"';
            }
       });
       
       showtypes = (showtype.length > 0) ? showtypes.substr(0, showtypes.length - 1) + "]" : showtypes + ']';
       projectName = (projectname.length > 0) ? projectName.substr(0, projectName.length - 1) + "]" : '';
       clientName = (clientname.length > 0) ? clientName.substr(0, clientName.length - 1) + "]" : '';
       
       d += showtypes + projectName + clientName + "}";
       
       if(!submitted) {
           jQuery.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    data: d,
                    url: '../ScriptServices/CastingDirectors.asmx/Create',
                    timeout: 2000,
                    dataType: "json",
                    success: function(r) { 
                      //jsonShow = r.d;
                      //submitted = true;
                      window.location.href="Thanks.aspx";           
                      
                    }
                  })
        }
   }
}