// browser & os check
var agent=navigator.userAgent.toLowerCase();
var isIE=agent.match(/msie/);
var isIE6=agent.match(/msie 6/);
var isIE7=agent.match(/msie 7/);
var isIE8=agent.match(/msie 8/);
var isFF=agent.match(/firefox/);
var isOP=agent.match(/opera/);
var isChrome=agent.match(/chrome/);
var isXP=agent.match(/nt 5\.1/);
var isVISTA=agent.match(/nt 6\./);

// port domain
var _host_=window.location.host.toLowerCase();

if (_host_.match(/test/) || _host_.match(/tsecure/)) {
    var _domain_="http://test.booksory.net";
    var _secure_="http://tsecure.booksory.net";
} else {
    var _domain_="http://www.booksory.net";
    var _secure_="https://secure.booksory.net";
}

// add favorite
function addFavorite()
{
	var _url="http://www.booksory.net";
	var _title=document.title;

	if (isIE) {
		window.external.AddFavorite(_url,_title);
	} else if (isFF) {
		window.sidebar.addPanel(_title,_url,"");
	/*
	} else if (isOP) {
		var obj=document.createElement('a');
		obj.setAttribute('href',_url);
		obj.setAttribute('title',_title);
		obj.setAttribute('rel','sidebar');
		obj.click();
	*/
	} else {
		return false;
	}
}


// javascript link description default 
function linkDescriptionDefault()
{ 
    // favorite link
	$("a#link_quick_favorite").click(function() {
		addFavorite();
		return false;
	});

	// service help link
	$("a#link_about").click(function() {
		openLink(_domain_+'/help/join.php','open_help',818,600);
		return false;
	});

	// banner link
	$("a#link_camp").click(function() {
		openLink('http://park.bugsbook.com','open_link',800,600,1,1,1,1,1,1,1);
		return false;
	});
	$("a#link_bugsbook").click(function() {
		openLink('http://www.bugsbook.com','open_link',800,600,1,1,1,1,1,1,1);
		return false;
	});
	$("a#link_research").click(function() {
		openLink('http://www.gulnara.or.kr','open_link',800,600,1,1,1,1,1,1,1);
		return false;
	});
	$("a#link_gulnara").click(function() {
		openLink('http://www.gulnara.net','open_link',800,600,1,1,1,1,1,1,1);
		return false;
	});
	$("a#link_library").click(function() {
		openLink('http://lib.gulnara.or.kr','open_link',800,600,1,1,1,1,1,1,1);
		return false;
	});

	// company law link
	$("a#link_comp_term").click(function() {
		openLink(_domain_+'/policy/term.php','open_law',620,610);
		return false;
	});
	$("a#link_comp_privacy").click(function() {
		openLink(_domain_+'/policy/privacy.php','open_law',620,610);
		return false;
	});
	$("a#link_comp_law").click(function() {
		openLink(_domain_+'/policy/law.php','open_law',620,610);
		return false;
	});
	$("a#link_comp_deny").click(function() {
		openLink(_domain_+'/policy/deny.php','open_law',620,232);
		return false;
	});
	$("a#link_comp_trouble").click(function() {
		openLink(_domain_+'/policy/trouble.php','open_law',620,630);
		return false;
	});
	$("a#link_comp_rebate").click(function() {
		openLink(_domain_+'/policy/rebate.php','open_law',620,610);
		return false;
	});
}

// default form submit description
function formDescriptionDefault()
{
	// login form
	$("form#id_loginform").submit(function() {
		if (!checkForm("id","text","¾ÆÀÌµð¸¦ ÀÔ·ÂÇØ ÁÖ¼¼¿ä")) {
			return false;
		}

        var _id=$("input[name='id']");

        if (!(/^[_a-z\d]{4,16}$/i.test(_id.val()))) {
            alert("ÀÔ·ÂÇÏ½Å ¾ÆÀÌµð¿¡ Çã¿ëµÇÁö ¾Ê´Â ¹®ÀÚ¿­ÀÌ ÀÖ½À´Ï´Ù");
            _id.focus();
            return false;
        }

		if (!checkForm("pwd","text","ºñ¹Ð¹øÈ£¸¦ ÀÔ·ÂÇØ ÁÖ¼¼¿ä")) {
			return false;
		}

        if ($(":checkbox#id_secure:checked").val()) {
            $(this).attr("action",_secure_+"/login/loginPost.php");
        }

		return true;
	});

	// go family site form
	$("form#id_familyform").submit(function() {
		return false;
	});
	$("select#id_furl").change(function() {
		var _str="";
		$("select#id_furl option:selected").each(function() {
			if ($(this).val()) {
				openLink($(this).val(),'open_link',800,600,1,1,1,1,1,1,1);
			}
		});
	});

	// input background
	$("input.cls_input").focus(function() {
		$(this).css({"background":"#FFFFFF"});
	});
}

