//==================================================================
//	Create Element / DOM Object
//==================================================================


//------------------------------------------------------------------
//	class
//------------------------------------------------------------------
function xMMS_DOM(){
	//--------------------------------------------------------------
	// variables
	//--------------------------------------------------------------
		var PARENT			= "";
		var TYPE			= "";
		var SUBMIT			= "";
		var id				= "";
		var name			= "";
		var aDATA			= "";
		var action			= "";
		var rootPath		= "";
		var xmlFile			= "";
		var jumpToDisplay	= "";
		var jumpToFile		= "";
		var isIE			= false;
	//--------------------------------------------------------------
	// arrays
	//--------------------------------------------------------------
		

	//--------------------------------------------------------------
	// constructor
	//--------------------------------------------------------------
		DOTS = getObject('DOTS');
		MENU = getObject('Menu');
		if(DOTS){MENU.removeChild(DOTS);}

		// set global class level variables
		//----------------------------------------------------------
		this.setGlobals = function(PARENT, TYPE, id, action){
			// IE check
			if(window.ActiveXObject){isIE = true;}

			//	set class vars
			this.PARENT		= getObject(PARENT);
			this.TYPE		= TYPE;
			this.id			= id;
			this.name		= id;
			this.action		= action;
			xmlFile = this.xmlFile;
		}

	//--------------------------------------------------------------
	// methods
	//--------------------------------------------------------------
		//	create the object on the stage to its assigned parent
		//----------------------------------------------------------
		this.addObjDOM	= function(){
			//	usefull alert tracing alert
			//-------------------------------------------
			altTXT = "creating DOM object\n";
			altTXT += "PARENT:" + this.PARENT + "\n"
			altTXT += "TYPE:" + this.TYPE + "\n"
			altTXT += "id:" + this.id + "\n"
			altTXT += "name:" + this.name + "\n"
			/*alert(altTXT);*/

			//	what 'TYPE' of object are we handling
			//	run specific object type code
			//	aDATA now exists in this object
			//---------------------------------------
			switch(this.TYPE){
				case "select":
					this.typeDOM_select();
				break;
			}
		}

		//	SELECT dropdown
		//----------------------------------------------------------
		this.typeDOM_select = function(){
			//	get document object for handling
			//-----------------------------------------------------
			OBJ = getObject(this.name);


			count = this.aDATA.length;
			// create elements and ready for append
			//---------------------------------------------
					if(isIE){
						//	this is for IE
						//	IE 5.5 and 6 cannot append additional data 
						//	at a later state, so a blank one is created for now!
						//---------------------------------------------
						var OBJ			= document.createElement("<" + this.TYPE + " name=" + this.name + " id=" + this.id + " onchange=javascript:" + this.action + ">");
						var DOTS		= document.createElement("<img src='images/dots.gif' style='padding:0px 0px 0px 5px;' name='DOTS' id='DOTS'>");
					} 
					else{
						//	catch all (any other browser)
						//---------------------------------------------
						var OBJ = document.createElement(this.TYPE);
						OBJ.setAttribute("name", this.name);
						OBJ.setAttribute("id", this.id);
						OBJ.setAttribute("onchange", "javascript:" + this.action);

						var DOTS	= document.createElement("img");
						DOTS.setAttribute("src", "images/dots.gif");
						DOTS.setAttribute("style", "padding:0px 0px 0px 5px;");
						DOTS.setAttribute("name", "DOTS");
						DOTS.setAttribute("id", "DOTS");
					}
					//	append the parent with the newly created object
					//	object created is assigned above with ELEMENT
					//	PARENT is assigned in setGlobals
					//-------------------------------------------------
					this.PARENT.appendChild(DOTS);
					this.PARENT.appendChild(OBJ);


			
			
			//	clear old OPTION datat if any previously existed
			//-----------------------------------------------------
			clear = OBJ.options.length;
			for(n = 0; n < clear; n++){OBJ.options[n] = null;}

		
			//	DATA count
			//---------------------------------------
			count = this.aDATA.length;
			
			var addTXT = xmlFile == "CANADA.xml" ? " Province" : " City";

			OBJ.options[0] = new Option("Select" + addTXT, "");
			OBJ.options[1] = new Option("------------------------------------", "");

			//	loop and populate OPTION object
			//---------------------------------------			
			for(n = 0; n < count; n++){
				name	= this.aDATA[n][0];
				url		= this.aDATA[n][1];
				code	= this.aDATA[n][2];
				
				OBJ.options[(n + 2)] = new Option(name,url);
			}
		}

		//------------------------------------------------
		//	Get Object for usage
		//------------------------------------------------
		getObject  = function(name){
			if (document.getElementById){
			   OBJ = document.getElementById(name);
			}
			else if (document.all){
			   OBJ = document.all[name];
			}
			else if (document.layers){
				if (document.layers[name]){
					OBJ = document.layers[name];
				}
			}
			return OBJ;
		}
}

