var clothes_designer = {

    status: 0, // 0 - not valid; 1 - waiting for validation; 2 - positive validation
    token: null,
    id: null,
    base_url: 'http://designer.routeone-solutions.co.uk/',
    http_request: false,
    width: 0,
    height: 0,
    params: '',
	
    init: function(t, i) {
        clothes_designer.token = t;
        clothes_designer.id = i;
        if(clothes_designer.token == '' || clothes_designer.token == null) return;
        clothes_designer.status = 1;
        clothes_designer.createHTTPRequestObject();
        clothes_designer.validateUser();
    },
	
    createHTTPRequestObject: function() {
        if (window.XMLHttpRequest) {
            clothes_designer.http_request = new XMLHttpRequest();
        } else if (window.ActiveXObject) { // IE
            try {
                clothes_designer.http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    clothes_designer.http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
    },
	
    createXMLStringParser: function(XMLString) {
        try {
            var iosvc = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
            var aboutBlankURI = iosvc.newURI("about:blank", null, null);
            var xmlParser = new DOMParser(null,aboutBlankURI,null);
            var xmlDoc = xmlParser.parseFromString(XMLString, "text/xml");
        } catch(Err) {
            try {
                var xmlDoc= new ActiveXObject("Microsoft.XMLDOM");
                xmlDoc.async="false";
                xmlDoc.loadXML(XMLString);
            } catch(Err) {
                window.alert("Browser does not support XML parsing.");
                return false;
            }
        }
        return xmlDoc;
    },
	
    makeRequest: function(u, p, c) {
        clothes_designer.http_request.onreadystatechange = c;
        clothes_designer.http_request.open('GET', u + p, true);
        //clothes_designer.http_request.setRequestHeader('X-Alt-Referer', clothes_designer.base_url);
        if (window.XMLHttpRequest) {
            clothes_designer.http_request.send();
        } else {
            clothes_designer.http_request.send();
        }
    },
	
    include: function(f) {
        var b = document.getElementsByTagName('body').item(0);
        s = document.createElement('script');
        s.src = f;
        s.type = 'text/javascript';
        b.appendChild(s);
    },
	
	script_back_loaded: function() {
		clothes_designer.status = 2;
	},
	
    validateUser: function() {
        codename = '';
        q = location.href.substring(location.href.indexOf('?')+1);
        q = q.split('&');
        for(var i in q) {
            q[i] = q[i].split('=');
            if(q[i][0] == 'back') {
                codename = q[i][1];
                break;
            }
        }
        if(codename != '') {
            sc = document.createElement('script');
			sc.type = 'text/javascript';
			sc.onreadystatechange= function () {
				if (this.readyState == 'loaded' || this.readyState == 'complete') clothes_designer.script_back_loaded();
			}
            sc.onload = clothes_designer.script_back_loaded;
            sc.src = clothes_designer.base_url+'print_back/'+codename;
            document.getElementsByTagName('body')[0].appendChild(sc);
        } else { 
            clothes_designer.status = 2;
        }
    },
	
    validateUserCallback: function() {
        if (clothes_designer.http_request.readyState != 4 || clothes_designer.http_request.status != 200) return;
        var responseData = clothes_designer.http_request.responseXML;
        if(typeof responseData === 'null' || typeof responseData === 'undefined') {
            clothes_designer.status = 0;
            return;
        }
        validate_status = responseData.getElementsByTagName('status');
        if(validate_status.length == 0) {
            clothes_designer.status = 0;
            return;
        }
        validate_status = validate_status.item(0);
        if(validate_status.childNodes[0].nodeValue == 'not valid' || validate_status.childNodes[0].nodeValue == 'false' || validate_status.childNodes[0].nodeValue == '0') {
            clothes_designer.status = 0;
            return;
        }
        clothes_designer.status = 2;
    },
	
    show_designer: function() {
        if(clothes_designer.status == 1) {
            setTimeout(clothes_designer.show_designer, 500);
            return;
        }
        var obj = document.getElementById(clothes_designer.id);
        if(clothes_designer.status != 2) {
            obj.innerHTML = 'Your account token is not valid. Please contact service administrator.';
            return;
        }
        prop = 900 / 566;
        if(clothes_designer.width == 0) clothes_designer.width = 900;
        clothes_designer.height = Math.round(clothes_designer.width / prop);
        html  = '<!--[if !IE]> -->'+"\n";
        html += '<object type="application/x-shockwave-flash"'+"\n";
        html += '  data="'+clothes_designer.base_url+'clothes.swf" width="'+clothes_designer.width+'" height="'+clothes_designer.height+'">'+"\n";
        html += '<!-- <![endif]-->'+"\n";
        html += ''+"\n";
        html += '<!--[if IE]>'+"\n";
        html += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'+"\n";
        html += '  codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"'+"\n";
        html += '  width="'+clothes_designer.width+'" height="'+clothes_designer.height+'">'+"\n";
        html += '  <param name="movie" value="'+clothes_designer.base_url+'clothes.swf" />'+"\n";
        html += '<!--><!--dgx-->'+"\n";
        html += '  <param name="loop" value="true" />'+"\n";
        html += '  <param name="menu" value="false" />'+"\n";
        html += '  <param name="allowScriptAccess" value="always" />'+"\n";
        par = '';
        if(typeof clothes_designer.params == 'object') {
            par = '&' + clothes_designer.params.params;
        }
        html += '  <param name="flashvars" value="@url='+clothes_designer.base_url+'get/'+clothes_designer.token+par+'" />'+"\n";
        html += ''+"\n";
        html += '  <param name="wmode" value="opaque" />'+"\n";
        html += ''+"\n";
        html += '  <p>You need Flash Player to see this content. You can install it <a href="http://get.adobe.com/flashplayer/">here</a></p>'+"\n";
        html += '</object>'+"\n";
        html += '<!-- <![endif]-->'+"\n";
        obj.innerHTML = html;
        return;
    }

};
