// 重写整个 JS 文件
window.addEvent('load', show_fck);

/**
 * 模拟 prototype 的 document.getElementsByClassName() 方法
 */
document.getElementsByClassName = function(eleClassName) {
    var results  = [];
    var myclass  = new RegExp("\\b"+eleClassName+"\\b");
    var elements = this.getElementsByTagName("*");

    for(var i=0; i<elements.length; i++) {
        var classes = elements[i].className;
        if (myclass.test(classes))
            results.push(elements[i]);
    }
    return results;
}


function show_fck() {
    //alert('hello');
    var replaced = Array();
    var ii       = 0;
    var in_it    = 0;
    var obj      = document.getElementsByTagName('textarea');

    try {
        //alert( obj.length  );
        if( obj.length < 1 ) {
            return false;
        }

        for( i=0 ;i < obj.length; i++) {
            if( obj[i].type == "textarea" && obj[i].getAttribute("rel") == "fck" ) {
                in_it = 0;
                for( key in replaced ) {
                    if( replaced[key] == obj[i].id ) {
                        in_it = 1;
                    }
                }

                if( in_it == 0 ) {
                    //alert(obj[i].id);
                    replaced[ii] = obj[i].id;
                    ii++;

                    var oFCKeditor = new FCKeditor(obj[i].id) ;
                    oFCKeditor.BasePath = "/fckeditor/" ;
                    if(obj[i].getAttribute("toolbar")) {
                        oFCKeditor.ToolbarSet = obj[i].getAttribute("toolbar");
                    }
                    else {
                        oFCKeditor.ToolbarSet = "Basic";
                    }

                    // get the width and height of textarea
                    //alert( parseInt( obj[i].getAttribute("height") ) );
                    if(parseInt( obj[i].getAttribute("height") ) > 0) {
                        oFCKeditor.Height = parseInt( obj[i].getAttribute("height") ) ;
                        //alert( oFCKeditor.Height );
                    }

                    if(parseInt( obj[i].getAttribute("width") ) > 0) {
                        oFCKeditor.Width = parseInt( obj[i].getAttribute("width") ) ;
                    }
                    oFCKeditor.ReplaceTextarea() ;
                }
            }
        }
    }
    catch (e)
    {
    }
}

function do_fck( tid )
{
    var oFCKeditor = new FCKeditor( tid ) ;
    oFCKeditor.BasePath = "/fckeditor/" ;
    //alert( 'tool' + $(tid).getAttribute("toolbar") );
    if( $(tid).getAttribute("toolbar")  ) {
        oFCKeditor.ToolbarSet = $(tid).getAttribute("toolbar");
    }
    else {
        oFCKeditor.ToolbarSet = "Basic";
    }

    // get the width and height of textarea
    //alert( parseInt( obj[i].getAttribute("height") ) );
    if( parseInt( $(tid).getAttribute("height") ) > 0 ) {
        oFCKeditor.Height = parseInt( $(tid).getAttribute("height") ) ;
        //alert( oFCKeditor.Height );
    }

    if( parseInt( $(tid).getAttribute("width") ) > 0 ) {
        oFCKeditor.Width = parseInt( $(tid).getAttribute("width") ) ;
    }
    oFCKeditor.ReplaceTextarea() ;
}

//photo functions 
//
/////////////////
function add_photo(){
	
	$('div_photo').innerHTML = $('div_photo').innerHTML + '<br/><input type="file" name="file[]" id="file[]" value=""><input type="checkbox" name="foriginal[]" value="1"><br/>'

}


///////////////////////////////////////////////////////////////////////////////////////////////////////////

    function uploadImage(id) {
		
        var file = $(id);

        var uploadFormElement = $("uploadForm");

        //复制图片数据

        uploadFormElement.removeChild(uploadFormElement.imageFile);

        uploadFormElement.appendChild(file);

        document.getElementById("uploadImageDiv").innerHTML = '<img src="/images/waiting.gif" border="0"/>';

        //提交图片数据

        uploadFormElement.submit();

    }

	function uploadFinish(url,id){
	    document.getElementById("uploadImageDiv").innerHTML = '<input type="file" id="imageFile" name="imageFile" />';	
		$("photo_div").innerHTML = $("photo_div").innerHTML + '<img id=' + id + ' src="'+ url + '" width="150"/>';
		$("photo_div").innerHTML = $("photo_div").innerHTML +  '<a href="#" id="a_' + id + '" onclick="javascript:delPhoto(\'' + id + '\',\' '+ url +'\')">删除</a>';
		$("photo_div").innerHTML = $("photo_div").innerHTML + '<input type="hidden" value="'+ url + '" id="i_'+ id +'" name="ware_photo[]"/>';
	}

	function delPhoto(id,url){
		$(id).removeNode(true);
		$('a_' + id).removeNode(true);
		$('i_' + id).removeNode(true);
	}

	function getAjax(url){
		var myAjax = new Ajax(url, {
		method:'get',
        evalScripts: true
	 }).request();
	}


	function sendform(id){
		$('load').style.display = 'block';
		$('load_bg').style.display = 'block';
		//$(id).send({evalScript:true,method:'post',onComplete:function(){$('load').style.display = 'none';$('load_bg').style.display = 'none';}});
	}


	function del_ware(id){
		url = '/admin/delware/id/'+id;
		var myAjax = new Ajax(url, {
		method:'get',
        onComplete:function(){
			alert('删除成功');
			window.location.reload();
		}
	 }).request();
	}

	function recom_ware(id,flag){
		if(flag){
		url = '/admin/recomware/key/add/id/'+id;
		}else{
		url = '/admin/recomware/key/del/id/'+id;
		}
		var myAjax = new Ajax(url, {
		method:'get',
        onComplete:function(responseText){
			window.location.reload();
		}
	 }).request();
	}

