/**
 * some function for feedback
 *
 * @category JavaScript
 * @subpackage simple page function
 * @author Louis
 * @version $0.3$
 */

//init customer relationship
var _RC = [
	{id:1,rel:'Customer'},
	{id:2,rel:'Buyer'},
	{id:3,rel:'Client'},
	{id:4,rel:'User'},
	{id:5,rel:'Member'},
	{id:6,rel:'Tenant'},
	{id:7,rel:'Patient'},
	{id:0,rel:'Enter Custom Title...'}
];

//init supplier relationship
var _RS = [
	{id:8,rel:'Supplier'},
	{id:9,rel:'Seller'},
	{id:10,rel:'Provider'},
	{id:11,rel:'Renter'},
	{id:12,rel:'Vendor'},
	{id:13,rel:'Consultant'},
	{id:14,rel:'Broker'},
	{id:0,rel:'Enter Custom Title...'}
];

var feedback={
	_setting:{"MenuClass":"VoteMenu","MiniHolder":"MiniMenu","MiniMenuLinks":"MiniMenuLinks"},
	Message: {
		"SellTarget":"Entering a name or email will allow you to sell to a person, while entering a domain will let you sell to a network.",
		"NetSellTarget":"Entering a name or email will allow this network to sell to a person, while entering a domain will let this network sell to another network.",
		"BuyTarget":"Entering a name or email will allow you to buy something from a person, while entering a domain will let you buy something from a network.",
		"NetBuyTarget":"Entering a name or email will allow this network to buy something from a person, while entering a domain will let this network buy something from another network.",
		"URL":"Enter the URL where this product or service can be found. Or, you can enter the URL where the tranaction started (for instance a forum posting).",
		"ValidDate":"You will be able to update your feedback once per day, until the end of the transaction. A history of all feedback will be saved.",
		"Relation":"If your friend Aenna is an attorney representing you in a laysuit, you would put her as your \"lawyer\", and you as her \"client\".",
		"HomeTarget":"Enter the email address of the person or the domain name of the company you are planning to do business with."
	},
	_getDiffVal : function() {
		if(Version.ie)
		{
			this._diffVal = 0;
		}
		else
		{
			this._diffVal = 7;
		}
	},
	Run:function(om,ol){
    if(om&&getx(om)){
      getx(om).className=this._setting.MenuClass;
      getx(om).onmouseover=function(){
        feedback.draw(this,ol||false);
      };
    }
    return ;
  },draw:function(o,_link){
    var op=o.parentNode;
    op.style.position="relative";
    _holder=addx(op.parentNode,makex("div"));
    _holder.className=this._setting.MiniHolder;
    var _pop=this.PopWindow(_holder);
    _pop._core.style.textAlign="right";
    if(_link){
      var _a=addx(_pop._core,makex("a"));
      _a.innerHTML=_link.text;
      _a.href=_link.href;
      _a.onclick=function(){
        gotoUrl(this.href);
        return false;
      };
    }
    if(_pop._core.offsetWidth<o.offsetWidth-12){
      _pop._core.style.width=o.offsetWidth-12+"px";
    }
    _holder.style.top=o.offsetHeight+op.offsetTop+"px";
    _holder.style.paddingLeft=o.offsetLeft+o.offsetWidth-_holder.offsetWidth+"px";
    op.style.position="static";
    o._holder=_holder;
    o.onmouseover=function(){
      cleartimer(this._holder._timer);
      this._holder.style.display="block";
    };
    o.onmouseout=function(){
      cleartimer(this._holder._timer);
      var _holder=this._holder;
      _holder._timer=setTimeout(function(){
        _holder.style.display="none";
      },500);
    };
    _pop.onmouseover=function(){
      cleartimer(this.parentNode._timer);
    };
    _pop.onmouseout=function(){
      cleartimer(this.parentNode._timer);
      var _holder=this.parentNode;
      _holder._timer=setTimeout(function(){
        _holder.style.display="none";
      },500);
    };
  },PopWindow:function(x){
    var _table=makex("table",{"id":"PopWindow"});
    var _tbody=addx(_table,makex("tbody"));
    var _tr=addx(_tbody,makex("tr"));
    var PopArea=addx(_tr,makex("td"));
    var _th=addx(_tr,makex("th",{"id":"PopTR"}));
    var _img=addx(_th,makex("img"));
    _img.src="/images/tabbar/shadow-tr.gif";
    var _tr=addx(_tbody,makex("tr"));
    var _th=addx(_tr,makex("th",{"id":"PopBL"}));
    addx(_th,makex("div",{"id":"PopB"}));
    addx(_tr,makex("th",{"id":"PopBR"}));
    addx(x,_table);
    var _div=addx(PopArea,makex("div"));
    _div.className=this._setting.MiniMenuLinks;
    _table._core=_div;
    if(navigator.userAgent.indexOf("MSIE")>=0){
      css.add("table#PopWindow th","filter:alpha(opacity=60);");
    }
    return _table;
  },
  InputTip:function(o,field,defaultMessage)
  {
  	if(getx('inputTip'))
  		getx('inputTip').parentNode.removeChild(getx('inputTip'));
  		
  	this._getDiffVal();
  	if(defaultMessage)
  	{
  		o.dMsg = defaultMessage;
  		if(o.value == defaultMessage)
  		{
  			o.value = '';
  		}
  	}
  	o.style.color = 'black';
  	o.style.position="relative";
  	if(field == undefined) return;
  	var floatTip = makex("div",{"id":"inputTip"});
  	var outTable = this.drawTable(floatTip);
  	switch (field)
  	{
  		case 'BuyTarget' : 
  			outTable._Core.innerHTML = this.Message.BuyTarget;
  			floatTip.style.left = o.offsetLeft+o.offsetWidth+this._diffVal+8+ 'px'; 
  			floatTip.style.top = o.offsetTop+this._diffVal-30 + 'px';
  			break;
  		case 'NetBuyTarget' : 
  			outTable._Core.innerHTML = this.Message.NetBuyTarget;
  			floatTip.style.left = o.offsetLeft+o.offsetWidth+this._diffVal+8+ 'px'; 
  			floatTip.style.top = o.offsetTop+this._diffVal-30 + 'px';
  			break;
  		case 'SellTarget' : 
  			outTable._Core.innerHTML = this.Message.SellTarget;
  			floatTip.style.left = o.offsetLeft+o.offsetWidth+this._diffVal+8+ 'px'; 
  			floatTip.style.top = o.offsetTop+this._diffVal-30 + 'px';
  			break;
  		case 'NetSellTarget' : 
  			outTable._Core.innerHTML = this.Message.NetSellTarget;
  			floatTip.style.left = o.offsetLeft+o.offsetWidth+this._diffVal+8+ 'px'; 
  			floatTip.style.top = o.offsetTop+this._diffVal-30 + 'px';
  			break;
  		case 'URL' : 
  			outTable._Core.innerHTML = this.Message.URL;
  			floatTip.style.left = o.offsetLeft+o.offsetWidth+this._diffVal+8+ 'px';
  			floatTip.style.top = o.offsetTop+this._diffVal-30 + 'px';
  			break;
  		case 'ValidDate' : 
  			outTable._Core.innerHTML = this.Message.ValidDate;
  		    floatTip.style.left = o.offsetLeft+this._diffVal+290+ 'px';
  			floatTip.style.top = o.offsetTop+this._diffVal-30 + 'px';
  			break;
  		case 'Relation' : 
  		    outTable._Core.innerHTML = this.Message.Relation;
  			floatTip.style.left = o.offsetLeft+this._diffVal+310+ 'px';
  			floatTip.style.top = o.offsetTop+this._diffVal-30 + 'px';
  			break;
  		case 'HomeTarget' : 
  			outTable._Core.innerHTML = this.Message.HomeTarget;
  			floatTip.style.left = o.offsetLeft+o.offsetWidth+this._diffVal+8+ 'px'; 
  			floatTip.style.top = o.offsetTop+this._diffVal-25 + 'px';
  			break;
  		
  	}
  	addx(o.parentNode,floatTip);
  	o.onblur = function() { 
  		this.parentNode.removeChild(floatTip);
  		if((this.value.trim() == '') && (this.dMsg))
  		{
  			this.style.color = 'gray';
  			this.value = this.dMsg;
  		}
  	};
  	
  },
  InputTip2:function(o,field,defaultMessage)
  {
  	this._getDiffVal();
  	if(defaultMessage)
  	{
  		o.dMsg = defaultMessage;
  		if(o.value == defaultMessage)
  		{
  			o.value = '';
  			o.style.color = 'black';
  		}
  	}
  	if(!getx('inputTip'))
  	{ 		
  	
	  	o.style.position="relative";
	  	if(field == undefined) return;
	  	var floatTip = makex("div",{"id":"inputTip"});
	  	var outTable = this.drawTable(floatTip);
	  	switch (field)
	  	{
	  		case 'HomeTarget' : 
	  			outTable._Core.innerHTML = this.Message.HomeTarget;
	  			floatTip.style.left = o.offsetLeft+o.offsetWidth+this._diffVal+8+ 'px'; 
	  			floatTip.style.top = o.offsetTop+this._diffVal-25 + 'px';
	  			break;
	  		
	  	}
	  	addx(o.parentNode,floatTip);
  	}
  	o.onblur = function() { 
  		this.parentNode.removeChild(getx('inputTip'));
  		if((this.value.trim() == '') && (this.dMsg))
  		{
  			this.style.color = 'gray';
  			this.value = this.dMsg;
  		}
  	};
  	
  },
  drawTable:function(x)
  {
  	var _table = makex('table');
  	var _tbody = addx(_table,makex("tbody"));
  	var _tr = addx(_tbody,makex("tr"));
  	addx(_tr,makex("td",{"id":"LTcorner"}));
  	var _topB = addx(_tr,makex("td"));
  	_topB.className = "TDB";
  	addx(_tr,makex("td",{"id":"RTcorner"}));
  	var _tr = addx(_tbody,makex("tr"));
  	var _leftB = addx(_tr,makex("td"))
  	_leftB.className = "LRB";
  	var TdCore = addx(_tr,makex("td",{"id":"core"}));
  	var _RightB = addx(_tr,makex("td"));
  	_RightB.className = "LRB";
  	var _tr = addx(_tbody,makex("tr"));
  	addx(_tr,makex("td",{"id":"LDcorner"}));
  	var _downB = addx(_tr,makex("td"));
  	_downB.className = "TDB";
  	addx(_tr,makex("td",{"id":"RDcorner"}));
  	addx(x,_table);
  	var _div = addx(TdCore,makex("div",{"id":"tipContent"}));
  	_table._Core = _div;
  	return _table;
  },
  initSelect:function(TranType,rel2,rel)
  {
	var obSelect = getx('relation');
	var obSelect2 = getx('relation2');
	if(obSelect.style.display == 'none')
	{
		getx('enterRel').style.display = 'none';
		obSelect.style.display = 'block';
	}
	if(obSelect2.style.display == 'none')
	{
		getx('enterRel2').style.display = 'none';
		obSelect2.style.display = 'block';
	}
	obSelect.options.length = 0;
	obSelect2.options.length = 0;
	if(TranType == 'SUPPLIER')
	{
		for(i=0;i<_RS.length;i++)
		{
			var varItem = new Option(_RS[i].rel, _RS[i].id);
			obSelect2.options.add(varItem);
		}
		for(i=0;i<_RC.length;i++)
		{
			var varItem = new Option(_RC[i].rel, _RC[i].id);
			obSelect.options.add(varItem);
		}
	}
	else
	{
		for(i=0;i<_RS.length;i++)
		{
			var varItem = new Option(_RS[i].rel, _RS[i].id);
			obSelect.options.add(varItem);
		}
		for(i=0;i<_RC.length;i++)
		{
			var varItem = new Option(_RC[i].rel, _RC[i].id);
			obSelect2.options.add(varItem);
		}
	}
	this.SelectItemByValue(obSelect2,rel2);
	this.SelectItemByValue(obSelect,rel);
  },
  SelectItemByValue:function(objSelect,itemValue)
  {
  	for (var i = 0; i < objSelect.options.length; i++) 
	{     
		if(itemValue==0)
		{
			break;
		}
        else if (objSelect.options[i].value == itemValue) {        
            objSelect.options[i].selected = true;              
            break;        
        }        
    } 
  }
};


