// this is for ONLINE FORMS ONLY if you want normal ajax, use /srv/addingajax_basic.js ... or if in admin, use /admin/js/
if(typeof(isK)=='undefined') var isK=0;
var addingajaxcalled=1;  
var addingajaxincluded=1;
if(!typeof(w3url)=='string'||typeof(w3url)=='undefined'||!w3url.substring(0,4)=='http') {
	var w3url; // this should come in from user web page but if not, set it to default w3shoppingcart
	(location.href.substring(0,5)=="https")?w3url="https://www.w3now.com":w3url="http://www.w3now.com";	 
}  
// preload images
var myimages=new Array()
function preload(){
	for (var i=0;i<preload.arguments.length;i++){
		myimages[i]=new Image();
		myimages[i].src=w3url + "/srv/" + preload.arguments[i]; 
	}
}  
preload("images/ajax-loader.gif","images/ajax-loader-bar.gif","images/spacer.gif");   
//var dom = document.getElementById
// no semicolon above! 
var xmlhttp;  
// create XMLHttpRequest object 
function aaGetXmlHttpRequest() { 
   var xmlHttpObj = false;
   if(window.XMLHttpRequest) {
      xmlHttpObj = new XMLHttpRequest();
   } else {
      try
         {
            xmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
         }
         catch (e)
         {
            try
            {
               xmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
               xmlHttpObj = false;
            }
         }
    }
    return xmlHttpObj;
}
//preloader
function ajaxPreloaderForm(space, ps, spacedown,which) {
	if(!space) return;
	var c=getContainer(space);
	if(!c) return;
	if(!which) which='wheel';
	if(!ps) ps="center"; 
	var i,spa='';
	if(spacedown) { 
		for(i=1;i<=spacedown;i++) {spa=spa + "<br>";}
	}   
	if(which.indexOf('wheel')==0) {
		var wheelurl=w3url+"/srv/images/ajax-loader.gif";
		if( typeof(app_site_theme)!="undefined" && app_site_theme.length>=3)  wheelurl=w3url+"/admin/adminSite/templates/loader_wheel_"+app_site_theme+".gif"; 
	} 
	if(which=='wheel') c.innerHTML = '<div style="text-align:'+ps+';width:100%;overflow:hidden;padding:4px;color:black;background-color:white;z-index:600;">' + spa + '<img src="'+wheelurl+'" style="height:28px;width:28px;border:0;" alt="Loading..." align="absmiddle">One moment please</div>'; 
	else if(which=='bar') c.innerHTML = '<div style="text-align:'+ps+';">' + spa + '<img src="'+w3url+'/srv/images/ajax-loader-bar.gif" style="height:11px;width:43px;border:0;" alt="Loading..."></div>';  
	else if(which=='space') c.innerHTML = '<div style="text-align:'+ps+';padding:4px;color:black;background-color:white;white-space:nowrap;">' + spa + 'One moment please. . .</div>';  
	c.style.display='block';//loadingSpace
} 
function getContainer(dv) { 
	var container; 
	if(document.getElementById) {
		if(document.getElementById(dv)) container = document.getElementById(dv);   
	}
	else {
		var c=eval(dv);
		 if(c) container = c;
	} 
	return container;
}  
function doNothing() { } 
// for new form library 2014

