// 外部 JavaScript by Rex

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function search_check() {
  myform = window.document.form_search;
  if ( myform.key2.value == "" ) {
    window.alert("請輸入要查詢的文字！");
    myform.key2.focus();
    return false;
  } else {
    return true;
  }
}

function select_product(p,s) {
  var a, b, c, sn, no, code
  myform = window.document.form_product_select;
  n = myform.elements['product_item_number'].value;
  for( i=1; i<=n; i++ ) {
    a = myform.elements['product_select_'+i].value;
    b = myform.elements['product_amount_'+i].value;
    c = myform.elements['product_code_'+i].value;
    if( i == 1 ) {
      sn = a;
      no = b;
      code = c;
    } else {
      sn = sn + ',' + a;
      no = no + ',' + b;
      code = code + ',' + c;
    }
    if( p == 1 && myform.elements['product_select_'+i].checked == false )
      myform.elements['product_select_'+i].checked = true;
    if( p == 2 && myform.elements['product_select_'+i].checked == true )
      myform.elements['product_select_'+i].checked = false;
  }
  if( p == 1 ) {
    $.get(
      '../check_product_select.php',
      { 
        'sn': sn,
        'no': no,
        'code': code,
        '_sid': s,
        'type': 1
      }
    );

    //var ajaxCheckID = new Ajax.Updater('ID_NOUSE','check_product_select.php',{ method: 'get',parameters: 'sn='+sn+'&no='+no+'&code='+code+'&type=1',evalScripts: true });
  }
  if( p == 2 ) {
    $.get(
      '../check_product_select.php',
      { 
        'sn': sn,
        'no': no,
        'code': code,
        '_sid': s,
        'type': 2
      }
    );

    //var ajaxCheckID = new Ajax.Updater('ID_NOUSE','check_product_select.php',{ method: 'get',parameters: 'sn='+sn+'&no='+no+'&code='+code+'&type=2',evalScripts: true });
  }
}

function select_one(i,s) {
  //myform = window.document.form_product_select;
  //t1 = myform.elements['product_select_'+i].value;
  //t2 = myform.elements['product_amount_'+i].value;
  //t3 = myform.elements['product_code_'+i].value;
  //if( myform.elements['product_select_'+i].checked == true ) t4 = 1;
  //if( myform.elements['product_select_'+i].checked == false ) t4 = 2;

  $.get(
    '../check_product_select.php',
    { 
      'sn': $('#product_select_'+i).val(),
      'no': $('#product_amount_'+i).val(),
      'code': $('#product_code_'+i).val(),
      '_sid': s,
      'type': function() { return $('#product_select_'+i).attr('checked') ? 1 : 2; }
    }
  );
  //var ajaxCheckID = new Ajax.Updater('ID_NOUSE','check_product_select.php',{ method: 'get',parameters: 'sn='+t1+'&no='+t2+'&code='+t3+'&type='+t4,evalScripts: true });
}

function input_num(i,s) {
  //myform = window.document.form_product_select;
  //t1 = myform.elements['product_select_'+i].value;
  //t2 = myform.elements['product_amount_'+i].value;
  //t3 = myform.elements['product_code_'+i].value;
  flag = $('#product_select_'+i).attr('checked');
  if( flag == true ) {
    $.get(
      '../check_product_select.php',
      { 
        'sn': $('#product_select_'+i).val(),
        'no': $('#product_amount_'+i).val(),
        'code': $('#product_code_'+i).val(),
        '_sid': s,
        'type': 3
      }
    );
    //var ajaxCheckID = new Ajax.Updater('ID_NOUSE','check_product_select.php',{ method: 'get',parameters: 'sn='+t1+'&no='+t2+'&code='+t3+'&type=3',evalScripts: true });
  }
}

function input_temp(i,s) {
  //myform = window.document.form_product_select;
  //t1 = myform.elements['product_select_'+i].value;
  //t2 = myform.elements['product_temp_'+i].value;
  //t3 = myform.elements['product_code_'+i].value;
  //var ajaxCheckID = new Ajax.Updater('ID_NOUSE','check_product_select.php',{ method: 'get',parameters: 'sn='+t1+'&temp='+t2+'&code='+t3+'&type=5',evalScripts: true });
  $.get(
    '../check_product_select.php',
    { 
      'sn': $('#product_select_'+i).val(),
      'temp': $('#product_temp_'+i).val(),
      'code': $('#product_code_'+i).val(),
      '_sid': s,
      'type': 5
    }
  );
}