// open link page
function openLink(_url,_name,_width,_height,_history,_resizable,_toolbars,_status,_scroll,_mnbar,_location,_param) 
{  
    var swidth=screen.availWidth;
    var sheight=screen.availHeight;

    var position_x=(swidth-_width)/2.7;
    var position_y=(sheight-_height)/10;  

	var _history=(_history)?_history:0;
	var _resizable=(_resizable)?_resizable:0;
	var _toolbars=(_toolbars)?_toolbars:0;
	var _status=(_status)?_status:0;
	var _scroll=(_scroll)?_scroll:0;
	var _mnbar=(_mnbar)?_mnbar:0;
	var _location=(_location)?_location:0;
	var _param=(_param)?_param:'';

   	return window.open(_url,_name,'left='+position_x+',top='+position_y+',width='+_width+',height='+_height+',history='+_history+',resizable='+_resizable+',toolbar='+_toolbars+',status='+_status+',scrollbars='+_scroll+',menubar='+_mnbar+',location='+_location+_param); 
}

// check form validation
function checkForm(_name,_type,_txt)
{
	var _check=true;
	var _elem;

	switch (_type) {
		case "text" :
			_elem=$("input[name='"+_name+"']");
			if (!_elem.val()) {
				_check=false;
			}
			break;

		case "id" :
		case "pwd" :
			_elem=$("input[name='"+_name+"']");
			var _exp=/^[a-zA-Z0-9]{4,16}$/;
			if (!exp.test(_elem.val())) {
				_check=false;
			}
			break;

		case "select" :
			_elem=$("select[name='"+_name+"']");
			if (!$("select[name='"+_name+"'] option:selected").val()) {
				_check=false;
			}
			break;

		case "radio" :
			_elem=$(":radio[name='"+_name+"']:first");
			if (!$(":radio[name='"+_name+"']:checked").val()) {
				_check=false;
			}
			break;

		// checkbox's _name is class name
		case "check" :
			_elem=$("."+_name+":first");
			if (!$("."+_name+":checked").val()) {
				_check=false;
			}
			break;

		case "email" :
			_elem=$("input[name='"+_name+"']");
			var _exp=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/;
			if (!_exp.test(_elem.val())) {
				_check=false;
			}
			break;

		default :
			return false;
	}

	if (!_check) {
		alert(_txt);
		_elem.focus();
		return false;
	} else {
		return true;
	}
}

// close window
function closeWin()
{
    top.window.opener=top;
    top.window.open('','_parent', '');
    top.window.close();   
}

// window resize
function resizeWindow(w,h)
{
    var agent=navigator.userAgent.toLowerCase();
    var isIE=((agent.indexOf("msie")!=-1) && (agent.indexOf("opera")==-1));
    var isOP=((agent.indexOf("msie")==-1) && (agent.indexOf("opera")!=-1));
    var isIE7=agent.match(/msie 7/);
    var isIE8=agent.match(/msie 8/);
    var isFF3=agent.indexOf("firefox/3")>0;
    var isXP=agent.indexOf("nt 5.1")>0;
    var isVISTA=agent.indexOf("nt 6.")>0;
    var isChrome=agent.indexOf("chrome")>0;
    
    if (isIE8 || isFF3) {
        if (isVISTA) {      
            var _width=parseInt(parseInt(w)+10);
            var _height=parseInt(parseInt(h)+65);
        } else {
            var _width=parseInt(parseInt(w)+10);
            var _height=parseInt(parseInt(h)+80);
        }
    } else if (isIE7 || isChrome) {
        if (isVISTA) {      
            var _width=parseInt(parseInt(w)+10);
            var _height=parseInt(parseInt(h)+55);
        } else {
            var _width=parseInt(parseInt(w)+10);
            var _height=parseInt(parseInt(h)+70);
        }
    } else if (isIE) {
        var _width=parseInt(parseInt(w)+10);
        if (isXP) {
            var _height=parseInt(parseInt(h)+58);
        } else {        
            var _height=parseInt(parseInt(h)+49);
        }
    } else if (isOP) {
        var _width=parseInt(parseInt(w)+10);
        var _height=parseInt(parseInt(h)+43);
    } else {
        var _width=parseInt(parseInt(w)+10);
        var _height=parseInt(parseInt(h)+50);
    }
    
    window.resizeTo(_width,_height);
}
