// JavaScript Document

function CLFeatureModuleData ( ) {
	this.createRequestObject = function ( ) {
		var xmlhttp;
		try { xmlhttp = new ActiveXObject( "Msxml2.XMLHTTP" ); }
	  		catch(e) {
	    try { xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );}
	    	catch(f) { xmlhttp=null; }
	  	}
	  	if( !xmlhttp&&typeof XMLHttpRequest != "undefined" ) {
	  		xmlhttp = new XMLHttpRequest();
	 	}
		this.__xmlHttp = xmlhttp;
	}
	
	this.sendRequestXmlPost = function ( ) {		
		try{
			var requestObject = this.__xmlHttp;
			
			this.__xmlHttp.open('POST',  'saveConfig.php');
			this.__xmlHttp.setRequestHeader('Content-Type', "application/x-www-form-urlencoded");
			this.__xmlHttp.onreadystatechange = function(){
				try{
					if( requestObject.readyState == 4 &&  requestObject.status == 200 ){
						var response = requestObject.responseText;
						//alert(response);
						document.getElementById("saveResults").innerHTML = 'Changes Saved!';
						updateSWFObject();
					}
				}
				catch(e){
					}
				finally{}
			}
			this.__xmlHttp.send( this.__outputXML );
		}
		catch(e){
		}
		finally{
		}
	}
	
	this.createXMLDOM = function ( __form ) {
		
		this.__outputXML = "__featureHeading="+__form.elements[ '__featureHeading' ].value;
		this.__outputXML += "&__featureURL[0]="+__form.elements[ '__featureURL[0]' ].value;
		this.__outputXML += "&__featureImage[0]="+__form.elements[ '__featureImageHidden[0]' ].value;
		this.__outputXML += "&__featureTitle[0]="+__form.elements[ '__featureTitle[0]' ].value;
		this.__outputXML += "&__featureURL[1]="+__form.elements[ '__featureURL[1]' ].value;
		this.__outputXML += "&__featureImage[1]="+__form.elements[ '__featureImageHidden[1]' ].value;
		this.__outputXML += "&__featureTitle[1]="+__form.elements[ '__featureTitle[1]' ].value;
		this.__outputXML += "&__featureURL[2]="+__form.elements[ '__featureURL[2]' ].value;
		this.__outputXML += "&__featureImage[2]="+__form.elements[ '__featureImageHidden[2]' ].value;
		this.__outputXML += "&__featureTitle[2]="+__form.elements[ '__featureTitle[2]' ].value;
		this.__outputXML += "&__featureURL[3]="+__form.elements[ '__featureURL[3]' ].value;
		this.__outputXML += "&__featureImage[3]="+__form.elements[ '__featureImageHidden[3]' ].value;
		this.__outputXML += "&__featureTitle[3]="+__form.elements[ '__featureTitle[3]' ].value;
		this.__outputXML += "&__featureURL[4]="+__form.elements[ '__featureURL[4]' ].value;
		this.__outputXML += "&__featureImage[4]="+__form.elements[ '__featureImageHidden[4]' ].value;
		this.__outputXML += "&__featureTitle[4]="+__form.elements[ '__featureTitle[4]' ].value;

		return this.__outputXML;
	}
	
	this.ParseXMLConfig = function( __inputFile ){

		
		this.__xmlHttp.onreadystatechange = function(){
			__handleInputXML();
		}
		
		this.__xmlHttp.open( "GET", __inputFile, true );
		this.__xmlHttp.setRequestHeader( 'Content-Type', "text/xml" );
		this.__xmlHttp.send( null );
	}
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	this.__xmlHttp = null;	
	this.createRequestObject( );	
}

function __handleInputXML(){	
	
	if( featureConfigurator.__xmlHttp.readyState == 4 ){

		try{			
			var form = document.featureDataEntry.elements;
			
			if( featureConfigurator.__xmlHttp.responseXML.firstChild.nodeName == 'xml')
			var resp = featureConfigurator.__xmlHttp.responseXML.documentElement;
			else
			var resp = featureConfigurator.__xmlHttp.responseXML.firstChild;

			var itemCount = 0;
			
			for( i = 0; i < resp.childNodes.length; i++ ){
				
				switch( resp.childNodes[i].nodeName ) {
					
					case 'heading':
						form.__featureHeading.value = resp.childNodes[i].firstChild.nodeValue;
					break;
					
					case 'link':							
						form['__featureURL['+itemCount+']'].value 	= resp.childNodes[i].attributes[0].nodeValue;
						var imageParts = resp.childNodes[i].attributes[1].nodeValue.split( '/' );
						form['__featureImageHidden['+itemCount+']'].value = imageParts[ imageParts.length-1 ];
						form['__featureTitle['+itemCount+']'].value = resp.childNodes[i].firstChild.nodeValue;
						itemCount++;
					break;
					
				}
			}		
		}
		catch(e){
		}
		finally{
			
		}
	}	
}



function submitForm( __formName ) {
	
	featureConfigurator.createXMLDOM( __formName );
	featureConfigurator.sendRequestXmlPost( );	
	
	return false;
}

function updateSWFObject(){	
	var container = document.getElementById( 'featureSWFContainer' );

	container.innerHTML = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" width=\"631\" height=\"244\" id=\"featureModuleSWF\" align=\"middle\">";
	
	container.innerHTML += "<param name=\"allowScriptAccess\" value=\"sameDomain\" />";
	container.innerHTML += "<param name=\"movie\" value=\"m2.swf\" />";
	container.innerHTML += "<param name=\"loop\" value=\"false\" />";
	container.innerHTML += "<param name=\"quality\" value=\"high\" />";
	container.innerHTML += "<param name=\"scale\" value=\"noscale\" />";
	container.innerHTML += "<param name=\"wmode\" value=\"transparent\" />";
	container.innerHTML += "<param name=\"bgcolor\" value=\"#ffffff\" />";

	container.innerHTML += "<embed src=\"m2.swf\" loop=\"false\" quality=\"high\" scale=\"noscale\" wmode=\"transparent\" bgcolor=\"#ffffff\" width=\"631\" height=\"250\" name=\"featureModuleSWF\" align=\"middle\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />";
	container.innerHTML += "</object>";
	
	featureConfigurator.ParseXMLConfig( 'featuresConfig.xml' );
}


var featureConfigurator = new CLFeatureModuleData();