function change_num(i,s) {
  //myform = window.document.form_product_select;
  //t1 = myform.elements['product_select_'+i].value;
  //t2 = myform.elements['product_amount_'+i].value;
  //t3 = myform.elements['product_code_'+i].value;
  $.get(
    '../check_product_select.php',
    { 
      'sn': $('#product_select_'+i).val(),
      'no': $('#product_amount_'+i).val(),
      'code': $('#product_code_'+i).val(),
      '_sid': s,
      'type': 3
    }
  );

  //var ajaxCheckID = new Ajax.Updater('ID_NOUSE','check_product_select.php',{ method: 'get',parameters: 'sn='+t1+'&no='+t2+'&code='+t3+'&type=3',evalScripts: true });
}

function unselect_product(s) {
  myform = window.document.form_product_select;
  n = myform.elements['product_item_number'].value;
  $.get(
    '../check_product_select.php',
    {
      '_sid': s,
      'type': 4
    }
  );
  //var ajaxCheckID = new Ajax.Updater('ID_NOUSE','check_product_select.php',{ method: 'get',parameters: 'type=4',evalScripts: true });
  for( i=1; i<=n; i++ ) {
    myform.elements['product_select_'+i].checked = false;
    myform.elements['product_amount_'+i].value = 1;
  }
}

function perview_product(s) {
  location.href = 'index.php?preview=1&_sid='+s;
}

function product_print(s) {
  location.href = 'index.php?choice=1&_sid='+s;
}

function print_choice(s) {
  myform = window.document.form_choice;
  if( myform.choice[0].checked == true && myform.choice[0].value == '1' )
    location.href = 'index.php?printA=1&_sid='+s;
  if( myform.choice[1].checked == true && myform.choice[1].value == '2' )
    location.href = 'index.php?printB=1&url=1&_sid='+s;
}

function form_clean() {
  myform = window.document.form_mail_1;
  myform.elements['name1'].value = '';
  myform.elements['name2'].value = '';
  myform.elements['addr'].value = '';
  myform.elements['email1'].value = '';
  myform.elements['tel'].value = '';
  myform.elements['note'].value = '';
}

function check_email1() {
  myform = window.document.form_mail_1;
  if( myform.elements['name1'].value == '' ) { window.alert("請輸入 客戶名稱"); myform.elements['name1'].focus(); return false; }
  if( myform.elements['name2'].value == '' ) { window.alert("請輸入 接洽人"); myform.elements['name2'].focus(); return false; }
  if( myform.elements['email1'].value == '' ) { window.alert("請輸入 E-mail"); myform.elements['email1'].focus(); return false; }
  if( !myform.elements['email1'].value.match(/^\S+@\S+\.\S+$/) ) { window.alert("請輸入正確的【E-mail】"); myform.elements['email1'].focus(); return false; }
  if( myform.elements['tel'].value == '' ) { window.alert("請輸入 電話"); myform.elements['tel'].focus(); return false; }
  return true;
}

function check_email2() {
  myform = window.document.form_mail_2;
  if( myform.elements['email2'].value == '' ) { window.alert("請輸入 收件人E-mail"); myform.elements['email2'].focus(); return false; }
  if( !myform.elements['email2'].value.match(/^\S+@\S+\.\S+$/) ) { window.alert("請輸入正確的【E-mail】"); myform.elements['email2'].focus(); return false; }
  return true;
}

function check_cntect() {
  myform = window.document.form_contect;
  if( myform.elements['name'].value == '' ) { window.alert("請輸入 聯絡人"); myform.elements['name'].focus(); return false; }
  if( myform.elements['tel'].value == '' ) { window.alert("請輸入 聯絡電話"); myform.elements['tel'].focus(); return false; }
  if( myform.elements['mail'].value == '' ) { window.alert("請輸入 E-mail"); myform.elements['mail'].focus(); return false; }
  return true;
}

function check_prize() {
  myform = window.document.form_prize;
  if( myform.elements['money'].value == '' ) { window.alert("請輸入 摸彩品預算金額"); myform.elements['money'].focus(); return false; }
  if( myform.elements['type'].value == '' ) { window.alert("請輸入 獎別"); myform.elements['type'].focus(); return false; }
  if( myform.elements['number'].value == '' ) { window.alert("請輸入 獎品"); myform.elements['number'].focus(); return false; }
  return true;
}

function check_select() {
  myform = window.document.form_product_select;
  if( myform.elements['product_select'].checked == false ) { window.alert("請輸入選擇產品"); return false; }
  return true;
}

function ShowCost(p) {
  myform = window.document.form_product_select;
  flag = myform.elements['CostCheck_'+p].checked;
	if( flag ) document.all['CostShow_'+p].style.visibility = "visible"; 
	else document.all['CostShow_'+p].style.visibility = "hidden";
}

