var xmlHttp;

function startRequest(chkurl){
    createXMLHttpRequest();
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.open("GET",chkurl, true);
    xmlHttp.send(null);
}

function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
}

function handleStateChange() {
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200){
			document.getElementById("submit1").disabled=false;
			document.getElementById("ssubmit").disabled=false;
			location.reload();
        }
    }
}

function startRequest2(chkurl){
    createXMLHttpRequest();
    xmlHttp.onreadystatechange = handleStateChange2;
    xmlHttp.open("GET",chkurl, true);
    xmlHttp.send(null);
}

function handleStateChange2() {
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200){
			location.reload();
        }
    }
}


function docut()
{
	//要裁减的图片名称
	$('#pc-width').val(getClipInfo('width'));
	$('#pc-horizontalInterval').val(getClipInfo('left'));

	$('#pc-verticalInterval').val(getClipInfo('top'));
	
	//文件名
	var filename = document.getElementById('filename').value;

	var pcwidth = document.getElementById("pc-width").value;

	var horizontalInterval = document.getElementById("pc-horizontalInterval").value;

	var verticalInterval = document.getElementById("pc-verticalInterval").value;

	var whichpic = document.getElementById('whichpic').value;

	var dir = document.getElementById('dir').value;

	//判断将要切成的图片是否大于285*380
	
	var cutwidth = pcwidth * (10/times);
	var cutheight = cutwidth * (380/285);
	
	if(cutwidth<285 || cutheight<380)
	{
		var alertmsg = "您切的尺寸为：宽"+cutwidth+",高"+cutheight+"，规定的最小尺寸为：宽285,高380";
		alert(alertmsg);
		return false;
	}

	var url = "cut.php?filename="+filename+"&pcwidth="+pcwidth+"&horizontalInterval="+horizontalInterval+"&verticalInterval="+verticalInterval+"&times="+times+"&whichpic="+whichpic + "&dir=" + dir;
	
	startRequest(url);
	document.getElementById("submit1").disabled=true;
	document.getElementById("ssubmit").disabled=true;
}


function setmainpic(pic,pos)
{	
	var url = 'ajax.php?action=setmainpic' + '&mainpic=' + pic + '&pos=' + pos;
	startRequest(url);
}

function setmainpic2(pic,pos)
{	
	var url = 'ajax.php?action=setmainpic' + '&mainpic=' + pic + '&pos=' + pos;
	startRequest2(url);
}

function msetmainpic(pos,wid)
{
	var url = 'ajax.php?action=msetmainpic'+'&pos='+pos + '&wid=' + wid;
	startRequest2(url);
}

/**
 *重置id为oid的区域的隐藏显示属性，如果其属性为显示则置为隐藏，如果其属性为隐藏则置为显示
 */
function display(oid)
{
	if($(oid).style.display == "none")
	{
		$(oid).style.display = "inline";
	}
	else if($(oid).style.display == "inline")
	{
		$(oid).style.display = "none";
	}
	
}

//添加友情链接检查

function flink_input_check()
{
	if($('linkname').value == "")
	{
		$('msg').innerHTML = "<font color='red'>请输入链接名称</color>";
		return false;
	}
	if($('linkurl').value == "")
	{
		$('msg').innerHTML = "<font color='red'>请输入链接地址</color>";
		return false;
	}

	if($('linkurl').value.indexOf("http://") == -1)
	{
		$('msg').innerHTML = "<font color='red'>地址格式不对，地址须带\"http://\"</color>";
		return false;
	}
	return true;
}

