var profile = new userProfile();
var unspsc = new Unspsc();

function userProfile()
{
	var selFunc = new selectFunc();
	var lsFunc = new listFunc();
	var regExpressFunc=new regExpFunc();
	var winFunc=new newWinFunc();
	//
	this.checkBoxesActivityId = "chkBusiActivity";
	this.countCheckBoxesActivity = 0;
	this.checkBoxesCategoryId = "chkCategory";
	this.countCheckBoxesCategory = 0;
	
	this.noCanAm = false;
	//properties
	this.userStatus="";
	this.bcid="";
	this.firstName = "";
	this.lastName="";
	this.email = "";
	this.businessName = "";
	this.address = "";
	this.city = "";
	this.country="Canada";
	this.provState="";
	this.postZip="";
	this.phone="";
	this.fax="";
	this.areas= new Array();
	this.categories=new Array();
	this.keywordsIn=new Array();
	//this.keywordsEx=new Array();
	this.anyIn="";
	//this.anyEx="";
	this.status="";
	this.format="1";
	this.content="1";
	this.headInfo="";
	this.newsletter=1;
	this.conflict=false;
	/*---------------validate-------------------*/
	this.validate=function()
	{
		var oValSum = document.getElementById("validateSum");
		var ret=true;
		lsFunc.clear(oValSum);
		if(!document.getElementById("agree").checked)
		{
			lsFunc.addChild(oValSum, "li", "If you want to logon/sign up profile, You must agree the privacy notice.");
			ret= false;
		}
		if(document.getElementById("firstName").value.replace(" ")=="")
		{
			lsFunc.addChild(oValSum, "li", "Please input first name");
			ret= false;
		}
		if(document.getElementById("lastName").value.replace(" ")=="")
		{
			lsFunc.addChild(oValSum, "li", "Please input last name");
			ret= false;
		}
		if(document.getElementById("email").value.replace(" ")=="")
		{
			lsFunc.addChild(oValSum, "li", "Please input email");
			ret= false;
		}
		if(!regExpressFunc.validateEmail(document.getElementById("email").value))
		{
			lsFunc.addChild(oValSum, "li", "Invalid email address");
			ret= false;
		}
		if(document.getElementById("reEmail").value.replace(" ")=="")
		{
			lsFunc.addChild(oValSum, "li", "Please re-enter your email to confirm");
			ret= false;
		}
		if(document.getElementById("email").value != document.getElementById("reEmail").value)
		{
			lsFunc.addChild(oValSum, "li", "The email and re-enter email are not identical");
			ret= false;
		}
		if(document.getElementById("businessName").value.replace(" ")=="")
		{
			lsFunc.addChild(oValSum, "li", "Please input business name");
			ret= false;
		}
		if(document.getElementById("address").value.replace(" ")=="")
		{
			lsFunc.addChild(oValSum, "li", "Please input address");
			ret= false;
		}
		if(document.getElementById("city").value.replace(" ")=="")
		{
			lsFunc.addChild(oValSum, "li", "Please input city");
			ret= false;
		}
		if(document.getElementById("country").selectedIndex==-1)
		{
			lsFunc.addChild(oValSum, "li", "Please select your country");
			ret= false;
		}
		if(this.country =="United States" || this.country=="Canada")
		{
			if(document.getElementById("provState").selectedIndex<=0 && document.getElementById("txtProvState").value=="")
			{
				lsFunc.addChild(oValSum, "li", "Please input province/state");
				ret= false;
			}
			if(document.getElementById("postZip").value=="")
			{
				lsFunc.addChild(oValSum, "li", "Please input post/Zip");
				ret= false;
			}
		}
		if(document.getElementById("phone").value.replace(" ")=="")
		{
			lsFunc.addChild(oValSum, "li", "Please input phone");
			ret= false;
		}
		if(!this.procurementChecked("activity"))
		{
			lsFunc.addChild(oValSum, "li", "Please select business activity");
			ret= false;
		}
		if(!this.procurementChecked("category"))
		{
			lsFunc.addChild(oValSum, "li", "Please select procurement category");
			ret= false;
		}
		if(!ret)
		{
			oValSum.style.display="";
			scrollToTop();
		}
		else
			oValSum.style.display="none";
		return ret;
	}
	/*---------private functions---------------*/
	function setTxtProvState()
	{
		oSelect=document.getElementById("provState");
		selFunc.clearSelect(oSelect);
		oSelect.style.display="none";
		oText=document.getElementById("txtProvState");
		if(profile.provState)
			oText.value=profile.provState;
		else
			oText.value="";
		oText.className="formInput";
		this.noCanAm = true;
		document.getElementById("provRequired").style.display="none";
		document.getElementById("postRequired").style.display="none";
	}
	
	//set provState drop down list
	function setProvState(bCan, val)
	{
		var oSelect = document.getElementById("provState");
		var oText = document.getElementById("txtProvState");
		//hide text
		oText.className="hide";
		oText.value="";
		//reset select
		selFunc.clearSelect(oSelect);
		var oOpt = document.createElement("option");
		if(bCan)
		{
			oOpt.text="--select province--";
		}
		else
		{
			oOpt.text="--select state--";
		}
		oSelect.options[0]=oOpt;
		if(!bCan)
		{
			selFunc.addSelect(oSelect, states,statesValue, val);
		}
		else
		{
			selFunc.addSelect(oSelect, provinces,provincesValue, val);
		}
		
		selFunc.sigleSelect(oSelect, val);
			
		oSelect.style.display="";
		
		this.noCanAm = true;
		document.getElementById("provRequired").style.display="";
		document.getElementById("postRequired").style.display="";
	}
	
	
	function getProvStateValue()
	{
		var oSelect, oText;
		oSelect = document.getElementById("provState");
		oText=document.getElementById("txtProvState");
		if(oSelect.style.display=="none")
		{
			return oText.value;
		}
		else
		{
			return oSelect.options[oSelect.selectedIndex].value;
		}
	}
	this.procurementChecked=function(name)
	{
		var i=0, oCheck;
		switch(name)
		{
			case "activity":
				if(this.countCheckBoxesActivity > 0)
				{
					
					for(i=0;i<this.countCheckBoxesActivity;i++)
					{
						oCheck = document.getElementById(this.checkBoxesActivityId+i.toString());
						if(oCheck.checked)
						{
							return true;
						}
					}
				}
				break;
			case "category":
				if(this.countCheckBoxesCategory>0)
				{
					for(i=0;i<this.countCheckBoxesCategory;i++)
					{
						oCheck = document.getElementById(this.checkBoxesCategoryId+i.toString());
						if(oCheck.checked)
						{
							return true;
						}
					}
				}
				break;
			default:
				break;
		}
		return false;
	}
	
	/*--------------------------------------------------
	/function: encode an array to a string for specified field
	/*param: name of the field
	/return: Escape(value1);Escape(value2)...
	/-----------------------------------------------------*/
	this.encodeChecked=function(name)
	{
		var ret = "";
		var i=0, oCheck;
		switch(name)
		{
			case "activity":
				if(this.countCheckBoxesActivity > 0)
				{
					for(i=0;i<this.countCheckBoxesActivity;i++)
					{
						oCheck = document.getElementById(this.checkBoxesActivityId+i.toString());
						if(oCheck.checked)
						{
							ret+=(ret=="")?escape(oCheck.value):","+escape(oCheck.value);
							
						}
					}
				}
				break;
			case "category":
				if(this.countCheckBoxesCategory>0)
				{
					for(i=0;i<this.countCheckBoxesCategory;i++)
					{
						oCheck = document.getElementById(this.checkBoxesCategoryId+i.toString());
						if(oCheck.checked)
						{
							ret+=(ret=="")?escape(oCheck.value):","+escape(oCheck.value);
						}
					}
				}
				break;
			default:
				break;
		}
		return ret;
	}
	/*---------------------------------
	/function: encode keywords select control
	/param: select id
	/return: encode string
	/------------------------------------*/
	function encodeSelects(oSelect)
	{
		var ret="";
		if(oSelect.options)
		{
			for(var i=0;i<oSelect.options.length;i++)
			{
				ret+=(ret=="")?escape(oSelect.options[i].value):(","+escape(oSelect.options[i].value));
			}
		}
		return ret;
	}
	/*--------------------set country drop down list--------------------*/
	this.setCountry=function()
	{
		var oSelect=document.getElementById("country");
		
		if(oSelect.options.length > 0)
		{
			selFunc.sigleSelect(oSelect, this.country);
		}
		else
		{
			selFunc.addSelect(oSelect, countries, countries, this.country);
		}
		if(this.country=="Canada")
		{
			setProvState(true,this.provState);
			return
		}
			
		if(this.country=="United States")
		{
			setProvState(false,this.provState)
			return;
		}
		setTxtProvState();	
	}
	
	
	
	/*--------------initialize procurement profiles--------------*/
	this.setKeywords = function(btnInId, btnExId)
	{
		var oKeywordsIn, oKeywordsEx;
		/*-----------keywords(include)--------------------*/
		oKeywordsIn=document.getElementById("keywordsInclude");
		selFunc.addSelect(oKeywordsIn, this.keywordsIn, this.keywordsIn, "");
		if(oKeywordsIn.length > 0)
			document.getElementById(btnInId).disabled=false;
		/*-----------keywords(exclude)--------------------
		oKeywordsEx=document.getElementById("keywordsExclude");
		selFunc.addSelect(oKeywordsEx, this.keywordsEx, this.keywordsEx, "");
		if(oKeywordsEx.length > 0)
			document.getElementById(btnExId).disabled=false;*/
	}
	
	/*---------------Add keyword----------------------*/
	this.addKeyword=function(oBtn,selectId,inputId,btnRemoveId)
	{
		var oSelect, oInput, oOpt;
		var value="";
		oInput = document.getElementById(inputId);
		oSelect = document.getElementById(selectId);
		value=oInput.value;
		for(var i=0;i<oSelect.options.length;i++)
		{
			if(oSelect.options[i].value.toLowerCase() == value.toLowerCase())
			{
				oSelect.options[i].selected=true;
				return;
			}
		}
		//create option and add to select
		oOpt = document.createElement("option");
		oOpt.value=value;
		oOpt.text = value;
		oSelect.options[oSelect.options.length]=oOpt;
		
        this.updateKeywordsTracker(oSelect);
		
		oInput.value = "";
		oBtn.disabled = true;
		document.getElementById(btnRemoveId).disabled=false;
		
	}
	/*------------on remove button clicked-------------*/
	this.removeKeyword=function(id, obj)
	{
		var oSelect = document.getElementById(id);
		for(var i=oSelect.options.length-1;i>=0;i--)
		{
			if(oSelect.options[i].selected)
				oSelect.remove(i);
		}
		if(oSelect.options.length==0)
		{
			obj.disabled=true;
		}
		this.updateKeywordsTracker(oSelect);
	}
	this.updateKeywordsTracker=function(oSelect) {
	    oKeywords = document.getElementById('keywords');
	    var keywords = '';
	    for(var i=0; i<oSelect.options.length; i++) {
	        keywords += oSelect.options[i].value + ',';
	    }
	    keywords = keywords.substr(0, keywords.length-1); //remove last comma
	    oKeywords.value = keywords;
	}
	/*------------------onKeywordsInputKeyPress-----------*/
	this.onKeywordsInputKeyPress=function(idButton, obj)
	{
		var text = "", oButton;
		text = obj.value;
		oButton = document.getElementById(idButton);
		if(text.replace(" ", "")!="")
		{
			oButton.disabled = false;
		}
		else
			oButton.disabled = true;	
	}
	this.countrySelectChanged=function(value)
	{
		this.country=value;
		this.provState="";
		this.setCountry();
	}

	/*-------------------submit----------------------*/
	this.submitProfile=function(oForm)
	{
	
		if(this.validate())
		{
		
			var newWin=null;
			document.getElementById("hidProvState").value=getProvStateValue();
			document.getElementById("activities").value=this.encodeChecked("activity");
			document.getElementById("cates").value=this.encodeChecked("category");
			document.getElementById("keysIn").value=encodeSelects(document.getElementById("keywordsInclude"));
			//document.getElementById("keysEx").value=encodeSelects(document.getElementById("keywordsExclude"));
			return true;
		}
		else
		{
			return false;
		}
	}
	
	function sortCheckbox(a,b)
	{
		if(a.value<b.value)
			return -1;
		if(a.value==b.value)
			return 0;
		if(a.value>b.value)
			return 1;
	}
	
	function setActivities()
	{
		profile.areas.sort();
		var i=0, j=0;
		var sAreas = new Array();
		while(i<profile.countCheckBoxesActivity)
		{
			var ch = document.getElementById(profile.checkBoxesActivityId + i.toString());
			sAreas[i] = ch;
			
			i++;
		}
		
		sAreas.sort(sortCheckbox);
		
		i=0;j=0;
		while(i<profile.countCheckBoxesActivity && j<profile.areas.length)
		{
		
			var checkbox = sAreas[i];
			if(profile.areas[j]==checkbox.value)
			{
				checkbox.checked=true;
				i++;
				j++;
			}
			else
				i++;
		}
		
	}
	//When the select all button clicked
	this.selectAllActivities = function()
	{
		for(var i=0;i<this.countCheckBoxesActivity;i++)
		{
			var ch = document.getElementById(this.checkBoxesActivityId + i.toString());
			ch.checked=true;
		}
	}
	
	this.unselectAllActivities = function()
	{
		for(var i=0;i<this.countCheckBoxesActivity;i++)
		{
			var ch = document.getElementById(this.checkBoxesActivityId + i.toString());
			ch.checked=false;
		}
	}
	this.selectAllCategories = function()
	{
		for(var i=0;i<this.countCheckBoxesCategory;i++)
		{
			var ch = document.getElementById(this.checkBoxesCategoryId + i.toString());
			ch.checked=true;
		}
	}
	this.unselectAllCategories = function()
	{
		for(var i=0;i<this.countCheckBoxesCategory;i++)
		{
			var ch = document.getElementById(this.checkBoxesCategoryId + i.toString());
			ch.checked=false;
		}
	}
	this.setCategories = function(cates,expand)
	{
		cates.sort();
		var sCates = new Array();
		var sCatesOld = new Array();
		var i=0,j=0;
		var bFirst = false;
		var theTop = 0;
		var oDiv;
		while(i<this.countCheckBoxesCategory)
		{
			var ch = document.getElementById(this.checkBoxesCategoryId + i.toString());
			
			sCates[i] = ch;	
			sCatesOld[i] = ch;
			i++;
		}
		
		sCates.sort(sortCheckbox);
		
		i=0;
		j=0;
		while(i<this.countCheckBoxesCategory && j<cates.length)
		{
			var checkbox = sCates[i];
			if(cates[j]==checkbox.value)
			{
				checkbox.checked=true;
				//Expand the checkbox
				if(expand)
				{
					var id = "ul_" + checkbox.id;
					var ul = document.getElementById(id);
					if(ul)
						ul.style.display = "";
					id = "li_" + checkbox.id;
					var li = document.getElementById(id);
					if(li)
						li.className = "withChild expand";
				}
				if(!bFirst)
					{
						bFirst = true;
						theTop = checkbox.id;
					}
				i++;
				j++;
			}
			else
				i++;
		}
		if(bFirst)	//Find
		{
			var iTop = 0;
			for(var k=0;k<sCatesOld.length;k++)
			{
				if(theTop == sCatesOld[k].id)
				{
					iTop = k;
					break;
				}
			}
			oDiv = document.getElementById("divCategoryList");
			
			oDiv.scrollTop = oDiv.scrollHeight * iTop /(this.countCheckBoxesCategory);
			
		}
		
	}
	
	this.expandAll=function()
	{
		var i=0;
		while(i<this.countCheckBoxesCategory)
		{
			var li = document.getElementById("li_" + this.checkBoxesCategoryId + i.toString());
			if(li)
			{
				li.className = "withChild expand";
			}
			var ul = document.getElementById("ul_" + this.checkBoxesCategoryId + i.toString());
			if(ul)
				ul.style.display = "";
		}
	}
	
	this.toggleAll=function()
	{
		var i=0;
		while(i<this.countCheckBoxesCategory)
		{
			var li = document.getElementById("li_" + this.checkBoxesCategoryId + i.toString());
			if(li)
			{
				li.className = "withChild toggle";
			}
			var ul = document.getElementById("ul_" + this.checkBoxesCategoryId + i.toString());
			if(ul)
				ul.style.display = "none";
		}
	}
	
	this.expandToggle=function(sId)
	{
	
		var id = "ul_" + sId;
		var ul = document.getElementById(id);
		if(ul)
		{
			if(ul.style.display == "")
				ul.style.display = "none";
			else
				ul.style.display = "";
		}	
		id = "li_" + sId;
		var li = document.getElementById(id);
		if(li)
		{
			if(li.className == "withChild toggle")
				li.className = "withChild expand";
			else
				li.className = "withChild toggle";
		}
	}

}