var FloatBox2 = {
	_config : {'Title':'Confirmation','Question':'Are you sure you want to do this?','Button':'Ok','Button2':'Cancel','Callback':false,'Argu':null},
	_makeBorder : function(x)
	{	if (!this._instance) return;
		if (navigator.userAgent.indexOf('MSIE')>=0) css.add('table#PopWindow th','filter:alpha(opacity=60);');
		var _table=addx(this._instance,makex('table',{'id':'PopWindow'}));
		var _tbody=addx(_table,makex('tbody'));
		var _tr = addx(_tbody,makex('tr'));
		addx(addx(_tr,makex('td')),x);
		/*
		//Comment these codes, the float window will not has the shadow
		var _th = addx(_tr,makex('th',{'id':'PopTR'}));
		var _img= addx(_th,makex('img'));
		_img.src="/images/tabbar/shadow-tr.gif";
		var _tr = addx(_tbody,makex('tr'));
		var _th = addx(_tr,makex('th',{'id':'PopBL'}));
		addx(_th,makex('div',{'id':'PopB'}));
		addx(_tr,makex('th',{'id':'PopBR'}));
		*/
		var _w=this._instance.offsetWidth,_h=this._instance.offsetHeight;
		this._instance.style.width=_w+'px';this._instance.style.height=_h+'px';
		this._instance.style.left=(getDom('cw')-_w)/2 + 'px';
		this._instance.style.top=getDom('st')+Math.floor((getDom('ch')-_h)*2/5) + 'px';
		this._instance.style.visibility='visible';
		this._cover.style.visibility='visible';
	},
	draw : function(o)
	{	if (!this._holder)this._holder=document.body;
		if (this._instance) {this._holder.removeChild(this._instance);this._instance=null;}
		var select=document.getElementsByTagName('select'),div=makex('div');
		with (div.style)
		{	position='absolute';visibility='hidden';
			zIndex=2900;top='0px';left='0px';
			width=getDom('sw')+'px';
			height=Math.max(getDom('sh'),getDom('ch'))+'px';
			if (Version.ie&&!Version.ie7){background='none';filter='progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod="scale", src="/images/tabbar/back.png")';}
			else{background='url(/images/tabbar/back.png)';}
		}
		document.body.appendChild(div);
		if (Version.ie&&!Version.ie7&&select.length)
		{	var ifm = addx(div,makex('iframe',{'id':'PopBack'}));
			with(ifm.style){width='100%';height='100%';top='0px';left='0px';filter="Alpha(Opacity=0)";}
		}
		this._cover=div;
		this.windowresize=window.onresize;
		this.bodykeydown=document.body.onkeydown;
		//document.body.onkeydown=function(e){e=e||window.event;if (e.keyCode==9){e.cancelBubble=true;e.returnValue=false;if(div.currentStyle){e.keyCode=0;}return false;}}
		window.onresize=function(){div.style.height=Math.max(getDom('sh'),getDom('ch'))+'px';div.style.width=getDom('sw')+'px';}
		this._instance = makex('div');with (this._instance.style)
		{position='absolute';zIndex='2901';top='0px';visibility='hidden';}
		addx(this._holder,this._instance);this._makeBorder(o);
	},
	cancel : function()
	{	if (this._instance) {this._holder.removeChild(this._instance);this._instance=null;}
		document.body.removeChild(this._cover);
		window.onresize=this.windowresize;
		document.body.onkeydown=this.bodykeydown;
	}
}


