var lm_Domain = "http://www.leadmaster.com/login/"
//var lm_Domain = "http://qa.leadmaster.com/"

function lm_SubmitWebFormAjax(CompanyID, FormID) {
    if (window.XMLHttpRequest) { 
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { 
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        alert('Cannot create an XMLHTTP instance');
        return false;
    }

	var this_url;
	this_url = "http://www.leadmaster.com/login/custom_form_getkey.asp?lm_CompanyID=" + CompanyID + "&lm_FormID=" + FormID

    http_request.open('GET', this_url, true);
	http_request.onreadystatechange=function() {
		if (http_request.readyState==4) {
			// get the form key element
			number_of_forms = document.forms.length;
			for (i = 0; i < number_of_forms; ++i) {
				if (document.forms[i].lm_FormKey != null) {
					document.forms[i].lm_FormKey.value = http_request.responseText;
					//document.forms[i].action = "http://www.leadmaster.com/login/custom_form_process.asp"
					document.forms[i].action = "http://64.132.240.176/login/custom_form_process.asp"
					document.forms[i].submit();
					break;
				}
			}
		}
	}

    http_request.send(null);
	return false;
}

function lm_SubmitWebForm() {
	// get the form key element
	number_of_forms = document.forms.length;
	for (i = 0; i < number_of_forms; ++i) {
		if (document.forms[i].lm_FormKey != null) {
			document.forms[i].action = lm_Domain + "custom_form_process.asp"
			document.forms[i].submit();
			break;
		}
	}
	return false;
}

// Constructor -- pass a REST request URL to the constructor
//
function lm_JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl; 
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'JscriptId' + lm_JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
lm_JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
lm_JSONscriptRequest.prototype.buildScriptTag = function () {
    // Create the script tag
    this.scriptObj = document.createElement("script");
    
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
// removeScriptTag method
lm_JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
}

// addScriptTag method
lm_JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}

// the callback function
function lm_SetFormKey(formKey) {     
	// get the form key element
	number_of_forms = document.forms.length;
	for (i = 0; i < number_of_forms; ++i) {
		if (document.forms[i].lm_FormKey != null) {
			document.forms[i].lm_FormKey.value = formKey;
			break;
		}
	}
	bObj.removeScriptTag(); 
}

// called by the body onload funtion to get the form key
function lm_InitWebForm() {
	// make the web service call to get the form key
	var lm_CompanyID = '0'
	var lm_FormID = '0'
	// get the form key element
	number_of_forms = document.forms.length;
	for (i = 0; i < number_of_forms; ++i) {
		if (document.forms[i].lm_CompanyID != null) {
			lm_CompanyID = document.forms[i].lm_CompanyID.value;
		}
		if (document.forms[i].lm_FormID != null) {
			lm_FormID = document.forms[i].lm_FormID.value;
		}
	}
	var req = lm_Domain + 'custom_form_getkey.asp?lm_CompanyID=' + lm_CompanyID + '&lm_FormID=' + lm_FormID
	// create a new request object
	bObj = new lm_JSONscriptRequest(req); 
	// build the dynamic script tag
	bObj.buildScriptTag(); 
	// add the script tag to the page
	bObj.addScriptTag();
}     

