var vl = '';

function vld()
{
	 this.vl = new Valid(arguments[0]);
}

Valid = function()
{
		var args = arguments;
		this.Model =
		{
				error:null,
				time_limit:null,
				time_counter:null,
				time:null,
				inf_check:null,
				cname:null,
				cemail:null,
				frm:null,
				w_box:null,
				h_box:null,
				using_external_container:null,				


				check_infusion_fields : function()
				{
					document.getElementById('debug').innerHTML = '';
					this.inf_check = '';
					this.time_counter = 0;
					this.show_box('progress');
				  this.poling();
				},


				poling : function ()
				{
						if (this.time_counter == 0)
						{
								this.aj();
						}
						try
						{
								this.inf_check = document.getElementById('debug').innerHTML;
								if(trim(this.inf_check) != '')
								{
										this.hide_box('progress');
										if (this.inf_check == 'OK')
										{
												this.inf_check = '<img src="http://deondesigns.ca/images/deondesignslogo.jpg"  width="300" height="120" border="0"><br>Thank you for submitting this quote!<br><br> We will contact you shortly!';																									
												this.show_info();
												this.frm.reset();
												return;																								
										}
										else
										{
												this.show_error();
										}
									  return true;
								}
								else
							  {
									this.time_counter = this.time_counter + this.time;
									if (this.time_counter >= this.time_limit)
									{
											this.hide_box('progress');
											this.inf_check = 'Internal Error !<br>';
											this.show_error();
											return true;
									}
							  	setTimeout('vl.Model.poling()',this.time);
							  }
						}
						catch(err)
						{
								this.hide_box('progress');
								this.inf_check = 'Internal Error !<br>';
								this.show_error();
						}
				},



				aj : function ()
				{
				    var aj = false;
					  if (window.ActiveXObject) { aj = new ActiveXObject("Microsoft.XMLHTTP");}
					  else if (window.XMLHttpRequest) {aj = new XMLHttpRequest(); aj.overrideMimeType("text/xml"); }
				    if(aj)
				    {
				        aj.open("POST", this.data_source, true);
				        aj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				        aj.onreadystatechange = function()
				        {
					          if (aj.readyState == 4 && aj.status == 200)
					          {
					          	document.getElementById('debug').innerHTML = trim(aj.responseText);
					          }
				        }
				        params = 't'+'=' + new Date().getTime();
							  for (var i=0;i < this.frm.elements.length;i++)
							  {
									  var e = this.frm.elements[i];
							  		if (e.type=='radio' || e.type=='text' || e.type=='password' || e.type=='select-one' || e.type=='hidden'  || e.type=='checkbox' || (e.type=='textarea' && e.className!="hidden-code" && e.className!="codepress php linenumbers-on"))
							  		{
												if (e.type=='radio' && !e.checked) 
												{
													continue;
												}
												if (e.type=='checkbox' && !e.checked) 
												{
													t = 1;
													continue;
												}
									      var buf_val = e.value;
												if(undefined===window.buf_val){window.buf_val = [];}
												buf_val = encodeURIComponent(buf_val);
									    	params += '&'+ e.name+'='+buf_val;
									  }
							  }				        
				        aj.send(params);
				    } return true;
				},

				show_box : function(box_id)
				{
					sm(box_id,this.w_box,this.h_box);
				},

				hide_box : function(box_id)
				{
					hm(box_id);
				},

				show_error : function()
				{
						this.error += this.inf_check + '<br>';
						this.error = '<font color="red">'+this.error+'</font>';
						this.show_box('box');
						get_id('txt').innerHTML = this.error;
						document.getElementById('rmbtn').focus();
				},
				show_info : function()
				{
						this.error = '<font color="green">'+this.inf_check+'<br></font>';
						this.show_box('box');
						get_id('txt').innerHTML = this.error;
						document.getElementById('rmbtn').focus();
				},
		};
		this.Model.data_source = 'email_form_ajax.php';
		this.Model.using_external_container = true;		
		this.Model.external_container_id = 'external_container';				
		this.Model.error = '';
		this.Model.time_limit = 4000;
		this.Model.time_counter = 0;
		this.Model.time = 1;
		this.Model.inf_check = '';
		this.Model.cname = '';
		this.Model.cemail = '';
		this.Model.frm = args[0];

		this.Model.w_box = 300;
		this.Model.h_box = 300;
		this.Model.check_infusion_fields();
}

function trim(str)
{
    str = this != window? this : str;
    return str.replace(/^\s+|\s+$/g,"");
}