//上传商品附图时检查用户是否正确选择了上传文件
function fileupload_check()
{
/*
	if($('file1').value == "")
	{
		$('msg').innerHTML = "<font color='red'>请选择商品主图</color>";
		return false;
	}
	//判断主图大小
	if($('up1').width<285 || $('up1').height<380)
	{	
		$('msg').innerHTML = "<font color='red'>主图长宽不符合规格</color>";
		return false;
	}
	
	//判断附图一大小
	if($('file2').value != "")
	{
		if($('up2').width<285 || $('up2').height<380)
		{
			$('msg').innerHTML = "<font color='red'>附图1长宽不符合规格</color>";
			return false;
		}
	}

	//判断附图二大小
	if($('file3').value != "")
	{
		if($('up3').width<285 || $('up3').height<380)
		{
			$('msg').innerHTML = "<font color='red'>附图2长宽不符合规格</color>";
			return false;
		}
	}

	//判断附图三大小
	if($('file4').value != "")
	{
		if($('up4').width<285 || $('up4').height<380)
		{
			$('msg').innerHTML = "<font color='red'>附图3长宽不符合规格</color>";
			return false;
		}
	}
*/

	if(document.getElementById("file1").value == "")
	{
		document.getElementById("msg").innerHTML = "<font color='red'>请选择商品主图</color>";
		return false;
	}

/*
	//判断主图大小
	if(document.getElementById("up1").width<285 || document.getElementById("up1").height<380)
	{	
		document.getElementById("msg").innerHTML = "<font color='red'>主图长宽不符合规格</color>";
		return false;
	}

	//判断附图一大小
	if(document.getElementById("file2").value != "")
	{
		if(document.getElementById("up2").width<285 || document.getElementById("up2").height<380)
		{
			document.getElementById("msg").innerHTML = "<font color='red'>附图1长宽不符合规格</color>";
			return false;
		}
	}

	//判断附图二大小
	if(document.getElementById("file3").value != "")
	{
		if(document.getElementById("up3").width<285 || document.getElementById("up3").height<380)
		{
			document.getElementById("msg").innerHTML = "<font color='red'>附图2长宽不符合规格</color>";
			return false;
		}
	}

	//判断附图三大小
	if(document.getElementById("file4").value != "")
	{
		if(document.getElementById("up4").width<285 || document.getElementById("up4").height<380)
		{
			document.getElementById("msg").innerHTML = "<font color='red'>附图3长宽不符合规格</color>";
			return false;
		}
	}
*/
	return true;
}

//图片切换
function picchange(pid)
{	
	$("#pic1").css({'border':1+'px solid #CCCCCC'});
	$("#pic2").css({'border':1+'px solid #CCCCCC'});
	$("#pic3").css({'border':1+'px solid #CCCCCC'});
	$("#pic4").css({'border':1+'px solid #CCCCCC'});

	tmpid = pid.id;
	tmpid = '#'+tmpid;
	$(tmpid).css({'border':1+'px solid red'});

	document.getElementById('filename').value = pid.src;
	document.getElementById('filename').value = pid.src;
	document.getElementById('whichpic').value = pid.id;

	document.getElementById('t1').src = pid.src;
	document.getElementById('t2').src = pid.src;
	
	//调整图片显示大小
	var wid = pid.id+"w";
	var hid = pid.id+"h";

	var pwidth = document.getElementById(wid).value;
	var pheight = document.getElementById(hid).value;

	var n = 10;
	times = 10;
	while(pwidth * (times/n) > 440 )
	{
		times--;
	}
	pwidth = pwidth * (times/n);
	pheight = pheight * (times/n);

	$('#portMakerMachine, #portMakerMachine #clipee').css({'width':pwidth+'px','height':pheight+'px'});
	w = pwidth;
	h = pheight;
	document.getElementById('t1').width = pwidth;
	document.getElementById('t1').height = pheight;
	document.getElementById('t2').width = pwidth;
	document.getElementById('t2').height = pheight;
									
									$('#clipee').css('background-color','#000');
									$('#cliper').css({
										'display':'block',
										'left':(pwidth-108)/2+'px',
										'top':(pheight-144)/2+'px',
										'width':108+'px',
										'height':144+'px'
									});
									$('#cliper img').attr('src', $('#clipee img').attr('src'));
									$('#cliper img').css({
										'position' : 'absolute',
										'left': '-' + ((pwidth-108)/2) + 'px',
										'top': '-' + ((pheight-144)/2) + 'px'
									});
													
									$('#cliper').Resizable({
										minWidth:108,
										minHeight:144,
										maxWidth: pwidth,
										maxHeight: pheight,
										minTop: 0.1,
										minLeft: 0.1,
										maxRight: pwidth,
										maxBottom: pheight,
										dragHandle: true,
										ratio: 144/108,
										onDrag: function(x, y)
										{
											$('img', this).css({
												'left': '-' + x + 'px',
												'top': '-' + y + 'px'
											});
										},
										handlers: {
											se: '#resizeSE',
											ne: '#resizeNE',
											nw: '#resizeNW',
											sw: '#resizeSW'
										},
										onResize : function(size, position) {
											$('img', this).css({
												'left': '-' + position.left + 'px',
												'top': '-' + position.top + 'px'
											});
										}
									});
}

