// Author : Arvind M.Mittal  arvind.mittal@gmail.com
	// alert("ok");

	
// I am  to be able to code this.
	// $(":input").css({'font-size':'10pt'});
	var enqformScriptPath = ""; 	
	document.write("<div id='formDiv' style='display:none;'></div>");
	$("script").each(function(){
		enqformScriptPath = $(this).attr('src');
		if(enqformScriptPath && enqformScriptPath.search('enquiry.js') >= 0 ){						
			enqformScriptPath = enqformScriptPath.replace('enquiry.js','');
		}
		//enqformScriptPath += $(this).attr('src') + "<br/>";
	})
	// $("#msgpop").html(location.hostname + "<br/>" + enqformScriptPath);
	$(".enqForm").html("<img id='formButton' src='"+enqformScriptPath+"enquirydesk.jpg' style='cursor:pointer; width:100%; border:0px;' alt='enqForm' />");
	
	$( "#formDiv" ).html('').load(enqformScriptPath+"enquiry.php");
	
	$(function() {		
		// $(":input").each(function(){	populateMyTitle_in_input( $(this) );	});		
		$(":input").focusout(function(){	populateMyTitle_in_input( $(this) );	});
		$(":input").focusin(function(){		clearMyTitle_in_inputs( $(this) );		});	
		
		// $(":input").click(function(){ alert( "okok" ); });	
				
		//$("#formButton").css({'width':'100%'});
		//$("#formButton").width('100%');
		// alert("okokok now");	
		$("body").delegate("#formButton, .enqformButton", 'click', function(){
			// alert("okokok now");		
			applyJQ();			
			//$(".flashDiv").hide();
			// $( "#formDiv" ).load(enqformScriptPath+"enquiry.php");
			$( "#formDiv" ).dialog({
				autoOpen: false,
				width : '500',
				height: 'auto',
				title: 'Enquiry Form',
				zIndex: '20000',
				modal:true,
				show: "explode",					
				hide: "explode"
				//close: function(event, ui) {	$(".flashDiv").show();	}
			}).parents(".ui-dialog").find(".ui-dialog-titlebar").css({'background':'none','border':'none'});
			$( "#formDiv" ).dialog().parents(".ui-dialog").find(".ui-icon-closethick").css({'background-color':'#999'});
			// .parents(".ui-dialog").find(".ui-dialog-titlebar").hide();
			$(".jqbutton").button();		
			$( "#formDiv" ).dialog('open');						
			
		})
	});
	

	function applyJQ(){	
		// $(".popenqclosebutton").click(function(){
			// $( "#formDiv" ).dialog('close');
			// $("form[name='enquiryForm']").clearQueue();
		// });
		
		$("form[name='enquiryForm']").submit(function() {
			var formOK = true; //formSubmit();
			if(formOK){		
				formOK = doAjax_enquiryForm();
			}
			formOK = false;
			return false;		// As Form is not to be submitted, but values are passed thru Ajax
		});	
	}
	
	function clearMyTitle_in_inputs(oThis){
		if ( $(oThis).val() == $(oThis).attr('title') || !$(oThis).val() ) {
			$(oThis).val('');
			$(oThis).removeClass("inpDefaultText");
			// $(oThis).css({'color':'blue'});
		}		
	}
	function populateMyTitle_in_input( oThis ){
		if ( $(oThis).val() == "" ) {
			$(oThis).val( $(oThis).attr('title') );
			$(oThis).addClass("inpDefaultText");
				// $(oThis).css({'color':'black'});
		}
	}
	
	function doAjax_enquiryForm(){
	
		$(":input").each(function(){		clearMyTitle_in_inputs( $(this) );		});	
		
		var sends = $("form").serialize();  // below is defined as google chrome was excluding empty <select> on 2nd submission
		// var sends = $(".inpRF").serialize();
		
		// $("#errorid").html('input fields : ' + sends + "<br/><hr/>");
		//alert(sends);
		$(":input").removeClass("bdr2red").addClass("bdr2blue");
		$.ajax({
		   cache:false,
		   type: "POST",
		   url: enqformScriptPath+"enquiry_ajax.php",
		   data: sends,
		   success: function(msg){					
			//$("#errorid").append('Returned Msg from Ajax' + msg + "<br/><hr/>");			
			 if(msg.indexOf("*OK*") >= 0){
				
				$("#formDiv").html('Your Query has been posted. You shall hear from us soon.' + "<br/><hr/><span style='font-size:8pt;'>press 'esc' to close</span>\n"); //+msg
				//$( "#formDiv" ).dialog("close");		
				// $("#formButton").hide();
				// $("#formButton").parent().html("Query<br/>Submitted");
				$("#formButton").attr('src', enqformScriptPath+'enquirydeskposted.jpg');
				//$('#formButton').unbind('click');
				
				//$('html, body').animate({scrollTop:0}, 'slow');										
			 } else if (msg == "*NO*"){
				$("#errorid").html("Error or Server Busy", "Form could not be submitted!! Please try again.");
			} else if (msg.indexOf("*DUP*") >= 0 ){
				$("#errorid").html("<span style='color:red;'>A Query has already been submitted from this Computer/ Device! Please try after sometime..</span>"); //+msg
			}else {
			
				$("#errorid").html("Input Error: All Inputs marked with <span style='font-size:7pt; color:red;border:1px solid;'>&nbsp;Red Border&nbsp;</span> are Required.");	//+msg
				// $(":input").each(function(){	populateMyTitle_in_input( $(this) );	});
				
				var reqFields = msg.split(' ');    //  Non-valid Inputs (name) are returned thru Ajax seperated by a space
				msg="";
				//alert(reqFields.length);
				
				for(i=0; i < reqFields.length; i++){
					if(reqFields[i] != ""){
						msg += reqFields[i] + "-";
						selectorX = ":input[name='" + reqFields[i] + "']";
						$(selectorX).removeClass("bdr2blue").addClass("bdr2red");
					}
				}
				// $("#errorid").prepend(msg + "<br/><hr/>");
				
				
			 }
		   }
		 });
		 
		 sends="";		
	}
