function swap(imgIndex) {
    if(imgIndex == 0){
        document.one.src=emptyStar;
        document.two.src=emptyStar;
        document.tree.src=emptyStar;
        document.four.src=emptyStar;
        document.five.src=emptyStar;
    } else {
        switch (imgIndex) {
            case 5 :
                document.five.src=fullStar;
            case 4 :
                document.four.src=fullStar;
            case 3 :
                document.tree.src=fullStar;
            case 2 :
                document.two.src=fullStar;
            case 1 :
                document.one.src=fullStar;
        }
    }
}

function ratingOk(xml) {
    getElementFromName('rating').innerHTML = xml;    // assign the content to the form
}

function rateModel(id, rating) {
    var url = 'rateproduct.do' ;
    var data = 'id=' + id + '&rating=' + rating;
    
    makeHttpRequest(url, 'ratingOk', false, data);
}

function getFormData(name) {  
    return Url.encode(getElementFromName(name).value);
}

function validateEmail(email){
    return (email.indexOf(".") > 0) && (email.indexOf("@") > 0);
}

function mailSent(xml) {
    document.poppedLayer = getElementFromName('mailbox');
    document.poppedLayer.innerHTML = xml;
}

function sendMail(id){
    if(!validateEmail(getElementFromName('email_from').value) || !validateEmail(getElementFromName('email_to').value)) {
        alert("Email addresses must be valid");
        return ;
    }
    
    var data = 'email_from=' + getFormData('email_from') + '&email_to=' + getFormData('email_to') + '&email_name=' + getFormData('email_name') + '&email_message=' + getFormData('email_message') + '&email_id=' + id ;
    makeHttpRequest('SendMail', 'mailSent', false, data);
}

function hideMailDiv() {
    document.poppedLayer = getElementFromName('mailbox');
    document.poppedLayer.style.visibility = "hidden";
    document.poppedLayer.innerHTML = "";
}

function showMailDiv(id) {
    getElementFromName('share').className = '';
    getElementFromName('bookmark').className = '';
    getElementFromName('embed').className = '';
    getElementFromName('mail_tab').className = 'active';
    getElementFromName('link_tab').className = '';
    
    var url = 'mailform.jsp' ;
    var data = 'id=' + id ;    
    makeHttpRequest(url, 'showShareDiv', false, data);
    return false;
}



function showShareDiv(xml) {
    getElementFromName('embed_div').innerHTML = xml;    // assign the content to the form
}

function share(id, name) {
    getElementFromName('share').className = 'active';
    getElementFromName('bookmark').className = '';
    getElementFromName('embed').className = '';
    getElementFromName('mail_tab').className = '';
    getElementFromName('link_tab').className = '';
    
    var url = 'share.jsp' ;
    var data = 'id=' + id + '&name=' + name;     
    makeHttpRequest(url, 'showShareDiv', false, data);
    return false;
}

function bookmark(id, name) {
    getElementFromName('share').className = '';
    getElementFromName('bookmark').className = 'active';
    getElementFromName('embed').className = '';
    getElementFromName('mail_tab').className = '';
    getElementFromName('link_tab').className = '';
    
    var url = 'bookmark.jsp' ;
    var data = 'id=' + id + '&name=' + name;     
    makeHttpRequest(url, 'showShareDiv', false, data);
    return false;
}

function embed(id) {
    getElementFromName('share').className = '';
    getElementFromName('bookmark').className = '';
    getElementFromName('embed').className = 'active';
    getElementFromName('mail_tab').className = '';
    getElementFromName('link_tab').className = '';
    
    var url = 'embedcode.jsp' ;
    var data = 'id=' + id ;    
    makeHttpRequest(url, 'showShareDiv', false, data);
    return false;
}

function link(id, name) {
    getElementFromName('share').className = '';
    getElementFromName('bookmark').className = '';
    getElementFromName('embed').className = '';
    getElementFromName('mail_tab').className = '';
    getElementFromName('link_tab').className = 'active';
    
    var url = 'link.jsp' ;
    var data = 'id=' + id + '&name=' + name;    
    makeHttpRequest(url, 'showShareDiv', false, data);
    return false;
}

function clearTabs() {
    getElementFromName('cu-textures').className = '';
    getElementFromName('cu-background').className = '';
    //getElementFromName('cu-camera').className = '';
    getElementFromName('cu-save').className = '';
}
function setActive(caller) {
    caller.className = 'active';
}

var EMBED_CODE = '<!--[if !IE]>--><object type="application/x-java-applet" archive="MadlixApplet.jar" classid="java:com.agency9.applet.MadlixApplet" width="{width}" height="{height}"> \n' +
' <param name="codebase" value="http://www.madlix.com">\n' +
'  <param name="code" value="com.agency9.applet.MadlixApplet">\n' +
'  <param name="JAVA_CODEBASE" value="http://www.madlix.com">\n' +
'  <param name="archive" value="MadlixApplet.jar" />\n' +
'  <param name="url" value="http://www.madlix.com/preview/{id}.mlx">\n' +
'  <param name="background_color" value="{color}"><!--<![endif]-->\n' +
'  <object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" width="{width}" height="{height}">\n' +
'    <param name="codebase" value="http://www.madlix.com">\n' +
'    <param name="code" value="com.agency9.applet.MadlixApplet">\n' +
'    <param name="archive" value="MadlixApplet.jar" />\n' +
'    <param name="url" value="http://www.madlix.com/preview/{id}.mlx">\n' +
'    <param name="background_color" value="{color}">\n' +
'    <img src="http://www.madlix.com/images/models/{id}-big.jpg" width="{width}" height="{height}">\n' + 
'  </object>\n' +
'<!--[if !IE]>--></object><!--<![endif]-->';

function updateEmbed(id) {
    getElementFromName('embed_code').value = EMBED_CODE.replace(/{width}/g,getElementFromName('embed_w').value).replace(/{height}/g,getElementFromName('embed_h').value).replace(/{color}/g,getElementFromName('embed_bg').value).replace(/{id}/g, id);
}

function previewEmbed() {
    var wid = parseInt(getElementFromName('embed_w').value) + 30;
    var hei = parseInt(getElementFromName('embed_h').value) + 30;
    var win = window.open('','preview','width=' + wid + ' , height=' + hei );
    win.document.body.innerHTML = '';
    win.document.write(getElementFromName('embed_code').value);
}

/**
 *
 *  URL encode / decode
 *  http://www.webtoolkit.info/
 *
 **/
var Url = {
    
    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },
    
    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },
    
    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";
        
        for (var n = 0; n < string.length; n++) {
            
            var c = string.charCodeAt(n);
            
            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            
        }
        
        return utftext;
    },
    
    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
        
        while ( i < utftext.length ) {
            
            c = utftext.charCodeAt(i);
            
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
            
        }
        
        return string;
    }
    
}

function getApplet() {
    var div;
    if (browserType == "gecko" ) div = eval('document.getElementById(\'mozApplet\')');
    else if (browserType == "ie") div = eval('document.all[\'ieApplet\']');
    else div = eval('document.layers[\'`mozApplet\']');
    return div;
}

function callApplet(method) {
    var applet = getApplet();
    applet.call(method);
}

function setBackgroundImage() {
    var applet = getApplet();
    applet.call('setBackgroundImage', getElementFromName('backgroundImage').value);    
}

function setTexture(id) {
    var applet = getApplet();    
    applet.call('setTexture', id , getElementFromName('texture' + id ).value);    
}

function saveModel() {
    var applet = getApplet();    
    applet.call('saveModel', getElementFromName('uploadusername').value, getElementFromName('uploadpassword').value );
}
