/* Punycode Javascript Converter(PJC)

PJC is a punycode encoder and decoder for non-ASCII characters. Also, it provides 
methods to convert between punycode and International Domain Name(IDN).

jkode_pjc.js	v.0.1.0
*/
var base = 36; 
var t_min = 1; 
var t_max = 26; 
var skew = 38;
var damp = 700;
var initial_bias = 72; 
var initial_n = 128; 
var delimiter="-";
var max_int=2147483647;
var Punycode=function(){};
Punycode.prototype.chCode=function(str)
	{
		return str.length ? str.charCodeAt(0) : 0    
	}
Punycode.prototype.LastPos=function(a, b)
	{
		var s="", p;
		for(p=0; p < b.length; p++) 
		{
			s += b.charAt(b.length-p);
		}
		p = s.indexOf(a);
		var result=(p > -1) ? s.length-p : -1;
		return result;
	}
Punycode.prototype.return_i=function(i)
	{
		return (i < 0) ? 65536 + i : i;
	}
Punycode.prototype.SimpleConversionAU=function(d)
	{
		var a = 0;
		if(d < 26)
		{
			a = d + 97;
		}else{
			if (d < base) {a = d - 26 + 48;}
		}
		return a;
	}
Punycode.prototype.SimpleConversionUA=function(c)
	{
		var a = base;
		if ((c - 48) < 10)
		{
			a = c - 22
		}else{
			if ((c - 65) < 26)
			{
				a = c - 65
			}else{
				if((c - 97) < 26)
				{
					a = c - 97
				}
			}
		}
		return a
	}
Punycode.prototype.biasAdapt=function(delta,numpoints,firsttime)
	{
		var bias;
		var dt=delta,
		k=0;
		if(firsttime)
		{
			dt=parseInt(dt/damp)
		}else{
			dt=parseInt(dt/2)
		}
		dt+=parseInt(dt/numpoints);
		while(dt>parseInt(((base-t_min)*t_max)/2))
		{
			dt=parseInt(dt/(base-t_min));
			k+=base;
		}
		bias = k+parseInt(((base-t_min+1)*dt)/(dt+skew));
		return(bias);
	}
Punycode.prototype.isLessThen=function(character,n)
	{
		return (this.return_i(this.chCode(character))<n)
	}
Punycode.prototype.toUnicode=function(n, to_enc)
{
	var res = max_int;
	for(var t = 0; t < to_enc.length; t++)
	{
		var a = to_enc.charCodeAt(t);
		if ((a >= n) && (a < res)) res = a
	}
	return res
}
Punycode.prototype.Encode=function(to_encode)
{
	var to_enc=to_encode,
	$O="";
	try
	{
		var n = initial_n, init_bias = initial_bias, b=0, c;
		for (var l=0; l < to_enc.length; l++)
		{
			c=to_enc.charAt(l);
			if(this.isLessThen(c,initial_n))
			{
				$O+=c;b++
			}
		}
		if ($O.length<to_enc.length)
		{
			if($O.length>0)
			{
				$O+=delimiter
			}
		}
		var h=b,_1e=0;
		while (h<to_enc.length)
		{
			var m = this.toUnicode(n,to_enc);
			_1e+=this.return_i(m-n)*(h+1);
			n = m;
			for (var l=0;l<to_enc.length;l++)
			{
				c=to_enc.charAt(l);
				if(this.isLessThen(c,n))
				{
					_1e++
				}else{
					if (this.return_i(this.chCode(c))==n)
					{
						var q=_1e;
						k=base;
						while(k<=max_int)
						{
							if (k<=(init_bias+t_min))
							{
								t=t_min
							}else{
								if (k>=(init_bias+t_max))
								{
									t=t_max
								}else{
									t=k-init_bias
								}
							}
							if (q<t) break;
							$O+=String.fromCharCode(this.SimpleConversionAU(t+((q-t)%(base-t))));
							q=parseInt((q-t)/(base-t));
							k+=base
						}
						$O+=String.fromCharCode(this.SimpleConversionAU(q));
						first=false;
						if (h==b)
						{
							first=true;
						}
						init_bias=this.biasAdapt(_1e,h+1,first);
						_1e=0;
						h++
					}
				}
			}
			_1e++;n++
		}
	}
	catch(e)
	{
		$O=to_encode
	} 
	return $O.toLowerCase()
}
Punycode.prototype.EncodeDomain = function(domain_to_punycode)
{
	domain_to_punycode = domain_to_punycode.replace("@", "");
	var c=domain_to_punycode.split("."); 
	for (var i = 0; i < c.length; i++)
	{
		if (/[^\x00-\x7f]+/.test(c[i]))
		{
			c[i]="xn--" + Punycode.prototype.Encode(c[i])
		}
	} 
	c = c.join("."); 
	return "@"+c
}
Punycode.prototype.GetTail=function(imail)
{
		if (imail)
		{
			var new_iline;
			var iline = imail.replace(/\@([^\s\,\>]+)/g, this.EncodeDomain);
			return iline;
		}else{
			return imail;
		}
}

function changeInput(inp, new_inp)
{
   	inp = document.getElementById(inp);
    new_inp = document.getElementById(new_inp);
    
	if (inp && new_inp)
	{
        new_inp.name = inp.name;
        inp.name = "";
	}
	else{}
}

function  ReplaceValue()
{
		//Определение числового номера версии для IE
		 var ua = navigator.userAgent;
		 var IEoffset = ua.indexOf("MSIE ");
		 var MSIEVersionNumber = parseFloat(ua.substring(IEoffset+5, ua.indexOf(";", IEoffset)));
			if(MSIEVersionNumber < 6 )
			{
			}
			else
			{
				var email_1_new=document.getElementById("email_1_new");
				var email_2_new=document.getElementById("email_2_new");
				var email_1=document.getElementById("email_1");
				var email_2=document.getElementById("email_2");
				if (email_1_new && email_1 && email_2 && email_2_new){
					changeInput('email_1', 'email_1_new');
					changeInput('email_2', 'email_2_new');
					punycode = new Punycode(); 
					email_1_new.value = punycode.GetTail(email_1.value);
					email_2_new.value = punycode.GetTail(email_2.value);
				}
			}
    return true;
}