//==================================================================
//	Other DOM and MISC functions
//==================================================================

//------------------------------------------------
//	select on change function
//------------------------------------------------
function checkSelect(OBJ, xmlFile){
	index = OBJ.selectedIndex;
	value = OBJ.options[index].value;
	display = OBJ.options[index].text;

		DOTS = getObject('DOTS');
		MENU = getObject('Menu');
		if(DOTS){MENU.removeChild(DOTS);}

	if(value != null && value != ""){
		// set global values and other variables
		//---------------------------------------
		xNAV.rootPath	= "xml/";
		xNAV.xmlFile	= xmlFile;
		xNAV.setGlobals("Nav", display, value);
		xNAV.addTrail();
	
	
		// remove Original element
		//---------------------------------------
		PARENT = OBJ.parentNode;
		PARENT.removeChild(OBJ);
	
		// set XML FILE path and name
		//---------------------------------------
		xMMS.setGlobals();
		xMMS.xmlFile	= value;
		xMMS.loadXML();
	}
}

//------------------------------------------------
//	Get Object for usage
//------------------------------------------------
function getObject(name){
		if (document.getElementById){
		   OBJ = document.getElementById(name);
		}
		else if (document.all){
		   OBJ = document.all[name];
		}
		else if (document.layers){
			if (document.layers[name]){
				OBJ = document.layers[name];
			}
		}
	return OBJ;
}

//------------------------------------------------
//	Show Hide
//------------------------------------------------
function showHide(name, state){
	alert(name)
	OBJ = getObject(name);
	OBJ.style.visibility	= state == 0 ? "hidden" : "visible" ;
	OBJ.style.display		= state == 0 ? "none" : "block" ;
}

//-------------------------------------------------
// Form check
//-------------------------------------------------
//--------------------------------------------------------------------------------

function setFormCheck(formFields, formName){
	var formFields	= formFields;
	var formName	= formName;

}

function submitForm(name){
	if (document.getElementById){
		OBJ = document.getElementById(name).submit();
	}
	else if (document.all){
		OBJ = document.all[name].submit();
	}
	else if (document.layers){
		if (document.layers[name]){
			OBJ = document.layers[name].submit();
		}
	}
}

function getValue(name){
	if (document.getElementById){
		objValue = document.getElementById(name).value;
	}
	else if (document.all){
		objValue = document.all[name].value;
	}
	else if (document.layers){
		if (document.layers[name]){
			objValue = document.layers[name].value;
		}
	}
	return(objValue);
}


function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}


function checkForm() {
	isError	= 0;
	aFields = formFields.split(",");
	aNames = formFieldsNames.split(",");

	error = "The following fields require your input\n----------------------------------------------";
	count = aFields.length;
	for(n = 0; n < count; n++){
		OBJ		= aFields[n];
		objValue	= "";
		objValue	= getValue(OBJ);
		//alert(aFields[n] + ":" + objValue)
		if(objValue.length == 0 || objValue == "none" || objValue == null || objValue == "null" ){
			isError = 1;
			error += "\n" + aNames[n];
		}
		if(aFields[n] == "Email" || aFields[n] == "email" && objValue.length != 0){
			isEmail = checkEmail(objValue);
			if(isEmail != "OK"){
				isError = 1;
				error += "\n" + isEmail;
			}
		}

	}

  privacy=document.getElementById("privacy");
  if (privacy){
    value=getCheckedValue(privacy);
    if (value!="1"){
      isError=1;
      error="You should agree with privacy policy to be able to submit.";
    }
  }
	if(isError == 1){
		alert(error);
	}
	else{
		submitForm(formName);
	}
}
