var ns4 = (document.layers) ? true:false;
var ie4 = (document.all) ? true:false;
var ns6 = (document.getElementById) ? true:false;

function getChannel() {

	var doc = document.adpicker;

	var channel = doc.channel.options[doc.channel.selectedIndex].value;
	
	var link = location.href.split( /\?/, 2 );

	location.href = link[0]+"?channel="+escape(channel);

	return 1;

}//getChannel
function getAdCode() {

	var doc = document.adpicker;

	if( doc.adtype.value == 0 ) {

		getBannerCode();

	} else if( doc.adtype.value == 1 ) {

		getHTMLEmail();

	} else if( doc.adtype.value == 2 ) {

		getTextLink();

	}

}// getAdCode()
function getTextLink() {
        var doc = document.adpicker;
                 
        var descript = doc.adsize.options[doc.adsize.selectedIndex].value;
                 
        var linedata = getLine( descript );
                 
        var position = linedata[0];
                 
        var lineid = linedata[1];


	var href = "http://affiliates.x10.com/?property=affiliates&aposition="+position+"&lineid="+lineid+"&affid="+affid+"&type=href";

	var img = "http://affiliates.x10.com/?property=affiliates&aposition="+position+"&lineid="+lineid+"&affid="+affid+"&type=img";

	var html = "<a href=\""+href+"\" target=\"_blank\">"+descript+"</a><img src=\""+img+"\">";
	
        doc.code.value = html;

        ChgInnerDiv("display",null,html);

}//getTextLink()
function getHTMLEmail() {

	var doc = document.adpicker;

        var file = doc.adsize.options[doc.adsize.selectedIndex].value;

      	var channel = doc.channel.options[doc.channel.selectedIndex].value;
                                                                                                                                                             
        var link = location.href.split( /\?/, 2 );
                                                                                                                                                             
        location.href = link[0]+"?channel="+escape(channel)+"&filename="+escape(file);

}// getHTMLEmail()
function getBannerCode() {

        var doc = document.adpicker;
                                                                                                                                                             
        var descript = doc.adsize.options[doc.adsize.selectedIndex].value;
                                                                                                                                                             
        var linedata = getLine( descript );
                                                                                                                                                             
        var wh = linedata[0].split( 'x', 2 );
                                                                                                                                                             
        var lineid = linedata[1];
                                                                                                                                                             
        /*
         * Build code for both Javascript Version and HTML Version the html version show up
         * display box so go head and make both Version.
         */
                                                                                                                                                             
        // Javascript Version
                                                                                                                                                             
        var js = getJsVersion( linedata[0], wh, lineid );
                                                                                                                                                             
        //HTML Version
        var html = getHTMLVersion( linedata[1], wh, lineid );
                                                                                                                                                             
        //Get Code Type for code
        var tc = getCodeType( doc );
                                                                                                                                                             
        // Decide what code to return
        if( tc == 1 ) {
                                                                                                                                                             
                doc.code.value = js;
        
        } else if(  tc == 2 ) {
        
                doc.code.value = html;
        }
         
        //Display the ad
        ChgInnerDiv("display",null,html);

}// getBannerCode()
function getJsVersion( size, wh, lineid ) {

	var js = '<script type="text/javascript" language="JavaScript">\n';
	js += 'var a = "'+affid+'";\n';
	js += 'var s = "'+size+'";\n';
	js += 'var w = '+wh[0]+';\n';
	js += 'var h = '+wh[1]+';\n';
	js += 'var l = "'+lineid+'";\n';
	js += 'var d = new Date();\n';
	js += 'var r = d.getTime();\n';

	js += 'var m = \'<a href="http://affiliates.x10.com/?\';\n';
	js += 'm += \'property=default\';\n';
	js += 'm += \'&aposition=\'+s;\n';
	js += 'm += \'&lineid=\'+l;\n';
	js += 'm += \'&affid=\'+a;\n';
	js += 'm += \'&type=href\';\n';
	js += 'm += \'&rnd=\'+r;\n';
	js += 'm += \'" target="_blank">\';\n';
	    
	js += 'm += \'<img src="http://ads.x10.com/?\';\n';
	js += 'm += \'property=default\';\n';
	js += 'm += \'&aposition=\'+s;\n';
	js += 'm += \'&lineid=\'+l;\n';
	js += 'm += \'&affid=\'+a;\n';
	js += 'm += \'&type=img\';\n';
	js += 'm += \'&rnd=\'+r;\n';
	js += 'm += \'" border="0"\';\n';
	js += 'm += \'width="\'+w;\n';
	js += 'm += \'" height="\'+h;\n';
	js += 'm += \'">\';\n';
	js += 'm += \'</a>\';\n';
	js += 'document.writeln(m);\n';
	js += '</scr'+'ipt>\n';

	return js;

}// getJsVersion()
function getHTMLVersion( size, wh, lineid ){

	var html = "<a href=\"http://affiliates.x10.com/?property=affiliates";
	html += "&aposition="+size+"&lineid="+lineid+"&affid="+affid+"&type=href\">\n";
	html += "<img src=\"http://affiliates.x10.com/?property=affiliates";
	html += "&aposition="+size+"&lineid="+lineid+"&affid="+affid+"&type=img\" ";
	html += "width=\""+wh[0]+"\" height=\""+wh[1]+"\" border=\"0\"></a>\n";

	return html;
	
}//getHTMLVersion()
function getCodeType( doc ) {

	var tc = 0;

	for( x = 0; x < doc.typecode.length; x++) {

			if( doc.typecode[x].checked ) {

				tc = doc.typecode[x].value;

			}

	}

	return tc;

}// getCodeType()
function getLine( descript ) {
	
	var x = 0;
	
	var data = new Array();
	
	for( x = 0; x < Ads.length; x++ ) {
		
		if( descript == Ads[x][0] ) {
		
			data[0] = Ads[x][1];

			data[1] = Ads[x][2];

			return data;
		
		}
		
	}
	
	return 0;

}// getLine()
function ChgInnerDiv(TextArea,nestLyr,Text) {
       
       if(ns4) {
       
                var docLyr = (nestLyr) ? eval('document.'+nestLyr+'.document.'+TextArea+'.document') : document.layers[TextArea].document;
       
		docLyr.open();

		docLyr.write(Text);
                
		docLyr.close();

	} else if(ie4) {

		document.all[TextArea].innerHTML = Text;
		
        } else if(ns6) {

                document.getElementById(TextArea).innerHTML = Text;
	
	}

	return 1;

}//ChgInnerDiv();