//图片发布时检查
function tupianrechk()
{
	if($('picname').value == "")
	{
		$('msg').innerHTML = "<font color='red'>请输入图片名称</color>";
		return false;
	}

	if($('huohao').value == "")
	{
		$('msg').innerHTML = "<font color='red'>请输入商品货号</color>";
		return false;
	}

	if($('price').value == "")
	{
		$('msg').innerHTML = "<font color='red'>请输入商品价格</color>";
		return false;
	}

	if($('tid').value == "")
	{
		$('msg').innerHTML = "<font color='red'>请先设置图集</color>";
		return false;
	}
	return true;
}

function tupianrechk2()
{
	if($('picname').value == "")
	{
		$('msg').innerHTML = "<font color='red'>请输入图片名称</color>";
		return false;
	}

	if($('tid').value == "")
	{
		$('msg').innerHTML = "<font color='red'>请先设置图集</color>";
		return false;
	}
	return true;
}

//删除确认
function del_confirm()
{
	var flag = confirm("你确认要删除吗?");
	if(flag == false)
	{
		return false;
	}
	return true;
}

//添加QQ验证
function qqchk()
{
	if($('qqnum').value == "")
	{
		$('msg').innerHTML = "<font color='red'>请输入qq号码</color>";
		return false;
	}
	if($('qqnum').value > 999999999 || $('qqnum').value < 10001 || !isNumber($('qqnum').value))
	{
		$('msg').innerHTML = "<font color='red'>请输入正确的qq号码</color>";
		return false;
	}
	return true;
}

//添加好友分组验证
function addfrigrochk()
{
	if($('frigro').value == "")
	{
		$('msg').innerHTML = "<font color='red'>请输入分组名称</color>";
		return false;
	}
	return true;
}

function isEmail(str)	//判断是否email地址
{	

	var myReg = /^[-_A-Za-z0-9]+@([_A-Za-z0-9]+\.)+[A-Za-z0-9]{2,3}$/;
		if(myReg.test(str)) return true;
			return false;
}

function isNumberOrLetter(s)	//判断是否是数字或字母
{
		var regu = "^[0-9a-zA-Z]+$";
		var re = new RegExp(regu);
		if (re.test(s))
		{
			return true;
		}
		else
		{
			return false;
		}
}

function isNumber(s)	//判断qq号码的正确性
{

	var regu = "^[0-9]+$";
	var re = new RegExp(regu);
	if (s.search(re) != -1) 
	{
		return true;
	}else
	{
		return false;
	}
}

//清空input原来的数值
function clsinput(id)
{
	document.getElementById(id).value = "";
}

/*
用途：检查输入字符串是否符合正整数格式
输入：
s：字符串
返回：
如果通过验证返回true,否则返回false
 
*/
function isNumber(s)
{
	var regu = "^[0-9]+$";
	var re = new RegExp(regu);
	if (s.search(re) != -1)
	{
		return true;
	}else
	{
		return false;
	}
}