var ofsId;
var name='';
var email='';
var phone='';
var strHref = window.location.href;
var siteUrl = "";
var siteName = "";
getDomain(strHref);
var isGWP = "false";
if(errorPage == null || errorPage == "") {
	errorPage = "/siteValidator/error.jsp?siteName=" + siteName;
}

	new Ajax.Request('/siteValidator/validate', {method:'get', parameters: {siteUrl: siteUrl, siteName: siteName},
	onSuccess: function(transport) {
		var response = transport.responseText;
        var doc;
        try { // IE
            doc=new ActiveXObject("Microsoft.XMLDOM");
            doc.async=false;
            doc.loadXML(response);
           } catch(e) {
            //Firefox, Mozilla, Opera, etc.
              doc=  new DOMParser().parseFromString(response,"text/xml");
        }
        var valid = doc.getElementsByTagName("valid")[0].childNodes[0].nodeValue;

        if(siteUrl == "forwardToWWWVersion") {
            isGWP = "false";
//            window.location.href = wwwPage;
		} else if(valid == 'NO') {
			window.location.href = errorPage;
		} else {
			isGWP = "true";
	    try 
            {
            ofsId = doc.getElementsByTagName("cust_id")[0].childNodes[0].nodeValue;
	    }
            catch(err)
	    {
		ofsId = 'error';
	    }
	    if(ofsId == null)
		ofsId = 'error';

            name = doc.getElementsByTagName("name")[0].childNodes[0].nodeValue;
            if(name == null) name='';
            email = doc.getElementsByTagName("email")[0].childNodes[0].nodeValue;
            if(email == null) email ='';
            phone = doc.getElementsByTagName("phone")[0].childNodes[0].nodeValue;
            if(phone == null) phone='';
        }
	},  onFailure: function() {
		window.location.href = errorPage;
}   });



function getDomain(thestring) {
	if(thestring.indexOf("//www") != -1) {
		var thenewstring = thestring.substring(thestring.indexOf("www.") + 4);

		if(thenewstring.indexOf(".com/") != -1) {
			thenewstring = thenewstring.substring(0, thenewstring.indexOf(".com/") + 4);
		}

		var currentTagTokens = thenewstring.split(".");

		if(currentTagTokens.length > 2) {
			siteName = thenewstring.substring(0, thenewstring.indexOf("."));
			siteUrl = thenewstring.substring(thenewstring.indexOf(".") + 1, thenewstring.length);
		} else {
			siteUrl = "forwardToWWWVersion";
		}
	} else if(thestring.indexOf("//test") != -1) {
		var thenewstring = thestring.substring(thestring.indexOf("test.") + 5);

		if(thenewstring.indexOf(".com/") != -1) {
			thenewstring = thenewstring.substring(0, thenewstring.indexOf(".com/") + 4);
		}

		var currentTagTokens = thenewstring.split(".");

		if(currentTagTokens.length > 2) {
			siteName = thenewstring.substring(0, thenewstring.indexOf("."));
			siteUrl = thenewstring.substring(thenewstring.indexOf(".") + 1, thenewstring.length);
		} else {
			siteUrl = "forwardToWWWVersion";
		}
	} else {
		var thenewstring = thestring.substring(thestring.indexOf("//") + 2);
		if(thenewstring.indexOf(".com/") != -1) {
			thenewstring = thenewstring.substring(0, thenewstring.indexOf(".com/") + 4);
		}

		var currentTagTokens = thenewstring.split(".");

		if(currentTagTokens.length > 2) {
			siteName = thenewstring.substring(0, thenewstring.indexOf("."));
			siteUrl = thenewstring.substring(thenewstring.indexOf(".") + 1, thenewstring.length);
		} else {
			siteUrl = "forwardToWWWVersion";
		}
	}
}

function goToShop() {
	if(ofsId == "error") {
		window.location.href = 'http://www.nsedreams.com/includes/error.html';
	} else {
		window.location.href = 'http://www.nsedreams.com/pwp/productSelection.do?pwpID=' + ofsId+ '&textCacheLocale=' + lang + '_' + countryCode;
	}
}

function goToDonate() {
	if(ofsId == "error") {
		window.location.href = 'http://www.nsedreams.com/includes/error.html';
	} else {
		window.location.href = 'http://www.nsedreams.com/pwp/productInfo.do?pwpID=' + ofsId+ '&textCacheLocale=' + lang + '_' + countryCode + '&prodId='+sku; 
	}
}

function goToSignup() {
	if(ofsId == "error") {
		window.location.href = 'http://www.nsedreams.com/includes/error.html';
	} else {
		window.location.href = 'http://www.nsedreams.com/pwp/onlineDA.do?pwpID=' + ofsId+  '&textCacheLocale=' + lang + '_' + countryCode;
	}
}
function goToOpportunity() {
	if(ofsId == "error") {
		window.location.href = 'http://www.nsedreams.com/includes/error.html';
	} else {
		window.location.href = 'http://www.nsedreams.com/pwp/video.do?type=opportunity&pwpID=' + ofsId+'&name='+name+'&email='+email+'&phone='+phone+ '&textCacheLocale=' + lang + '_' + countryCode;
	}
}

function goToHomePage() {
                 window.location.href = 'http://'+siteName+'.nsedreams.com/';
}