var loadSpace;
var loadSpace2;
var loadContainer; // object to load rather than space
var loadContainerAlsoArr = new Array(); // array of objects, not spaces
var loadingSpace; 
var closeSpace;// close when no error
var formHasSmoke=false; // site builders do (old) 
var formHas2014Smoke=false;// new site builder forms 2014
var global2014=false;
var submitButtonSpace;
var errSpace;
var globalciid=0;
var callbackOnSuccess;
var cbosParm1='';// callbackOnSuccess parm 1, if any
var cbosParm2='';// callbackOnSuccess parm 2, if any 
var cbosParm3='';// callbackOnSuccess parm 2, if any
var callbackOnError;
var cboeParm1='';// callBackOnError param 1, if any
var cboeParm2='';// callBackOnError param 2, if any
var cboeParm3='';// callBackOnError param 3, if any
var gettingCaptcha=false;
function reset2014Globals() {
	loadSpace=null;
	loadSpace2=null;
	loadContainer=null;// container that may be used in place of loadSpace 
	//loadContainerAlsoArr = new Array(); // array of objects, not spaces
	loadContainerAlsoArr.length=0;// found this is the best way to empty an existing array  
	loadingSpace=null; 
	closeSpace=null;// close when no error
	formHasSmoke=false; // site builders do (old) 
	formHas2014Smoke=false;// new site builder forms 2014
	global2014=false;
	submitButtonSpace=null;
	errSpace=null;
	globalciid=0; 
	callbackOnSuccess=null;
	cbosParm1='';// callbackOnSuccess parm 1, if any
	cbosParm2='';// callbackOnSuccess parm 2, if any 
	cbosParm3='';// callbackOnSuccess parm 2, if any
	callbackOnError=null;
	cboeParm1='';// callBackOnError param 1, if any
	cboeParm2='';// callBackOnError param 2, if any
	cboeParm3='';// callBackOnError param 3, if any
	gettingCaptcha=false;
}
function initForms2014() {// also calls to load the captcha images 
	// every site manager web page calls this to see if there are any forms on the page and if so, init each
	// reads element by name to see if there are any web forms on the page, and if so, calls captcha to get all the right number of captchas and fill them all in one swoop  
	var jqForms=$("form[id^='formid']");
	if(jqForms) if(isK) console.log(" jqForms found on page: " +jqForms.length);
	if(jqForms.length==0) return; 
	var zval,field,formCaptchaAjaxUrl=null;
	var formCiidArr = new Array();
	var ciid=0;
	gettingCaptcha=false;//reset global	
	loadContainerAlsoArr.length=0;// global
	$(jqForms).each(function (){
		field=$(this).find('input[name="returnToURL"]');
		ciid = $(this).attr("id").substring(6);// start on char 6 after the word "formid"  to get the ciid remaining
		if(field&&ciid) { 
			if(isK) console.log("form push ciid is: " + ciid);
			formCiidArr.push(ciid); 
			zval = field.val()
			if(zval.indexOf('http') === 0 && zval.indexOf("fForm.cfm")==-1 && zval.indexOf("w3sc_WebformPost.cfm")==-1) field.val( zval + "/w3sc_WebformPost.cfm");
			if(isK&&1)console.log(" jq set form field value to: " +field.val());
			if(!formCaptchaAjaxUrl) formCaptchaAjaxUrl=field.val(); // the post url where we will get all the captcha images (this  url /w3sc_WebformPost.cfm is the same for ALL of these forms)
		}
	});
	if(formCiidArr.length>0) getAllCaptchas2014(formCiidArr, formCaptchaAjaxUrl); // callsback to finish the Init forms, we gotta add the captchas before we can size the form  
}  
function getAllCaptchas2014(arr, url) {// this is called by the  initForms2014 above  
	arr = jQuery.unique(arr); // // jquery function to give us only unique values in the arr (in case we have the same form on the same page more than once)  
	if(isK) console.log("getting all captchas: " +arr.length);  
	errSpace=null;
	var ciid;
	for(var x=0;x<arr.length;x++) { //formCiidArr , an array of ciid's
		ciid=arr[x];
		$(".w3sc"+ciid+"_getCaptchaCodeBox").each(function (){ // best to use a Class there might be MORE THAT ONE instance of this same form ciid on the same page <div id="divCaptchaCIID"> 
			loadContainerAlsoArr.push(this); // this has the <div captcha jQuery OBJECT , not the space, loadContainerAlsoArr[0], etc
			if(!errSpace) errSpace=$(this).attr("id");// ciid ;// just pick the frst space for err, there should not be any 
		});
	}
	if(loadContainerAlsoArr.length==0) { 
		if(isK&&1) console.log("No divCaptchaCIID div elements found");  
		finishInitForms2014(arr);		
		return;  
	}
	if(!xmlhttp) xmlhttp = aaGetXmlHttpRequest();
	if(!xmlhttp) return;
	gettingCaptcha=true;//global 
	url = url+'?w3cp=1&n=4&NbrCaptchasNeeded='+loadContainerAlsoArr.length; // http://www.ownersite.com/w3sc_WebformPost.cfm?w3cp=1&n=4&NbrCaptchasNeeded=2 is where we "ajax" to and get the captcha images  
	callbackOnSuccess="finishInitForms2014";
	cbosParm1=arr;// send ciid array on callbackOnSuccess parm 1, if any
	cbosParm2='';// callbackOnSuccess parm 2, if any 
	console.log("getting all captchas at url: " + url);
	xmlhttp.open('GET', url, true); 
	xmlhttp.onreadystatechange = fillGivenFormSpace;
	xmlhttp.send(null);  
	return false;  
}  
function finishInitForms2014(arr) {// we have filled all the captchas so we can measure the divs now, or we had no capchas..
// sets heights etc for each of them so when the form is submitted the thank you message does not shrink (or overflow) the space that the form occupied  
	gettingCaptcha=false;//reset global
	var h,w,ciid,elem,elem1,elem2,L,ow;  
	for(var x=0;x<arr.length;x++) { //formCiidArr , an array of ciid's
		h=0;w=0;elem=null;elem1=null,elem2=null
		ciid=arr[x];
		$(".w3sc"+ciid+"_mainDiv").each(function (index,value){ // there might be MORE THAT ONE instance of this same form ciid on the same page <div id="divCaptchaCIID"> 
				//THE IDEA OF SAME CIID FORM MORE THAN ONCE ON A PAGE IS NOT DONE YET BUT THIS IS A START, WE NEED TO IDENTIFY EXACTLY WHICH <FORM> OBJECT IS BEING SUBMITTED, FROM THE SUBMIT BUTTON
				if(isK&&1) console.log("looking at form "+ciid+" index: "+index+ " with value: "+value);
				h=$(this).height();
				w=$(this).width(); 
				if(h&&w) {  
						$(this).css({"height":h+"px","width":w+"px"}); 
						elem=$(this).find("img.w3sc"+ciid+"_smokeScreen");// best to use a class, says jQuery 
						if(elem) {
							$(elem).css({"height":h+"px", "width":w+"px"});
							if(isK&&0) console.log("we set first image smokeScreen size inside of " + $(this).attr("id") + " to width/height: "+$(elem).width() + "/"+$(elem).height());
						}
						elem1=$(this).find(".w3sc"+ciid+"_err");// best to use a class, says jQuery 
						if(elem1) {
							$(elem1).css({"height":h+"px", "width":w+"px"});
							if(isK&&0) console.log("we set first divErrSpace size inside of " + $(this).attr("id") );
						} 
						elem2=$(this).find(".w3sc"+ciid+"_loading");// best to use a class, says jQuery 
						if(elem2) {
							$(elem2).css({"width":"100px","background-color":"#ffffff","color":"#000000","font-size":"13px","font-family":"arial,helvetica,sans-serif"});
							ow=$(elem2).width();
							if(isK&&0) console.log("the width of first divLoadingSpace is " +ow+ " and the width of the main box is: " +w);
							ow=parseInt(ow/2); 
							L = parseInt((w/2) - ow);// center the 100px 'please wait' Wheel space in the form space	 
							if(L>0)$(elem2).css({"left":L+"px"});  
							if(isK&&0) console.log("we set first divLoadingSpace LEFT inside of " + $(this).attr("id") + " to Left: "+$(elem2).css("left") );
						} 
				} 
		});
	} 
}  

