/*Top login form validation*/
    function valForm(theForm) {
    var reason = "";

      reason += validatePassword(theForm.password);
      reason += validatePhone(theForm.username);
      if (reason != "") {
        alert(reason);
        return false;
      }
      return true;
    }

    function validatePhone(fld) {

        var error = "";

       if (fld.value == "" || fld.value == 852) {
            error = "請輸入手機號碼.\n";
            fld.style.background = 'Yellow';
       }else{
            fld.style.background = 'White';
       }
        return error;
    }

    function validatePassword(fld) {
        var error = "";

        if (fld.value == "") {
            fld.style.background = 'Yellow';
            error = "請輸入密碼.\n";

        }else {
            fld.style.background = 'White';
        }
       return error;
    }


/*Register submit form*/
    function regForm(theForm){
        $('#username').val($('#selectList').val()+$('#phone').val());
        var reason = "";
        reason += validatePhone(theForm.phone);
        reason += validateEmail(theForm.email);
          if (reason != "") {
            alert(reason);
            return false;
          }
          return true;
        
    }
/*forget password*/
    function forgetForm(theForm){
        var reason = "";
        reason += validatePhone(theForm.username);
          if (reason != "") {
            alert(reason);
            return false;
          }
          return true;
    }

    function validateEmail(fld){
        var error = "";
        var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
        if (fld.value == "") {
            fld.style.background = 'Yellow';
            error = "請輸入電郵地址.\n";
        }
        return error;
    }
    function CheckEmail() {

email = document.f1.email.value


}
/*Top search form validation*/
    function valSearchForm(theForm) {
        var reason = "";

          reason += validateKeyword(theForm.keyword);
          if (reason != "") {
            alert(reason);
            return false;
          }
          return true;
    }
    
    function validateKeyword(fld) {
        var error = "";

        if (fld.value == "" || fld.value == "請輸入關鍵字") {
            fld.style.background = 'Yellow';
            error = "請輸入關鍵字.\n";

        }else {
            fld.style.background = 'White';
        }
       return error;

    }

	
$(function($) {
    //top book user menu
    $(".book_tools").hover(
             function () {
                $(".tools_items").show();
              },
              function () {
                $(".tools_items").hide();
              }
    );
    
        //paypal submit form
    $(".purchases_c a").click(function() {
        $('.purchases_c form').submit();
    });

})


//assign identifier to hidden input value
$(document).ready(function() {
    
var mac_add = Get_Cookie('24Device');
  if(mac_add == null){
      /*
        mac_add = macs.getMacAddressesJSON();
        if(mac_add!=null){
                $('#identifier').val(mac_add);
        }
        */
  }else{
        $('#identifier').val(mac_add);
  }
  
});

//detect browsers
var detect = navigator.userAgent.toLowerCase();
var windowsIE = (checkPlatform("msie") && checkPlatform("win"));

function checkPlatform(string)
{
    place = detect.indexOf(string) + 1;
    thestring = string;
    return place;
}

if (windowsIE) {
        document.write("<OBJECT codeBase=http://java.sun.com/update/1.6.0/jinstall-6-windows-i586.cab#Version=6,0,0,0 classid=clsid:5852F5ED-8BF4-11D4-A245-0080C6F74284 height=0 width=0>");
        document.write("<PARAM name=app VALUE=http://java.sun.com/javase/technologies/desktop/javawebstart/apps/notepad.jnlp>");
        document.write("<PARAM NAME=back VALUE=false>");
        document.write("</OBJECT>");
 }

if (navigator.mimeTypes && navigator.mimeTypes.length) {
        webstartAvailable = navigator.mimeTypes['application/x-java-jnlp-file'];
}

//get device id
/*
    var macs = {
        getMacAddressesJSON : function()
        {
            document.macaddressapplet.setSep( ":" );
            document.macaddressapplet.setFormat( "%02x" );
            var macs = eval( String( document.macaddressapplet.getMacAddressesJSON() ) );
	    if(macs[0]==''){
		macs[0]=macs[1];
	    }
            alert(macs[0]);
            setCookie(macs[0],365);
	    return macs[0];
        }
    }
*/
    function setCookie(mac_string,expires){
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+expires);
        document.cookie="24Device="+mac_string+";expires="+exdate.toUTCString();
    }

	function Get_Cookie( name ) {
		var start = document.cookie.indexOf( name + "=" );
		var len = start + name.length + 1;
		if ( ( !start ) &&
		( name != document.cookie.substring( 0, name.length ) ) )
		{
		return null;
		}
		if ( start == -1 ) return null;
		var end = document.cookie.indexOf( ";", len );
		if ( end == -1 ) end = document.cookie.length;
		return unescape( document.cookie.substring( len, end ) );
	}

