﻿
// Javascript File for Proof of Performance DIV and its functions

    function validEmail(sEmail) {
        return (testEmail(sEmail) == true)?"valid":"invalid";
    }
    function testEmail(sEmail) {
	    var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	    return re.test(sEmail);
    }
    function validPhoneNumber(s) {
        return (testDigits(s, 10) == true)?"valid":"invalid";
    }
    function testDigits(s, lngth) {
        var re = new RegExp("^\\d{" + lngth + "}");
        return re.test(s);
    }
    function submitForm() {
        fname=document.getElementById("txtName");
        company=document.getElementById("txtCompany");
        email=document.getElementById("txtEmail");
        areaCode=document.getElementById("txtAreaCode");
        phonePrefix=document.getElementById("txtPhonePrefix");
        phoneFour=document.getElementById("txtPhoneFour");
        phoneExtension=document.getElementById("txtPhoneExtension");
        callTime=document.getElementById("txtCallTime");

        var s1 = "Name: " + fname.value;
        s1 += "\nCompany: " + company.value;
        s1 += "\n"  + "Email is " + validEmail(email.value) + ":" + email.value;
        s1 += "\n" + "Phone is " + validPhoneNumber(areaCode.value + phonePrefix.value + phoneFour.value) + ":(" + areaCode.value + ") " + phonePrefix.value + "-" + phoneFour.value + ", Ext: " + phoneExtension.value;
        s1 += "\nBest Time: " + callTime.value;
        
        var s="";
        if (String(fname.value).length < 2) s += "Please provide your name.\n";
        if (String(company.value).length < 3) s += "Please provide your company name.\n";
        if (testEmail(String(email.value)) != true) s += "Please enter correct email address.\n"
        if (testDigits(String(areaCode.value), 3) != true) s += "Please provide your area code.\n"
        if (!testDigits(String(phonePrefix.value), 3) || !testDigits(String(phoneFour.value), 4)) s += "Please correct/provide your phone number.\n"
//alert("here");        
        if (s.length > 0) {
            s = "Free Trial !!\n\nThere is a problem with your data.\n\n" + s ;
        } else {
            s = "Free Trial !!\n";
            s += "\nThank you, " + String(fname.value) + ".\n";
            s += "\nOur experts will contact you within one business day.\n";
            s += "We will set up your Free Trial!\n";
            s += "\nRonn Schuman, CEO";
            s += "\nConnectyx Technologies Inc.";
            s += "\n\n" + s1
            togglepopDIV(325);

            var handleSuccess = function(o) {
                fname.value="";
                company.value="";
                email.value="";
                areaCode.value="";
                phonePrefix.value="";
                phoneFour.value="";
                phoneExtension.value="";
                callTime.value="";

                //if(o.responseText !== undefined){ 
                //    var sResult = "Transaction id: " + o.tId; 
                //    sResult += "HTTP status: " + o.status; 
                //    sResult += "Status code message: " + o.statusText; 
                //    sResult += "<li>HTTP headers: <ul>" + o.getAllResponseHeaders + "</ul></li>"; 
                //    sResult += "response: " + o.responseText; 
                //    sResult += "Argument object: " + o.argument;
                //    alert(sResult); 
                //} 
            }; 
            var handleFailure = function(o) {
                alert("failure: " + o.getAllResponseHeaders);
            };	 
            var args = ['foo','bar']; 
            var callback = { 
                success:handleSuccess, 
                failure: handleFailure,
                argument: args 
            };
	 
            var postData = "fullName=" + encodeURIComponent(String(fname.value));
            postData += "&companyName=" + encodeURIComponent(String(company.value));
            postData += "&eMailAddress=" + encodeURIComponent(String(email.value));
            postData += "&phoneNumber=" + encodeURIComponent("(" + String(areaCode.value) + ") " + String(phonePrefix.value) + "-" + String(phoneFour.value) + ", Ext: " + String(phoneExtension.value));
            postData += "&bestTime=" + encodeURIComponent(String(callTime.value));
alert("postData = " + postData);

alert("window.location.host = " + window.location.host);

            //Build URL to Service:
            //var sHost = "http://" + window.location.host;
            //var sHost = "http://MRNmanager.com";
            //var sHost = "http://208.38.149.52"
            var sHost = "/";
alert("host = " + sHost);
            var sURL = "services/LogContact.asmx/LogContact";
            if (sHost.indexOf("localhost") >= 0) sURL = "/mrnManager" + sURL;
            sURL = sHost + sURL;

//sURL="http://mrnmanager.IQwizard.com/services/LogContact.asmx/LogContact"
alert("URL = " + sURL);

//            var sURL = "http://" + window.location.host + "/services/LogContact.asmx/LogContact";
//sURL = "http://mrnmanager.iqwizard.com/services/LogContact.asmx/LogContact";
//alert(sURL);
            YAHOO.util.Connect.initHeader("Content-Type", "application/x-www-form-urlencoded");
            YAHOO.util.Connect.initHeader("Host", sHost);

//alert("http://" + window.location.host);

            //var request = YAHOO.util.Connect.asyncRequest('POST', sURL, callback, postData);
            YAHOO.util.Connect.asyncRequest('POST', sURL, callback, postData);
        }
        alert(s);
        return false;
    }
 
    var popDIV_visible = "hidden";
    function togglepopDIV(offsetY) {
        popDIV_visible = (popDIV_visible == "visible")?"hidden":"visible";
        var obj = document.getElementById('popDIV');
        switch (popDIV_visible) {
            case "hidden":
                obj.style.visibility = "hidden";
            break;
            case "visible":
                //alert(offsetY);
                var offsetX = parseInt(2 * findCenter()- obj.offsetWidth);
                obj.style.left = offsetX + "px" ;
                obj.style.top = offsetY + "px";
                obj.style.visibility = "visible";
                sweepLeft("popDIV", offsetX, findCenter() - parseInt(obj.offsetWidth / 2), 75, 50, obj.offsetWidth);
            break;
            default:
            break;
        }

        //obj.style.visibility = popDIV_visible;
    }

    function inputFocus(t) {
        //alert(t.offsetWidth);
        t.style.color = "#000";
        t.style.backgroundColor = "#ffffc0";
        //t.style.border = "solid 1px #40FF40";
        //t.style.border-top = "solid 1px #404040";
        //t.style.border-right = "solid 1px #808080";
        //t.style.border-bottom = "solid 1px #808080";
        //t.style.border-left = "solid 1px #404040";
    }
    
    function inputBlur(t){
        //alert(t.id);
        t.style.color = "#404040";
        t.style.backgroundColor = "#fff";
        //t.style.border = "solid 1px #fff";
    }
function findTop(obj) {
	var curTop = 0;
	if (obj.offsetParent) {
		curTop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curTop += obj.offsetTop;
		}
	}
	return curTop;
}

function findLeft(obj) {
	var curLeft = 0;
	if (obj.offsetParent) {
		curLeft = obj.offsetLeft;
		while (obj = obj.offsetParent) {
			curLeft += obj.offsetLeft;
		}
	}
	return curLeft;
}
function findLeftFromID(sID) {return findLeft(document.getElementById(sID));}

function findCenter() {
    var o = document.getElementById("containerDIV");
    return findLeft(o) + parseInt(o.offsetWidth /2);
}

function sweepLeft(objID, xPos, xTarget, xDelta, interval) {
	if (document.getElementById) {
        obj = document.getElementById(objID);
        xPos = xPos - xDelta;
        //alert(xPos + " < " + xTarget);
        if (xPos < xTarget) xPos = xTarget;
        obj.style.left = xPos + "px";
        if (xPos > xTarget) {
            window.setTimeout("sweepLeft('" + objID + "', " + xPos + ", " + xTarget + ", " + xDelta + ", " + interval + ")", interval);
        } else {
            //transitionSlideShow();
        }
    }
}