var HomeFloatBox = {
	_config : {'Title':'Confirmation','Question':'Are you sure you want to do this?','Button':'Ok','Button2':'Cancel','Callback':false,'Argu':null},
	_makeBorder : function(x)
	{	if (!this._instance) return;
		if (navigator.userAgent.indexOf('MSIE')>=0) css.add('table#PopWindow th','filter:alpha(opacity=60);');
		var _table=addx(this._instance,makex('table',{'id':'PopWindow'}));
		var _tbody=addx(_table,makex('tbody'));
		var _tr = addx(_tbody,makex('tr'));
		addx(addx(_tr,makex('td')),x);
		/*
		//Comment these codes, the float window will not has the shadow
		var _th = addx(_tr,makex('th',{'id':'PopTR'}));
		var _img= addx(_th,makex('img'));
		_img.src="/images/tabbar/shadow-tr.gif";
		var _tr = addx(_tbody,makex('tr'));
		var _th = addx(_tr,makex('th',{'id':'PopBL'}));
		addx(_th,makex('div',{'id':'PopB'}));
		addx(_tr,makex('th',{'id':'PopBR'}));
		*/
		var _w=this._instance.offsetWidth,_h=this._instance.offsetHeight;
		this._instance.style.width=_w+'px';this._instance.style.height=_h+'px';
		this._instance.style.left=(getDom('cw')-_w)/2 + 'px';
		this._instance.style.top=getDom('st')+Math.floor((getDom('ch')-_h)*2/5) + 'px';
		this._instance.style.visibility='visible';
		this._cover.style.visibility='visible';
	},
	draw : function(o)
	{	if (!this._holder)this._holder=document.body;
		if (this._instance) {this._holder.removeChild(this._instance);this._instance=null;}
		var select=document.getElementsByTagName('select'),div=makex('div');
		with (div.style)
		{	position='absolute';visibility='hidden';
			zIndex=3900;top='0px';left='0px';
			width=getDom('sw')+'px';
			height=Math.max(getDom('sh'),getDom('ch'))+'px';
			if (Version.ie&&!Version.ie7){background='none';filter='progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod="scale", src="/images/tabbar/back.png")';}
			else{background='url(/images/tabbar/back.png)';}
		}
		document.body.appendChild(div);
		if (Version.ie&&!Version.ie7&&select.length)
		{	var ifm = addx(div,makex('iframe',{'id':'topPopBack'}));
			with(ifm.style){width='100%';height='100%';top='0px';left='0px';filter="Alpha(Opacity=0)";}
		}
		this._cover=div;
		this.windowresize=window.onresize;
		this.bodykeydown=document.body.onkeydown;
		//document.body.onkeydown=function(e){e=e||window.event;if (e.keyCode==9){e.cancelBubble=true;e.returnValue=false;if(div.currentStyle){e.keyCode=0;}return false;}}
		window.onresize=function(){div.style.height=Math.max(getDom('sh'),getDom('ch'))+'px';div.style.width=getDom('sw')+'px';}
		this._instance = makex('div');with (this._instance.style)
		{position='absolute';zIndex='3901';top='0px';visibility='hidden';}
		addx(this._holder,this._instance);this._makeBorder(o);
	},
	cancel : function()
	{	if (this._instance) {this._holder.removeChild(this._instance);this._instance=null;}
		document.body.removeChild(this._cover);
		window.onresize=this.windowresize;
		document.body.onkeydown=this.bodykeydown;
	}
}

