﻿function Passport(passportcode,username,password,petname,pettype,callback){
	var tmpdebug = debug;
	var url="http://www.mydigipet.com/passport/?job=pp"; 
	var date=new Date();
	var me=this;
	this.username = username;
	this.password = password;
	this.xmldom=null;
	this.passport=passportcode;
	this.appid=0;
	this.folder="";
	this.petname=petname;
	this.pettype=pettype; if(this.pettype==null||this.pettype=="") this.pettype="santa07";
	this.xml="";
	this.dob=0;
	this.clock=30000;
	this.servertime=0;
	this.home=false;

	this.updateticker=0;
	this.passportxml=null;
	var xmlhttp=getXMLHTTP();
	this.callback=callback;
	//setTimeout(getPassport,10000);
	getPassport();
	this.getPassport = getPassport;
	function getPassport(){
		date=new Date();
		param="&passport="+me.passport+"&appid="+me.appid;
		param+="&user="+me.username+"&pass="+me.password;
		param+="&pettype="+me.pettype+"&petname="+me.petname;
		param+="&xml="+me.xml;
		param+="&cache="+date.getTime();
		//alert(param);
		try {
			xmlhttp.open("GET", url+param, true);
		} catch (e) {
			debug.error("cannot find server or invalid url");
			xmlhttp = null; // to do Catch invalid URLs
			return;
		}
		// Set the callback for when the downloading is completed (or failed)
		xmlhttp.onreadystatechange = getpassportdata;
		// Start the download
		try {
			xmlhttp.send("");
		} catch (e) {
			debug.error("sending request failed");
			xmlhttp = null; // to do catch errors sending the request
			return;
		 }
	}

	//this.getpassportdata = getpassportdata;
	function getpassportdata(){
		var tmpid=0;
		if(xmlhttp.readyState!=4) return;
		me.xmldom = xmlhttp.responseXML;
		if(me.xmldom==null) alert(xmlhttp.responseText);
		root   = me.xmldom.getElementsByTagName("PET")[0];
		tmpNode = root.getElementsByTagName("ERROR")[0];
		if(tmpNode!=null&&tmpNode.childNodes.length > 0){
			alert("Error "+tmpNode.firstChild.nodeValue);
			callback(false);
			return;
		}
		tmpNode = root.getElementsByTagName("PASSPORT")[0];
		if(tmpNode!=null&&tmpNode.childNodes.length > 0) me.passport = tmpNode.firstChild.nodeValue;
		tmpNode = root.getElementsByTagName("APPID")[0];
		if(tmpNode!=null&&tmpNode.childNodes.length > 0) tmpid = tmpNode.firstChild.nodeValue;
		tmpNode = root.getElementsByTagName("DOB")[0];
		if(tmpNode!=null&&tmpNode.childNodes.length > 0) me.dob = tmpNode.firstChild.nodeValue;
		tmpNode = root.getElementsByTagName("PETTYPE")[0];
		if(tmpNode!=null&&tmpNode.childNodes.length > 0) me.pettype = tmpNode.firstChild.nodeValue;
		tmpNode = root.getElementsByTagName("PETNAME")[0];
		if(tmpNode!=null&&tmpNode.childNodes.length > 0) me.petname = tmpNode.firstChild.nodeValue;
		tmpNode = root.getElementsByTagName("CLOCK")[0];
		if(tmpNode!=null&&tmpNode.childNodes.length > 0) me.clock = tmpNode.firstChild.nodeValue;
		tmpNode = root.getElementsByTagName("CURTIME")[0];
		if(tmpNode!=null&&tmpNode.childNodes.length > 0) me.servertime = tmpNode.firstChild.nodeValue;
		if(me.clock < 10000) me.clock = 10000;		
		if(me.appid==0) me.appid = tmpid;
		if(me.appid==tmpid) me.home=true; else me.home=false;
		if(me.petname==null || me.petname=="undefined") me.petname="";
		me.callback(true);
	}

}