//cart functions 

	function addCart(){
		//alert(Cookie.get('cart'));
		//Cookie.set('mycart','something',{duration:1});
		$('cart').style.display = 'block';
		$('size').style.visibility = 'hidden';
		$('color').style.visibility = 'hidden';
		queryString = get_form_value('cart-form');
		send_form('cart-form','cart');
	}

	function updateCart(id){
		url = '/cart/update/wid/' + id + '/num/' + $('num_'+id).value;
		//alert(document.cookie);
		var myAjax = new Ajax(url, {
		method:'get',
        onComplete:function(responseText){
					show(responseText,'cart');
		}
		}).request();
	}

	function delCart(id){
		url = '/cart/del/wid/' + id;
		var myAjax = new Ajax(url, {
		method:'get',
        onComplete:function(responseText){
					show(responseText,'cart');
		}
		}).request();
	}


function get_form_value(name) {
    var queryString = [];
    $(name).getFormElements().each(function(ele) {
        var name  = ele.name;
        var value = ele.value;
		var type  = ele.type;
        var rel   = ele.getProperty('rel');

        if (rel != null && rel == 'fck' && typeof(FCKeditorAPI)!='undefined') {
            var editor = FCKeditorAPI.GetInstance(name);
            queryString.push(encodeURIComponent(name)+'='+encodeURIComponent(editor.EditorDocument.body.innerHTML));
        }
        else if (type == 'checkbox' && ele.checked == false) {
        	
        }
		else {
			queryString.push(encodeURIComponent(name)+'='+encodeURIComponent(value));
		}
    });
    return queryString;
}

function send_form(name,div) {
    //fname = fname || "set_form";
    queryString = get_form_value(name);
    //options = {onComplete: set_form };
    //options = Object.extend(options || {}, {postBody: queryString.join('&'), method: 'post'});
    return new Ajax($(name).getProperty('action'), {
        postBody: queryString.join('&'),
        method: 'post',
        onComplete: function(responseText){
				show(responseText,div);
		}
    }).request();
}

function show(content,div){
	$(div).innerHTML = content;
}

function closeCart(){
		$('cart').style.display = 'none';
		$('size').style.visibility = 'visible';
		$('color').style.visibility = 'visible';	
}

function loadAddress(id){
		url = '/address/load/id/' + id;
		var myAjax = new Ajax(url, {
		method:'get',
        onComplete:function(responseText){
				obj = Json.evaluate(responseText);
				$('consignee').value = obj.consignee;
				$('address').value = obj.address;
				$('contact').value = obj.contact;
		}
		}).request();	
}


function check_registry(ntype){
	var reg = '';
	if(ntype == 'email' && !check_email($('email').value)) {
		$('email_info').innerHTML =  '<img src="/images/wrong.png" />邮箱格式不正确';
		return;
	}
	if(ntype == 'password') {
		if($('password').value != $('password_confirm').value){
			$('password_info').innerHTML =  '<img src="/images/wrong.png" />两次密码输入不一致';
		return;
		}
	}


		url = '/user/registry/action/' + ntype + '/' + ntype + '/' + $(ntype).value;
		var myAjax = new Ajax(url, {
		method:'get',
        onComplete:function(responseText){
				obj = Json.evaluate(responseText);
				result = obj.result;
			switch(ntype)
			{
			case 'username' :
				 if(!result) {
						$('username_info').innerHTML = '<img src="/images/right.png" />';
						$('flag').value = $('flag').value + 1;
					}else{
						$('username_info').innerHTML = '<img src="/images/wrong.png" />用户名已存在';
					}
			break;
			case 'email' : 
				 if(!result) {
						$('email_info').innerHTML = '<img src="/images/right.png" />';
						$('flag').value = $('flag').value + 1;
					}else{
						$('email_info').innerHTML = '<img src="/images/wrong.png" />邮箱已被注册';
					}
			break;
			case 'password' : 
				$('password_info').innerHTML = '<img src="/images/right.png" />';
				$('flag').value = $('flag').value + 1;
			break;
			}
		}
		}).request();	
	
}

function check_email(email){
var email = email;
var isemail=(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)
    if (email=="")
    {
		return false;
    }
	if (email.length>25)
	{
		return false
	}
	if (!isemail.test(email))
	{
	   return false;
	}
 return true;
}


function check_registry_form(){
	if($('flag').value != '0111' && $('password').value != $('password_confirm').value){
		alert('请正确填写信息');
	}else{
		$('registry').submit();
	}

}

function pay_dis(n){
	for(var i=1;i<3;i++){
		m="bank_d0"+i;
		$(m).style.display='none';
		l="li_bank_d0"+i;
		$(l).className = "";
	}
	$(n).style.display='block';
	$("r_"+n).checked = true;
	$("li_"+n).className = "title";
	this.className='';
}


function checkValue(name){
	var obj=document.getElementsByName(name);
	//alert(obj.length);
	for (var i=0;i<obj.length;i++)
	{
	   if(obj[i].checked)
	   return true;
	}
	return false;
}

function checkform(name){
	flag = false;
	$(name).getFormElements().each(function(ele) {
		if(ele.rel == "notnull" ) {
				if(ele.value == ''){
					alert(ele.alt + '不能为空');
					flag = false;
				}else{
					flag = true;
				}
			}	
	});
	return flag;
}

	function send_phone(value){
		if(value){
		url = '/index/send/phone/'+value;
		}else{
		alert('电话号码为空');
		return false;
		}
		var myAjax = new Ajax(url, {
		method:'get',
        onComplete:function(responseText){
			alert(responseText);
		}
	 }).request();
	}