var homeTrans = {
	_config:{'loading':"<p class='floatRuning' style='width:500px;'></p>"},
	createnew : function ()
	{
		var type = getx('type').value;
		var target = getx('target').value;
		var product = getx('product').value;
		var url = getx('url').value;
		var price = getx('price').value;
		var priceUnit = getx('priceUnit').value;
		var ongoing = getx('OnGo').value;
		var num = 		getx('num').value;
		var timeUnit = getx('timeUnit').value;
		var relation2 = getx('relation2').value;
		var enterRel2 = getx('enterRel2').value;
		var relation = getx('relation').value;
		var enterRel = getx('enterRel').value;
		var para = {'type':type,'target':target,'product':product,'url':url,'price':price,'priceUnit':priceUnit,'ongoing':ongoing,'num':num,'timeUnit':timeUnit,'relation2':relation2,'enterRel2':enterRel2,'relation':relation,'enterRel':enterRel};
		//$.post('/welcome/create',para,this.ShowC)
		$.ajax({
			beforeSend:this._beforeSend,
			success:this.ShowC,
			type:'POST',
			data:para,
			url:'/welcome/create'
		})
	},
	_beforeSend:function(){
		var _box=makex('div',{'id':'FloatRegister'});
		_box.innerHTML=homeTrans._config.loading;
		HomeFloatBox.draw(_box);
	},
	ShowC : function (response)
	{	
		HomeFloatBox.cancel();
		var json=eval('(' + response + ')');
		if(json.index == 'error')
		{
			tip = getx('htmlTip');
			if(tip)
			{
				tip.innerHTML = json.inner;
				tip.style.display = 'block';
			}
			else
			{
				return;
			}
			
		}
		else if(json.index == 'success')
		{
			tip = getx('htmlTip');
			if(tip){tip.style.display='none';}
			var _box=makex('div',{'id':'FloatRegister'});
			_box.innerHTML=json.inner;
			HomeFloatBox.draw(_box);
			if(getx('inviteCode'))
			{
				getx('inviteCode').focus();
			}
			else if(getx('firstName'))
			{
				getx('firstName').focus();
			}
			
			if(getx('account'))
			{
				var account = getx('account');
				var passwd = getx('passwd');
				if(account.value.trim() == '')
				{
					account.focus();
				}
				else
				{
					passwd.focus();
				}
				
			}
			
			if(getx('register').timezone)
			{
				var now = new Date();
				getx('register').timezone.value = 0-now.getTimezoneOffset();
			}
		}
		else if(json.index == 'Banned')
		{
			gotoUrl('Tips/Noresponse');
		}
		else
		{
			gotoUrl('/welcome/storedata');
			return;
		}
	},
	swapForm : function (type)
	{
		HomeFloatBox.cancel();
		if(type == 'login')
		{
			//$.post('/welcome/login',this.ShowC)
			$.ajax({
				beforeSend:this._beforeSend,
				success:this.ShowC,
				type:'POST',
				url:'/welcome/login'
			})
		}
		else if(type == 'register')
		{
			//$.post('/welcome/register',this.ShowC)
			$.ajax({
				beforeSend:this._beforeSend,
				success:this.ShowC,
				type:'POST',
				url:'/welcome/register'
			})
		}
		else
		{
			return;
		}
	},
	submitform : function (o,type)
	{
		if(type == 'register')
		{
			var url = '/welcome/register';
			var action = o.form.action.value;
			var timezone = o.form.timezone.value;
			var userId = o.form.userId.value;
			if(getx('inviteCode'))
			{
				var inviteCode = getx('inviteCode').value;
			}
			else
			{
				var inviteCode = '';
			}
			var firstName = o.form.firstName.value;
			var lastName = o.form.lastName.value;
			var email = o.form.email.value;
			var password = o.form.password.value;
			if(getx('CAPTCHAResponse'))
			{
				var CAPTCHAResponse = getx('CAPTCHAResponse').value;
			}
			else
			{
				var CAPTCHAResponse = '';
			}
			var para = {'action':action,'timezone':timezone,'userId':userId,'inviteCode':inviteCode,'firstName':firstName,'lastName':lastName,'email':email,'password':password,'CAPTCHAResponse':CAPTCHAResponse};
		}
		else if (type = 'login')
		{
			var url = '/welcome/login';
			var act = o.form.act.value;
			var account = o.form.account.value;
			var passwd = o.form.passwd.value;
			if(getx('CAPTCHAResponse'))
			{
				var CAPTCHAResponse = getx('CAPTCHAResponse').value;
			}
			var para = {'act':act,'account':account,'passwd':passwd,'CAPTCHAResponse':CAPTCHAResponse};
		}
		else 
		{
			return;
		}
		HomeFloatBox.cancel();
		$.ajax({
			beforeSend:this._beforeSend,
			success:this.ShowC,
			type:'POST',
			data:para,
			url:url
		})
	},
	cancelAction : function ()
	{
		HomeFloatBox.cancel();
	}
}