function getACaptchaImage2014(ciid,n) {   // for 2014 forms, called by the form to get new captcha
	if(!xmlhttp) xmlhttp = aaGetXmlHttpRequest();
	if(!xmlhttp) return; 
	if(!ciid) return;  
	loadSpace='divCaptcha'+ciid;
	errSpace=loadSpace; 
	formHasSmoke=false; // global
	formHas2014Smoke=false;// global	
	var theForm=eval("document.form"+ciid); 
	if(!theForm) return; 
	var field = theForm.returnToURL;//url where the form will be posted (on owner site url) to and where to get captcha image
	if(!field || field.value.substring(0,4)!='http') return; 
	var url =field.value; 
	var qrystr='w3cp=1&n='+n+'&NbrCaptchasNeeded=1';
	if(url.indexOf('?')==-1) url=url+'?';
	else url=url+'&';
	url+=qrystr;
	xmlhttp.open('GET', url, true); 
	xmlhttp.onreadystatechange = fillGivenFormSpace;
	xmlhttp.send(null);  
	return false; 
}  
function fillGivenFormSpace() { 
	   if(isK && xmlhttp.readyState == 4 && xmlhttp.status != 200) {
			var container;	
			if(errSpace)  container = $("#"+errSpace);
			else if(loadSpace2)  container = $("#"+loadSpace2);//if load space is not null, use it.			
		   	else if(loadSpace)  container = $("#"+loadSpace);  
			else if(loadContainer) container=loadContainer; // it is already a jQuery container, not a space
			else if(loadContainerAlsoArr.length>0) container=loadContainerAlsoArr[0]; // it is already a jQuery Div container, not a space
			if(container) container.innerHTML = xmlhttp.responseText + "<br>xmlhttp.status: " +xmlhttp.status;
			else console.log("error: " +  xmlhttp.responseText + "xmlhttp.status: " +xmlhttp.status );   
	   } 
	   if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {    
	   		if(isK&&0)  console.log("returned ok: " + xmlhttp.responseText);  
			var posE = -1;
		    var str=xmlhttp.responseText;   
			var pos=str.indexOf('<span id="w3scet"></span>');  
			var container;
			var hasError=false; 
			if(loadingSpace) hideSpace(loadingSpace);//get rid of preloader:
			if(formHasSmoke) unSmokeEm(); //once it returns you gotta unsmoke em no matter if error or good form 
			hideSpace("smokeScreen"+globalciid);// these should all have smoke; c.style.display="block"'  
			 if(pos >= 0) {   // no error
			 		str=str.substring(0,pos);
					if(loadSpace2)  container = $("#"+loadSpace2);//if load space is not null, use it.
					else if(loadSpace) container = $("#"+loadSpace);  // will be the same as closeSpace, if smoke
					else if(loadContainer) container=loadContainer; // it is already a jQuery object container, not a space
					else if(loadContainerAlsoArr.length>0) container=loadContainerAlsoArr[0]; // it is already a jQuery container, not a space (set it as this for now, just so it can get inside the if(container) check below) 
					if(str.length > 0) str= '<div style="background:white;padding:5px;border-radius:5px;-moz-border-radius:5px;"><span style="color:black;">' + str + '</span></div>';   
					if( ! formHasSmoke && closeSpace) hideSpace(closeSpace);// close up the form itself, since no error (if no smoke)
					if(errSpace) hideSpace(errSpace);//no err
			}
			else {// has form error 
					hasError=true;
					container = $("#"+errSpace);
					var posE=str.indexOf('<span id="w3scet">0</span>');  
					if(posE>=0) str=str.substring(0,posE);  
					if(closeSpace) showSpace(closeSpace);
					if(submitButtonSpace) showSpace(submitButtonSpace);//re-enable the submit button 
			}  
			if(str.indexOf('~eq~') >= 0)  str=str.replace(/~eq~/gi, '=');//replace all occurrences, case-insensitive, gotta do it this way because we cannot send any "=" signs in the return message due to delimiters: "u=uuid&htmlhat;msg=good post"
			if(container) {// loadspace or errSpace, or loadSpace2 or loadContainerAlsoArr[0] or loadContainer or 
					if(str.length > 0) {// might be either an error or not   
							if(hasError) {
								window.location ='#formTop'+globalciid;// then move view to the top of form so we can see the returned message  
								str = '<div style="background:white;padding:5px;border-radius:5px;-moz-border-radius:5px;"><span style="color:red;font-size:12px;">' + str + '</span></div>';
								if(global2014) str+= '<br /><div align="center"><input type="button" value="OK" onClick="hideSpace(errSpace);hideSpace(\'smokeScreen\'+globalciid);"></div>'; 								 
								$(container).html(str).css({"display":"block"}); 
							}
							else if(loadContainerAlsoArr.length>0&&str.indexOf("^")>0) {// true when getting captcha images
									var strArr = str.split("^"); // list to array		 
									for(var x=0;x<strArr.length;x++) {  
										if(x<loadContainerAlsoArr.length) {  // should be! loadContainerAlsoArr is an array of jQuery Div objects where we want to put captcha images										
											if(strArr[x] != "~") { // ~ means I' didn't want to fill this space, but have another one coming in the next required load space
												$(loadContainerAlsoArr[x]).html(strArr[x]).css({"display":"block"}); //loadContainerAlsoArr  we ALWAYS show the Also spaces if they are given												 
											}
										}
										else  if(isK&&1&&strArr[x] != "~")  console.log(" ! no container for loadContainerAlsoArr[" +x + "] but we have a string for it" );  
									}//for  
							}
							else {// if it's an error, it will go here also
								window.location ='#formTop'+globalciid;// then move view to the top of form so we can see the returned message   
								$(container).html(str).css({"display":"block"}); 
							}
						}
						else { // no string length  returned
							if(hasError)  $(container).html("There were errors. Please correct and try again.").css({"display":"block"});  // the container is the errSpace if it has an error  
							else { //  no error and  and no string returned  (has smoke means it's a web form submit 
								if(formHasSmoke) $(container).html("Thank You!<br />Your information was sent to us.").css({"display":"block"});  
								else $(container).css({"display":"block"}); // the container is the divMessageSpace+id so just close it up since there is no string to display and no error  // the container is the divMessageSpace+id 
							} 
						}  
				}
				else if(str.length > 0 && isK) console.log("NO CONTAINER AND HAS A STRING in /srv/js/addingajax.js: " + str  );
				 
				if(isK)console.log("Ajax call done in addingajax.js (for forms), now looking for any callbacks. has err: " + hasError);				
				// now do callbacks, if any
				if(!hasError) { //success, do callback if any
		 			if(callbackOnSuccess) { 
						window[callbackOnSuccess](cbosParm1,cbosParm2,cbosParm3);//perform the function
						callbackOnSuccess=null;
						cbosParm1='';
						cbosParm2='';
						cbosParm3='';
					}
				}  
				else { //error
		 			if(callbackOnError) {
						window[callbackOnError](cboeParm1,cboeParm2,cboeParm3);//perform the function
						callbackOnError=null;
						cboeParm1='';
						cboeParm2='';
						cboeParm3='';
					}
				}
				reset2014Globals();
		    }  
}  
function slideUpSpaceAfterTime(space, seconds, isSpaceInParent) {  
	var miliseconds = seconds * 1000; 
	if(isSpaceInParent) {
			window.parent.$("#"+space).delay(miliseconds).slideUp(600, "linear", function() {
				 parent.unSmokeEm(); // after animation complete
			}); 
	}
	else { 
			$("#"+space).delay(miliseconds).slideUp(600, "linear", function() {
			    unSmokeEm();// after animation complete
			}); 
	}  
} 
function SubmitFormAjax(givenForm,id,zOwnerUUID,appURL,lang,echo, is2014) {  
	if(!xmlhttp) xmlhttp = aaGetXmlHttpRequest();
	if(!xmlhttp) return;  
	var theForm=document.getElementById("formid"+id);
	if(!theForm) {console.log(" SubmitFormAjax() resorting to givenForm"); theForm=givenForm;	}
	if(!theForm) {console.log(" SubmitFormAjax() no theForm"); return;}
	else {console.log(" SubmitFormAjax() we have theForm: " + theForm.name ); }
	if(!id) {console.log("SubmitFormAjax() no id: " +id); return; }
	if(!is2014) is2014=0;
	global2014=is2014;// global: 1 or false tells fillFormSpace what to do 
	globalciid=id;// so fillFormSpace can find the 2014 smoke and turn it off 
	if(!lang) lang="PHP"; 
	lang=lang.toUpperCase();
	errSpace="divErrSpace"+id; 
	formHasSmoke = ( !is2014 && typeof(smokeEmOut)=="function" && typeof(unSmokeEm)=="function" && getContainer("smokeScreen") ); // 2014 forms will NOT have this smoke site builder form template has this (global! no var)
	formHas2014Smoke = ( is2014 && getContainer("smokeScreen"+id) );// make the smoke fill the form space when we submit the form, we simply display this space in this case
	if(errSpace) getContainer(errSpace).style.display="none";//close this in case they had a prior error 
	if(getContainer("closeWindowWhenDone")) { // this div is in site manager templates iframe_RequestAQuoteForm.cfm (iframe layer over), which is opened from parent: Admin18_cartPageTemplate.cfm
		var winSpace1 = parent.document.getElementById("popOverSmokeDiv");
		if(winSpace1) {
			callbackOnSuccess="slideUpSpaceAfterTime"; 
			cbosParm1="popOverSmokeDiv";// div is n Admin18_cartPageTemplate.cfm
			cbosParm2=8;// slide it up after 8 seconds
			cbosParm3=1; // this space is in the parent document
		}
	} 
	if(sFormFull(theForm)) { // located in cute_kart_net.js 	 
			submitButtonSpace="divSubmitButton"+id; // the submit button, important to set this so submit module knows to re-open it on an error 
			hideSpace(submitButtonSpace);
			getContainer(submitButtonSpace).style.display="none";//disable the submit button
			loadingSpace="divLoadingSpace"+id;// for wheel, both 2014 and other forms
			if(is2014) {
					showSpace(loadingSpace); //shows the existing overlay of ajax wheel (the wheel is already in it, display none div)
					// we already measured and sized the outer DIV when the page loaded, in init above, no need to do it here
					closeSpace=null; 
				 	loadSpace="w3scform"+id;
					if(formHas2014Smoke) showSpace("smokeScreen"+id);// these should all have smoke; c.style.display="block"' 
			} 
			else {  
					ajaxPreloaderForm(loadingSpace,'center',0,'wheel');// for all other web forms   
					// do the preloader first, then measure the outer div and set the height so that the page does not collapse when thank you comes in, or we close the inside space
					var mainDivId="divFormSpace"+id;
					var d=getContainer(mainDivId); 
					if(d) {
							var h=$("#"+mainDivId).height();// we have jquery on all templates now 
							if(h&&h>20) d.style.height=h+"px"; 
					}
					// try smoking them out instead of closing 
					if(formHasSmoke) {
						loadSpace="divFormElementSpace"+id;// rather than closing the form (since we have smoke to smoke it out instead) and causing the site to collapse, load the result into the form area 
						closeSpace=null;
						smokeEmOut(); // so they can't move while processing
					}
					else {
						loadSpace="divMessageSpace"+id; 
						closeSpace="divFormElementSpace"+id;  
						hideSpace(closeSpace);//close up the form itself so they can't affect it while processing  
					}  
			} 
			window.location ='#formTop'+id;// then move view to the top of form so we can see the returned message  
			var urlqry='fc='+zOwnerUUID+'&id='+id+'&appURL='+encodeURIComponent(appURL);//owner uuid & ciid & brand url 
			if(echo=='Echo') urlqry=urlqry + '&Echo=1';//tell it to echo the form fields back as sent in the emai to the owner
			else urlqry=urlqry + '&Echo=0';
			var qry='';// form field qry			
			var priorFieldID='';  
			var okqrystring=0;  
			var url,TCS, theField,FormField_Name,fieldValue,idParseArray,zFieldType;
			var fvID,qLFieldNames,qLarr,qLfield,thisQtyListVar;// for qty lists
			var qLfirst=true;		
			// first string any qty lists into dynamic variable: field.id_fieldNames
			for (var f=0; f < theForm.elements.length; f++) {
					theField = theForm.elements[f];			
					if(theField.id && theField.id > ' ') {  
						idParseArray = theField.id.split('_');				
						// should be at least 4: T_ffid_validID_required like:  T_1500_60_1 
						fvID = idParseArray[idParseArray.length-2]; // fvID is next-to last element   
						if(fvID==60) {	// Qty List
								thisQtyListVar = theField.id + "_fieldNames"; 
								if(qLfirst) {  // if it's the first field in the qty list 
									window[thisQtyListVar] = theField.name; // set dynamic variable (each field "name" is different in the qty list, they all share the same id tho)
									qLfirst=false;// so it will begin stringing the rest of the fields below
								 }
								else  window[thisQtyListVar] += "~" + theField.name; 								  
						}// 60 is Qty List
					}
			}	 
			for (var f=0; f < theForm.elements.length; f++) { 
					okqrystring=0;
					theField = theForm.elements[f];   
					FormField_Name=theField.name;  
					if(theField.id && theField.id > ' ') { 
						if(theField.id != priorFieldID) {  // checkboxes and radios and qtylists are read here as several fields; we'll submit them all for cf lists.		 
							priorFieldID=theField.id;
							zFieldType=theField.type.toLowerCase();
							idParseArray = theField.id.split('_');							 
							TCS = idParseArray[idParseArray.length-4].toUpperCase(); // etc   // T or C or S (text,  checkbox or radio or Qtylist is Q, or select [needs selectedIndex pull]   							
							 if(TCS == 'C') { // checkbox or qtylist (maybe a single checkbox, too)	 
									if( eval('theForm["'+ FormField_Name + '"].length') ) {
										 	nbrCkboxesRadios=eval('theForm["'+ FormField_Name +'"].length'); // it HAS to be done this way 
											// field length is the number of occurrences (selections) of the checkbox or radio field						
											theField = eval('theForm["' + FormField_Name + '"]');  
											//run through and pick up all checkbox values and string them together for cflist	 
											fieldValue='';												
											for (var i=0; i < nbrCkboxesRadios; i++) {  
											 	 if( theField[i].checked ) {
												 	 fieldValue = fieldValue + ',' + theField[i].value; //string together all checked values for cf list delim by comma
												 } 
											 } //end for 
											 if(fieldValue.length > 0) { //remove the last comma we put at the end 
												if(fieldValue.substring(0,1)==',') fieldValue=fieldValue.substring(1, fieldValue.length);
											 } 				
											 okqrystring=1;  	 
									}
									else if(theField.checked) {  
											fieldValue = theField.value;
											okqrystring=1;												 
									}   
							}
							else if(TCS== 'S') { 
								fieldValue = theField[theField.selectedIndex].value;
								okqrystring=1;  
							}
							else { // T
									// T might be fvID: 60: QtyList! 
									fvID = idParseArray[nbrParts-2]; // next-to last element is fvID (needed for QtyLists)
									if(fvID==60) {
										thisQtyListVar = theField.id + "_fieldNames";  
										if(window[thisQtyListVar] && window[thisQtyListVar] > ' ' ) {  
											  // I need the value of each field name
											qLarr = window[thisQtyListVar].split('~');	 // i have 3 separately-named fields with 3 separate values  
											console.log("in addingajax.js / sFormFull: QtyList field arr length is: " + qLarr.length); 
											for(var d=0; d < qLarr.length; d++) {
												qLfield = eval("theForm."+qLarr[d]);											
												if(qLfield) {// should be!
													 qry=qry + '&'+qLarr[d]+'='+encodeURIComponent(qLfield.value); // should be an integer
													 okqrystring=0;// makes it so it will not add this to qry again below
												}
											}
										} 
									}
									else { // T field, not QtyList
										fieldValue = theField.value; 
										okqrystring=1;
									}
							}   
						} // prior field id
					} 
					else { // no field id
						TCS='T'; // for hidden "TEXT" fields with no id
						fieldValue = theField.value; 
						okqrystring=1;
					}					
					if(okqrystring) { // this accomodates radio buttons, so they are submitted only once
						var addto='&'+FormField_Name+'='+encodeURIComponent(fieldValue);
						if(TCS!='C' || qry.indexOf(addto)==-1) qry=qry + addto;//fields submitted add all checkbox fields and ones not already in the query
					}	 
					if(FormField_Name=="returnToURL") {
						url=fieldValue;	 
					}
			}//for f   
			if(!url) url=unescape(location.href); 
			var posu = url.indexOf('#');//get the url before the #formTop anchor
			if(posu>0) url=url.substring(0,posu); 
			if(url.substring(0,4)=='http') {
					//I have a good url, so now create muddle field:
					var currentTime = new Date();
					var month = currentTime.getMonth() + 1; //Add 1 to the month variable to correct the problem with January being 0 and December being 11.
					var day = currentTime.getDate();
					var year = currentTime.getFullYear(); 
					month+='';//convert to string
					day+='';//convert to string
					year+='';//convert to string			
					if(month.length==1) month="0"+month;
					if(day.length==1) day="0"+day;
					dayd=(day*1)+13;//make it an integer, then add 13 days to it
					day=dayd+'';//set it back to string
					if(day.length==1) day="0"+day;//ensure day still has 2 characters
					if(year.length==4) year=year.substring(2);//0,1,2,3 ... to the rest of the string e.g. 2010 = 10 (If 2nd parm is omitted, it extracts the rest of the string)			
					var nowd1 = month+day+year+''; //MMDDYY, all with leading zeroes, '' makes string		
					//if(isK) console.log("MMDDYY is: " + nowd1); 
					nowd =(nowd1*1)+ id; // the *1 makes it an integer
					MuddledFieldcalc=nowd; 
					//if(isK ||1) console.log("MudC is: " + MuddledFieldcalc);
					MuddledField ="l" + MuddledFieldcalc;
					MuddledValue=((MuddledFieldcalc * id) - 31); 
					//if(isK||1) console.log("MudV is: " + MuddledValue);
					qry=qry + '&'+MuddledField+'='+MuddledValue;//muddle field		 
					if(url.indexOf('?')==-1) url = url+'?'+urlqry; 
					else url = url+'&'+urlqry;	 
					if(isK&&1) console.log("posting to url: " + url + " with formfileds: " +qry); 
					xmlhttp.open('POST', url, true);// I'm posting it to their exact url, where the Top php or cfm code will then submit it to form hiddepost using appURL
					xmlhttp.onreadystatechange = fillGivenFormSpace; 
					xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');   // makes this a post  
					xmlhttp.send(qry);// this is the one containing the form fields 
			}
			else {// not good url
					if(isK&&0) console.log(" no url to post to"); 
					var E = getContainer(errSpace);
					if(E) { 
						showSpace(errSpace);
						E.innerHTML = "Invalid URL. Please refresh the page and start again. Thank you."; 
						hideSpace(loadingSpace);//disable the preloading wheel  
						showSpace(submitButtonSpace);//enable the submit button  
					}
					else  {console.log("Invalid URL.\nRefresh this web page and try again.\nThank you.");}		 
			}// good url
	}	
	else window.location = '#formTop'+id;  //go to top of form so we can see the returned message 
	return false;  
} 
function getNewCaptchaImage(cspace,n) {   // for FORM pages that get captcha from their own url
	if(!xmlhttp) xmlhttp = aaGetXmlHttpRequest();
	if(!xmlhttp) return; 
	if(!cspace) return;  
	loadSpace=cspace; 
	errSpace=loadSpace; 
	formHasSmoke=false; // global
	formHas2014Smoke=false;// global
	var url = unescape(location.href);  
	var pos = url.indexOf('#');//get the url before the #formTop anchor
	if(pos>0) url=url.substring(0,pos); 
	if(url.indexOf('?')==-1) url=url+'?'+'w3cp=1&n='+n;
	else url=url+'&'+'w3cp=1&n='+n;  
	//ajaxPreloaderForm(loadSpace,'center',1,'bar');  
	xmlhttp.open('GET', url, true); 
	xmlhttp.onreadystatechange = fillGivenFormSpace;
	xmlhttp.send(null);  
	return false; 
}  
function getUsernameAvail(ciid,optid,username) {   
// currently only on Payment forms on W3 Site Builder
	if(!xmlhttp) xmlhttp = aaGetXmlHttpRequest();
	if(!xmlhttp) return;  
	if(!ciid || ciid < 0 || ciid > 99999999) return;
	if(!optid || optid < 0 || optid > 99999999) return;
	if(!username) return;
	loadSpace=optid+"UsernameAvail";
	errSpace=loadSpace; 
	if(getContainer(errSpace)) {
		getContainer(errSpace).style.display="none";
		if(username.length < 4) {
			getContainer(errSpace).innerHTML="<span style=\"background-color:red;color:yellow;\">'"  + username + "' too short. Use 4 or more characters.</span>";
			getContainer(errSpace).style.display="block";
			return;
		} 
	}
	var url = unescape(location.href);
	var pos = url.indexOf('#');//get the url before any # anchor
	if(pos>0) url=url.substring(0,pos); 
	if(url.indexOf('?')==-1) url=url+'?'+'do=getUsernameAvail&optid='+optid+'&ciid='+ciid+'&username='+encodeURIComponent(username);
	else url=url+'&'+'do=getUsernameAvail&optid='+optid+'&ciid='+ciid+'&username='+encodeURIComponent(username);
	//ajaxPreloaderForm(loadSpace,'left',1,'bar');  
	xmlhttp.open('GET', url, true); 
	xmlhttp.onreadystatechange = fillGivenFormSpace;
	xmlhttp.send(null);  
	return false; 
} 
function loadAdjust(id) {  
	var w=0;
	var Lc = getContainer("tdFarLeft"+id); 
	var Rc = getContainer("tdFarRight"+id);   
	if(Lc&&Rc) {// calc width of loading space, message and spacer image						
		var leftpos=Lc.offsetLeft * 1;//*1 makes it a number
		var rightpos = Rc.offsetLeft * 1;//*1 makes it a number
		w = rightpos - leftpos - 7;	// 6 for cellpadding of 3 on each side and 1 for good measure
		if(w>0) {
			if(w>600) w=600;  
			if(w<100) w=100;
			var c=getContainer("divLoadingSpace"+id); // for wheel 
			if(c) c.style.width = w+"px";  
			c=getContainer("divMessageSpace"+id);  
			if(c) c.style.width=w+"px";
			c=getContainer("imgSpacer"+id);  
			if(c) c.style.width=w+"px"; 
		}  
		var Fc = getContainer("divFormSpace"+id); 
		if(Fc) {// constricts the big description and the err space
			w = Rc.offsetLeft - Lc.offsetLeft - 2;
			 if(w>0) {
			 	if(w>600) w=600;  
				if(w<100) w=100;
			 	Fc.style.width=w+"px";	
			}//w
		}//Fc
	}//LcRc 
	if(isK&&0) console.log("adjusted");
}