function ParseElem(node) {
	var elem = new Object();
	for (var i = 0; i < node.childNodes.length ; i++) {
		var child = node.childNodes[i];
		if (child.tagName == "type")
			elem.type = child.textContent;
		if (child.tagName == "name")
			elem.name = child.textContent;
		if (child.tagName == "id")
			elem.id = child.textContent;
	}
	return elem;
}

function RuleData(rule) {
	this.website = null;
	this.id_elem = null;
	this.pw_elem = null;
	for (var i = 0; i < rule.childNodes.length ; i++) {
		var node = rule.childNodes[i];
		if (node.tagName == "website") {
			this.website = node.textContent;
			console.log("website=" + this.website);
		}
		else if (node.tagName == "id_elem") {
			this.id_elem = ParseElem(node);
			console.log("\tid_elem.type=" + this.id_elem.type);
			console.log("\tid_elem.name=" + this.id_elem.name);	
			console.log("\tid_elem.id=" + this.id_elem.id);	
		}
		else if (node.tagName == "pw_elem") {
			this.pw_elem = ParseElem(node);
			console.log("\tpw_elem.type=" + this.pw_elem.type);
			console.log("\tpw_elem.name=" + this.pw_elem.name);	
			console.log("\tpw_elem.id=" + this.pw_elem.id);	
		}
	}
}

function websiteParser() {
	this.Rule = new Array();
	this.genericRule = null;
	
	this.init = function(file) {
		var xmlhttp = new XMLHttpRequest();
		xmlhttp.open("GET", file, false);
		xmlhttp.send();
		var xmlDoc=xmlhttp.responseXML;
		var rule = xmlDoc.getElementsByTagName("Rule");
		var rule_index = 0;
		for (var i = 0 ; i < rule.length ; i++) {
			var rule_data = new RuleData(rule[i]);
			if (rule_data.website == "generic") {
				this.genericRule = rule_data;
			}
			else {
				this.Rule[rule_index] = rule_data;
				rule_index++;
			}
		}
	};
	
	this.findRule = function(url) {
		if (this.Rule == null) return;
		for (var i = 0 ; i < this.Rule.length ; i++) {
			if (url.toLowerCase().indexOf(this.Rule[i].website) >= 0)
				return this.Rule[i];
		}
		return this.genericRule;
	};
	
	this.getWebsiteName = function(url) {
		return url.split("/")[2];
	};
		
	this.parseForm = function(name, form) {
		if (this.Rule == null) return;
		var rule = this.findRule(name);
		if (rule) {
			console.log("rule = " + rule.website + ", form = " + form.name);
			var id_element, pw_element;
			for (var i = 0 ; i < form.elements.length ; i++) {
				//console.log("elements[" + i + "].type = " + form.elements[i].type);
				//console.log("elements[" + i + "].id = " + form.elements[i].id);
				//console.log("elements[" + i + "].name = " + form.elements[i].name);
				if ((!rule.id_elem.type || form.elements[i].type == rule.id_elem.type) &&
				    (!rule.id_elem.id || form.elements[i].id.toLowerCase() == rule.id_elem.id) &&
				    (!rule.id_elem.name || form.elements[i].name.toLowerCase() == rule.id_elem.name))
					id_element = form.elements[i];
				else if ((!rule.pw_elem.type || form.elements[i].type == rule.pw_elem.type) &&
				         (!rule.pw_elem.id || form.elements[i].id.toLowerCase() == rule.pw_elem.id) &&
				         (!rule.pw_elem.name || form.elements[i].name.toLowerCase() == rule.pw_elem.name))
					pw_element = form.elements[i];
					
			}
			if (id_element && pw_element) {
				console.log("found id and pw element");
				return {id: id_element, pw:pw_element};
			}
		}	
		return null;
	};
	
	this.checkPage = function(document, target) {
		console.log("checkPage");
		if (this.Rule == null) return;
		var site_name = this.getWebsiteName(document.URL);
		var rule = this.findRule(site_name);
		if (rule && document.forms) {
			var forms = document.forms;
			for (var i = 0 ; i < forms.length ; i++) {
				target.elements = this.parseForm(site_name, forms[i]);
				if (target.elements) {
					target.form = forms[i];
					return true;
				}
			}
		}
		return false;
	};
};