﻿// JavaScript Document

//验证 验证码 是否通过
var code_is_right = false;
  
function sendRequestCode( url )   
{						
	xmlHttpRequest.open("GET", url, false );
	
	// 判断是不是用火狐浏览器打开的
	var isFF = window.addEventListener;
	
	if( !isFF )
	{	
		xmlHttpRequest.onreadystatechange = function ()
		{
   			if (xmlHttpRequest.readyState == 4) 
   			{
    			if (xmlHttpRequest.status == 200) 
    			{								
					disposeCode();
				}
				else
   				{
					//window.alert("返回信息错误");
   				}
			}
   			else
   			{
				//window.alert("正在处理，请稍候......");
   			}
		};	
	}
	
	xmlHttpRequest.send(null);
	
	if( isFF )
	{
		disposeCode();
	}
}

/*
 * 处理请求信息
 */
function disposeCode()
{	
    if( xmlHttpRequest.responseText == 0 )
    {
      	code_is_right = false;    				 
    }
	else if( xmlHttpRequest.responseText == 1 )
	{
     	code_is_right = true;			 	
    }
}


//检验验证码的正确性
function checkValidateCode( validate_code )
{	
	var url = context_path + "/checkValidateCode.do?random=" + Math.random() + "&validate_code=" + validate_code;
	sendRequestCode(url);	
}



/*
 * 创建验证码
 * @img_code_id 为验证码SRC的ID
 */
function createCode( img_code_id )
{	
	var img_code = document.getElementById(img_code_id);     //获取验证码的img
	img_code.src = context_path + "/createValidateCode.do?random=" + Math.random();  //创建验证码 
}

// 对默认有灰色字提示输入的验证码表单
function createCode1( img_code_id , input_code_id )
{	
	createCode( img_code_id );
	document.getElementById(input_code_id).value="请输入验证码";
	document.getElementById(input_code_id).focus();
}

// 对默认无灰色字提示输入的验证码表单
function createCode2( img_code_id , input_code_id )
{	
	createCode( img_code_id );
	document.getElementById(input_code_id).value="";
	document.getElementById(input_code_id).focus();
}







/** 
 * 用来读取特定的cookie值,如果不存在返回null
 * 
 */
function getCookie( cookie_name )
{
	var allcookies = document.cookie;
	var cookie_pos = allcookies.indexOf(cookie_name);
	if (cookie_pos != -1)
	{
		cookie_pos += cookie_name.length + 1;
		var cookie_end = allcookies.indexOf(";", cookie_pos);
		if (cookie_end == -1)
		{
			cookie_end = allcookies.length;
		}
		return unescape(allcookies.substring(cookie_pos, cookie_end));
	}
	return null;
}

