/**
 *#! Data entry Javascript 2.0
 *@  XianminCat <XianminCat@gmail.com>
 */
(function(o){
	var d,check;
    d = o.document;
	check = {};
	check.opt = {
		encrypt: function(str, pwd)
		{
			if(pwd == null || pwd.length <= 0)
			{
				alert("Please enter a secret key with which to encrypt the message...");
				return null;
			}

			var prand = "";
			for(var i=0; i<pwd.length; i++)
			{
				prand += pwd.charCodeAt(i).toString();
			}

			var sPos = Math.floor(prand.length / 5);
			var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));
			var incr = Math.ceil(pwd.length / 2);
			var modu = Math.pow(2, 31) - 1;
			if(mult < 2)
			{
				alert("Algorithm cannot find a suitable hash. Please choose a different password. \nPossible considerations are to choose a more complex or longer password.");
				return null;
			}

			var salt = Math.round(Math.random() * 1000000000) % 100000000;
			prand += salt;
			while(prand.length > 10)
			{
				prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
			}
			prand = (mult * prand + incr) % modu;

			var enc_chr = "";
			var enc_str = "";
			for(var i=0; i<str.length; i++)
			{
				enc_chr = parseInt(str.charCodeAt(i) ^ Math.floor((prand / modu) * 255));
				if(enc_chr < 16) {
					enc_str += "0" + enc_chr.toString(16);
				} else enc_str += enc_chr.toString(16);
				prand = (mult * prand + incr) % modu;
			}

			salt = salt.toString(16);
			while(salt.length < 8)salt = "0" + salt;
			enc_str += salt;
			return enc_str;
		},
		ehomd5: function()
		{
			return "84d3dcb46d1f64b151606aa1a2c96ed1";
		},
		in_array: function(stringToSearch, arrayToSearch)
		{
			for (s = 0; s < arrayToSearch.length; s++) {
				thisEntry = arrayToSearch[s].toString();
				if (thisEntry == stringToSearch) {
					return true;
				}
			}
			return false;
		},
		getOut: function(str)
		{
			alert(str);
			self.location='http://jiajiaoban.com';
		},
		explode: function(str)
		{
			var typeArr = new Array;
			typeArr = str.split(',');
			return typeArr;
		},
		out: function()
		{
			return false;
		}
	};
	check.input = {
		option_studentname: function(str)
		{
			if(str.trim() === '') {
				alert('请填写姓名...');
				return false;
			}
			var ret=true;
			for(var i=0;i<str.length;i++)
				ret=ret && (str.charCodeAt(i)>=10000);
				if(ret == false){
				alert('姓名只能输入中文...');
				return false;
			}
			return true;
		},
		option_grade: function(str)
		{
			if(str === '' || str === null) {
				alert('请选择年级...');
				return false;
			}
			return true;
		},
		option_mobilephone: function(str)
		{
			var checkMobile = !(/(^13|^15|^17|^18)\d{9}$/.test(str));
			if(checkMobile){
				alert('请填写正确的手机号码...');
				return false;
			}
			return true;
		},
		option_message: function(str)
		{
			if(str === '' || str === null) {
				alert('请选择科目...');
				return false;
			}
			return true;
		}
	};

	inputClick = function(obStr)
	{
		if(typeof(obStr) === "object")
		{
			var sProtocol   = "http://"
			var sDoMain     = "data.izhikang.com/";
			var sController = "data_input/";
			var sAction     = "index";
			var bSubmit     = true;

			if(obStr.type && obStr.verify && obStr.mark_city && obStr.mark_web && obStr.mark_subject && obStr.mark_isMobile) {
				var cityArr = ['bj','sh','gz','sz','tj','nj','cd','hz','xa','wh','su','zz'];
				var webArr  = ['jjb','izk','bd','ed','tt'];
				var typeArr = ['studentname','parentname','grade','mobilephone','telphone','zkschool','school','email','message','address'];
				var testType  = check.opt.explode(obStr.type);
				var testVerify = check.opt.explode(obStr.verify);

				for (var v in testType) {
					if( ! check.opt.in_array(testType[v],typeArr))
					{
						check.opt.getOut('Error: The incoming values for "type" was error...');
						return false;
					}
				}

				for (var v in testVerify) {
					if( ! check.opt.in_array(testVerify[v],typeArr))
					{
						check.opt.getOut('Error: The incoming values for "verify" was error...');
						return false;
					}
				}

				if( ! check.opt.in_array(obStr.mark_city,cityArr))
				{
					check.opt.getOut('Error: The incoming values for "mark_city" was error...');
					return false;
				}

				if( ! check.opt.in_array(obStr.mark_web,webArr))
				{
					check.opt.getOut('Error: The incoming values for "mark_web" was error...');
					return false;
				}

				var verifyArr = check.opt.explode(obStr.verify);


				for (var val in verifyArr) {
					var option = 'check.input.option_'+ verifyArr[val];
					var value = d.getElementById(verifyArr[val]).value;

					if( ! eval(option)(value))
					{
						bSubmit = false;
						return;
					}
				}

				if(bSubmit)
				{
					var iRand = check.opt.encrypt(Math.random(),check.opt.ehomd5());
					d.getElementById('type').value = obStr.type;
					d.getElementById('irand').value = iRand;
					d.getElementById('mark_city').value=obStr.mark_city;
					d.getElementById('mark_web').value=obStr.mark_web;
					d.getElementById('mark_subject').value=obStr.mark_subject;
					d.getElementById('mark_isMobile').value=obStr.mark_isMobile;

					var oMyform = d.getElementById('catForm');
					oMyform.method="POST";
					oMyform.target="_blank";
					oMyform.action=sProtocol+sDoMain+sController+sAction;
					//alert(sProtocol+sDoMain+sController+sAction);
					oMyform.submit();
					var modal = d.getElementById('modal');
					modal.style.display = 'none';
				}else{
					check.opt.getOut('Error: All the incoming values for "verify" was error...');
					return false;
				}

			}else{
				check.opt.getOut('Error: The incoming values was not complete...');
				return false;
			}
		}else{
			check.opt.getOut('Error: The incoming value is not the object...');
			return false;
		}
	};
})(window);