function UnspscHead(sCode, sDesc)
{
	this.code = sCode;
	this.description = sDesc;
	this.contents = null;
	this.addContent = function(cont)
	{
		this.contents = new UnspscContent(cont);
		return this.contents;
	}
	this.search = function(sVal)
	{
		var conts = this.contents;
		var sValLowerCase = sVal.toLowerCase();
		while(conts != null)
		{
			if(conts.contentName.toLowerCase().indexOf(sValLowerCase)>=0)
				return true;
			conts = conts.next;
		}
		return false;
	}
}

function UnspscContent(cont)
{
	this.contentName = cont;
	this.next = null;
	this.add = function(sCont)
	{
		this.next = new UnspscContent(sCont);
		return this.next;
	}
}

function Unspsc()
{
	this.heads = null;
	this.addHead=function(sCode, sDesc)
	{
		if(this.heads == null)
		{
			this.heads = new Array();
			this.heads[0] = new UnspscHead(sCode, sDesc);
		}
		else
		{
			this.heads[this.heads.length] = new UnspscHead(sCode, sDesc);
		}
		
	}
	
	this.searchContent = function(sId)
	{
		var ret = null;
		if(this.heads == null)
			return null;
		var sSearch = document.getElementById(sId).value;
		if(sSearch == "") return null;
		for(var i=0;i<this.heads.length;i++)
		{
			if(this.heads[i].search(sSearch))
			{
				if(ret == null)
				{
					ret = new Array();
					ret[0] = this.heads[i].code;
				}
				else
				{
					ret[ret.length] = this.heads[i].code;
				}
			}
		}
		if(ret != null)
			profile.setCategories(ret, true);
		else
			alert("Sorry, no result of search for category '" + sSearch + "'!" );
	}	
}

