var xmlHttp;

function ajaxfunction(sendurl,sendstr,backfunction)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX£¡");
  return;
  } 

xmlHttp.open("POST",sendurl,true);
xmlHttp.onreadystatechange=backfunction;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//xmlHttp.setRequestHeader("Content-Type", "text/xml"); 
xmlHttp.send(encodeURI(sendstr).replace(/\+/g, "%2B"));
}

function stateChanged()
{ 
if (xmlHttp.readyState==4)
{ 
if (xmlHttp.status ==200)
{
//document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
var xmlDoc=xmlHttp.responseXML.documentElement;
document.getElementById('txtHintusername').innerHTML=xmlDoc.getElementsByTagName('txtusername')[0].childNodes[0].nodeValue;
document.getElementById('txtHintuserpwd').innerHTML=xmlDoc.getElementsByTagName('txtuserpwd')[0].childNodes[0].nodeValue;
}
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

function qregclick()
{
    document.getElementById('spannousernameq').style.display='none';
    document.getElementById('spannouserpwdq').style.display='none';
    document.getElementById('spannouserpwdcq').style.display='none';
    document.getElementById('spannickname').style.display='none';
    document.getElementById('Qregusername').value=jtrim(document.getElementById('Qregusername').value);
    document.getElementById('Qregpasswd').value=jtrim(document.getElementById('Qregpasswd').value);
    document.getElementById('QregSecpasswd').value=jtrim(document.getElementById('QregSecpasswd').value);
    document.getElementById('Qregnickname').value=jtrim(document.getElementById('Qregnickname').value);
    document.getElementById('Qregphone').value=jtrim(document.getElementById('Qregphone').value);
    if (document.getElementById('Qregusername').value=='') 
    {
         document.getElementById('spannousernameq').innerHTML='Please enter your User Name(E-Mail)!';
         document.getElementById('spannousernameq').style.display='inline';
         document.getElementById('Qregusername').focus();
         return false;
    }
    if(document.getElementById('Qregusername').value.split('@').length!=2)
    {
         document.getElementById('spannousernameq').innerHTML='User Name must be a E-Mail address.';
         document.getElementById('spannousernameq').style.display='inline';
         document.getElementById('Qregusername').focus();
         return false;
    }
    if (document.getElementById('Qregpasswd').value=='')
    {
         document.getElementById('spannouserpwdq').style.display='inline';
         document.getElementById('Qregpasswd').focus();
         return false;
    }
    if (document.getElementById('QregSecpasswd').value=='')
    {
         document.getElementById('spannouserpwdcq').style.display='inline';
         document.getElementById('QregSecpasswd').focus();
         return false;
    }
    if (document.getElementById('Qregpasswd').value!=document.getElementById('QregSecpasswd').value)
    { 
         document.getElementById('spannouserpwdcq').style.display='inline';
         document.getElementById('Qregpasswd').value='';
         document.getElementById('QregSecpasswd').value='';
         document.getElementById('Qregpasswd').focus();
         return false;
    }
    if (document.getElementById('Qregnickname').value=='')
    {
         document.getElementById('spannickname').style.display='inline';
         document.getElementById('Qregnickname').focus();
         return false;
    }
    document.getElementById('divloadingQ').style.display='inline-block';
    var qregstr;
    qregstr='Qregusername='+document.getElementById('Qregusername').value;
    qregstr=qregstr+'&Qregpasswd='+document.getElementById('Qregpasswd').value;
    qregstr=qregstr+'&cptcountry='+document.getElementById('cptcountry').value;
    qregstr=qregstr+'&Qregnickname='+document.getElementById('Qregnickname').value;
    qregstr=qregstr+'&Qregphone='+document.getElementById('Qregphone').value;
    ajaxfunction('qreg.asp',qregstr,qregfunction);
}

function qregfunction()
{
   if (xmlHttp.readyState==4)
   {
    if (xmlHttp.status ==200)
     {
      document.getElementById('divloadingQ').style.display='none';
      if (xmlHttp.responseText=='Y')
       {window.location=document.getElementById('hiddenbackpage').value;}
      else
       {
        document.getElementById('spannousernameq').innerHTML=xmlHttp.responseText;
        document.getElementById('spannousernameq').style.display='inline';
        document.getElementById('Qregusername').focus();
       }
     }
    }
}


function loginclick()
{
   document.getElementById('spanloginusername').style.display='none';
   document.getElementById('spanloginpwd').style.display='none';
   document.getElementById('loginusername').value=jtrim(document.getElementById('loginusername').value);
   document.getElementById('loginpasswd').value=jtrim(document.getElementById('loginpasswd').value);
   if (document.getElementById('loginusername').value=='')
      {
        document.getElementById('spanloginusername').style.display='block';
        document.getElementById('spanloginusername').innerHTML='Please enter the User Name!';
        document.getElementById('loginusername').focus();
        return false;
      }
   if (document.getElementById('loginpasswd').value=='')
     { 
       document.getElementById('spanloginpwd').style.display='block';
        document.getElementById('spanloginpwd').innerHTML='Please enter the Password!';
       document.getElementById('loginpasswd').focus();
       return false;
     }
     document.getElementById('divloading').style.display='inline-block';
     ajaxfunction('login.asp','loginusername='+document.getElementById('loginusername').value+'&loginpasswd='+document.getElementById('loginpasswd').value,loginfunction);
}

function loginfunction()
{
   if (xmlHttp.readyState==4)
   {
    if (xmlHttp.status ==200)
     {
      document.getElementById('divloading').style.display='none';
      if (xmlHttp.responseText=='Y')
       {window.location=window.location=document.getElementById('hiddenbackpage').value;}
      else
       {
        if (xmlHttp.responseText=='N1')
        {document.getElementById('spanloginusername').innerHTML='User Name does not exist';
         document.getElementById('spanloginusername').style.display='block';
         document.getElementById('loginusername').focus();
         document.getElementById('loginpasswd').value='';}
        else
        {document.getElementById('spanloginpwd').innerHTML='Password error!';
         document.getElementById('spanloginpwd').style.display='block';
         document.getElementById('loginpasswd').focus();
         document.getElementById('loginpasswd').value='';}
       }
     }
    }
}

function logoutclick()
{
     ajaxfunction('logout.asp','',logoutfunction);
}

function logoutfunction()
{
   if (xmlHttp.readyState==4)
   { 
    if (xmlHttp.status ==200)
     {
       window.location='index.asp';
     }
    }
}


function accountclick(strpori)
{  var updateastr;
   if(strpori=='I')
   { document.getElementById('spannickname').style.display='none';
     document.getElementById('spanok1').style.display='none';
     document.getElementById('anickname').value=jtrim(document.getElementById('anickname').value);
     document.getElementById('aphone').value=jtrim(document.getElementById('aphone').value);
     if (document.getElementById('anickname').value=='')
      {
        document.getElementById('spannickname').style.display='block';
        document.getElementById('anickname').focus();
        return false;
      }
     document.getElementById('divloadingQ1').style.display='inline-block';
     updateastr='strpori=I&username='+ jtrim(document.getElementById('divusername').innerHTML) + '&cptcountry='+document.getElementById('cptcountry').value;
     updateastr=updateastr + '&anickname=' + document.getElementById('anickname').value + '&aphone=' + document.getElementById('aphone').value;

   }
   else
   {
    document.getElementById('spannouserpwdqo').style.display='none';
    document.getElementById('spannouserpwdq').style.display='none';
    document.getElementById('spannouserpwdcq').style.display='none';
    document.getElementById('spanok2').style.display='none';
    document.getElementById('apasswdo').value=jtrim(document.getElementById('apasswdo').value);
    document.getElementById('apasswdn').value=jtrim(document.getElementById('apasswdn').value);
    document.getElementById('aSecpasswdn').value=jtrim(document.getElementById('aSecpasswdn').value);
    if (document.getElementById('apasswdo').value=='')
    {
         document.getElementById('spannouserpwdqo').style.display='block';
         document.getElementById('apasswdo').focus();
         return false;
    }
    if (document.getElementById('apasswdn').value=='')
    {
         document.getElementById('spannouserpwdq').style.display='block';
         document.getElementById('apasswdn').focus();
         return false;
    }
    if (document.getElementById('aSecpasswdn').value=='')
    {
         document.getElementById('spannouserpwdcq').innerHTML='Pleast enter the Confirm New Password';
         document.getElementById('spannouserpwdcq').style.display='block';
         document.getElementById('aSecpasswdn').focus();
         return false;
    }
    if (document.getElementById('apasswdn').value!=document.getElementById('aSecpasswdn').value)
    {
         document.getElementById('spannouserpwdcq').innerHTML='Confirm New Password error!';
         document.getElementById('spannouserpwdcq').style.display='block';
         document.getElementById('apasswdn').value='';
         document.getElementById('aSecpasswdn').value='';
         document.getElementById('apasswdn').focus();
         return false;
    }
   document.getElementById('divloadingQ2').style.display='inline-block';
   updateastr='strpori=P&username='+ jtrim(document.getElementById('divusername').innerHTML) + '&apasswdo='+document.getElementById('apasswdo').value;
   updateastr=updateastr + '&apasswdn=' + document.getElementById('apasswdn').value;
   }
   ajaxfunction('updateaccount.asp',updateastr,updateaccountfunction);

}

function updateaccountfunction()
{
   if (xmlHttp.readyState==4)
   { 
    if (xmlHttp.status ==200)
     {
      document.getElementById('divloadingQ1').style.display='none';
      document.getElementById('divloadingQ2').style.display='none';
      if (xmlHttp.responseText=='Y1')
       {document.getElementById('spanok1').style.display='inline-block';
        document.getElementById('divtopnickname').innerHTML=document.getElementById('anickname').value;}
      else
       {
        if (xmlHttp.responseText=='N')
        {
         document.getElementById('spannouserpwdqo').innerHTML='Password error!';
         document.getElementById('spannouserpwdqo').style.display='block';
         document.getElementById('apasswdo').value='';
         document.getElementById('apasswdo').focus();
         return false;
        }
        else
        {
         document.getElementById('spanok2').style.display='inline-block';
        }
       }
     }
    }
}

function checkorderclick()
{
   document.getElementById('spanordernum').style.display='none';
   document.getElementById('spanorderphone').style.display='none';
   document.getElementById('ordernum').value=jtrim(document.getElementById('ordernum').value);
   document.getElementById('orderphone').value=jtrim(document.getElementById('orderphone').value);
   if (document.getElementById('ordernum').value=='')
      {
        document.getElementById('spanordernum').style.display='block';
        document.getElementById('ordernum').focus();
        return false;
      }
   if (document.getElementById('orderphone').value=='')
     { 
       document.getElementById('spanorderphone').style.display='block';
       document.getElementById('orderphone').focus();
       return false;
     }
     document.getElementById('divloading').style.display='inline-block';
     ajaxfunction('checkorder.asp','ordernum='+document.getElementById('ordernum').value+'&orderphone='+document.getElementById('orderphone').value,checkorderfunction);
}

function checkorderfunction()
{
   if (xmlHttp.readyState==4)
   {
    if (xmlHttp.status ==200)
     {
      document.getElementById('divloading').style.display='none';
      document.getElementById('divcheckorder').innerHTML=xmlHttp.responseText;
     }
    }
}

function morephotoclick(intspani,intcount)
{
for(var intkk=1;intkk<=intcount+2;intkk++)
{document.getElementById('spanmorephoto'+intkk).className='divmovephoto1';}
document.getElementById('spanmorephoto'+intspani).className='divmovephoto3';
}

function addmessageclick(strprocode)
{
document.getElementById('txtmessage').value=jtrim(document.getElementById('txtmessage').value);
if (document.getElementById('txtmessage').value=='')
 {
 document.getElementById('txtmessage').focus();
 return false;
 }
document.getElementById('divloadingm').style.display='inline-block';
ajaxfunction('addmessage.asp','txtmessage='+document.getElementById('txtmessage').value+'&txtprocode='+strprocode+"&txt1or2="+document.getElementById('txt1or2').value,addmessagefunction);

}

function addmessagefunction()
{
   if (xmlHttp.readyState==4)
   { 
    if (xmlHttp.status ==200)
     {
      document.getElementById('divloadingm').style.display='none';
      document.getElementById('divmessage').innerHTML=document.getElementById('divmessage').innerHTML+xmlHttp.responseText;
      document.getElementById('txtmessage').value='';
      if (document.getElementById('txt1or2').value==1)
        { document.getElementById('txt1or2').value=2;}
      else
        { document.getElementById('txt1or2').value=1;}
     }
    }
}

function jsgetstring(strform)
{
var strhavexlink='';

if(location.href.split('?').length>1)
 {
  if((location.href.split('?')[1]).split('&').length>1)
  { var str_arr=(location.href.split('?')[1]).split('&');
    for(var i=0;i<str_arr.length;i++)
    {
      if(str_arr[i].split('=')[0]==strform)
      {
       strhavexlink=str_arr[i].split('=')[1];
      }
     }
  }
  else
  {
    if((location.href.split('?')[1]).split('=')[0]==strform)
    {
      strhavexlink=(location.href.split('?')[1]).split('=')[1];
     }
  }

 }
  return strhavexlink;
}

function addtocartclick()
{
var addstr;
addstr='txtprocode='+document.getElementById('hiddentxtprocode').value;
addstr=addstr+'&TxtQTY='+document.getElementById('TxtQTY').value;
document.getElementById('divloading').style.display='inline-block';
ajaxfunction('addtocart.asp',addstr,addtocartfunction);
}

function addtocartfunction()
{
   if (xmlHttp.readyState==4)
   { 
    if (xmlHttp.status ==200)
     {
       document.getElementById('divloading').style.display='none';
         if (xmlHttp.responseText=='Y')
          {
          window.location='shoppingcart.asp';
          }
         else
         {alert(xmlHttp.responseText);}
     }
    }
}

function cartdelfunction()
{
   if (xmlHttp.readyState==4)
   { 
    if (xmlHttp.status ==200)
     {
      window.location='shoppingcart.asp';
     }
    }
}

function saveqtyclick(intid,inti,intoldqty,strstock)
{ajaxfunction('saveqty.asp','thisid='+intid+'&TxtQty='+document.getElementById('TxtQty'+inti).value+'&HiddenQty='+intoldqty+'&HiddenStock='+strstock,saveqtyfunction);}


function saveqtyfunction()
{
   if (xmlHttp.readyState==4)
   { 
    if (xmlHttp.status ==200)
     {
       window.location='shoppingcart.asp';
     }
    }
}

function CartShippingEMS(strAorE)
{
if(strAorE=='air')
{
document.getElementById('txtshipping').value='n/a';
document.getElementById('Hiddenshipping').value=0;
document.getElementById('txttotalprice').value='$' + (document.getElementById('Hiddenitemprice').value);
document.getElementById('amount').value=document.getElementById('Hiddenitemprice').value;
//document.getElementById('Hiddencartordernum').value=document.getElementById('Hiddenstrdatetime').value+'-'+document.getElementById('amount').value+'-Air';
document.getElementById('txtcartordernum').value=document.getElementById('Hiddenstrdatetime').value + '-' + document.getElementById('amount').value + '-Air';
document.getElementById('item_name').value='Cart order number is :'+document.getElementById('Hiddenstrdatetime').value+'-'+document.getElementById('amount').value+'-Air';
document.getElementById('item_number').value=document.getElementById('Hiddenstrdatetime').value+'-'+document.getElementById('amount').value+'-Air';
}
else
{
document.getElementById('txtshipping').value='$'+strAorE.split('@')[1];
document.getElementById('Hiddenshipping').value=strAorE.split('@')[1];
document.getElementById('txttotalprice').value='$' + (jparsefloat(document.getElementById('Hiddenitemprice').value,strAorE.split('@')[1],'and'));
document.getElementById('amount').value=jparsefloat(document.getElementById('Hiddenitemprice').value,strAorE.split('@')[1],'and');
//document.getElementById('Hiddencartordernum').value=document.getElementById('Hiddenstrdatetime').value+'-'+document.getElementById('amount').value+'-'+strAorE.split('@')[0];
document.getElementById('txtcartordernum').value=document.getElementById('Hiddenstrdatetime').value+'-'+document.getElementById('amount').value+'-'+strAorE.split('@')[0];
document.getElementById('item_name').value='Cart order number is :'+document.getElementById('Hiddenstrdatetime').value+'-'+document.getElementById('amount').value+'-'+strAorE.split('@')[0];
document.getElementById('item_number').value=document.getElementById('Hiddenstrdatetime').value+'-'+document.getElementById('amount').value+'-'+strAorE.split('@')[0];
}
}

function cartchangecountry()
{
document.getElementById('selshipping').options[parseInt(document.getElementById('selshipping').options.length-2)].value='DHL@'+document.getElementById('cptcountry').value.split('@')[2];
document.getElementById('selshipping').options[parseInt(document.getElementById('selshipping').options.length-1)].value='EMS@'+document.getElementById('cptcountry').value.split('@')[2];
document.getElementById('selshipping').options[parseInt(document.getElementById('selshipping').options.length-2)].text='By DHL($'+document.getElementById('cptcountry').value.split('@')[2]+')';
document.getElementById('selshipping').options[parseInt(document.getElementById('selshipping').options.length-1)].text='By EMS($'+document.getElementById('cptcountry').value.split('@')[2]+')';
if(document.getElementById('selshipping').value!='air')
{CartShippingEMS(document.getElementById('selshipping').value.split('@')[0]+'@'+document.getElementById('cptcountry').value.split('@')[2]);}
}

function bookshippingclick()
{

document.getElementById('spancartphone').style.display='none';
document.getElementById('txtcomments').value=jtrim(document.getElementById('txtcomments').value);
document.getElementById('txtphone').value=jtrim(document.getElementById('txtphone').value);
document.getElementById('txtemail').value=jtrim(document.getElementById('txtemail').value);
if (document.getElementById('txtphone').value=='') {document.getElementById('spancartphone').style.display='inline-block';document.getElementById('txtphone').focus();return false;}


if(document.getElementById('cptcountry').value.split('@')[1]=='N'){alert('Your country is not yet set up in our shipping module. Please contact us (send email to sales@liaow.com) to help you solve this issue.');return false;}
var bookstr;

bookstr='amount='+document.getElementById('amount').value;

bookstr=bookstr + '&hiddenshipping='+document.getElementById('Hiddenshipping').value;

bookstr=bookstr + '&txtcartordernum='+document.getElementById('txtcartordernum').value;
bookstr=bookstr + '&txtcomments='+document.getElementById('txtcomments').value;

bookstr=bookstr + '&txtphone=' + document.getElementById('txtphone').value;
bookstr=bookstr + '&cptcountry=' + document.getElementById('cptcountry').value;
bookstr=bookstr + '&txtemail=' + document.getElementById('txtemail').value;


ajaxfunction('bookshopping.asp',bookstr,bookshippingfunction);
}

function bookshippingfunction()
{
   if (xmlHttp.readyState==4)
   { 
    if (xmlHttp.status ==200)
     {
         if (xmlHttp.responseText=='Y')
          {
      document.getElementById('divloging').style.display='block';

      document.body.scrollTop=document.body.scrollHeight;
      document.documentElement.scrollTop=document.documentElement.scrollHeight;

      document.getElementById('formpayment').submit();
          }
        else
         {alert(xmlHttp.responseText);}
     }
    }
}

function addmessagejump(strback)
{
window.location='loginpage.asp?backpage='+strback+ '&isaddmessage=Y';
}

function orderdetailclick(strwdetail)
{
if(document.getElementById(strwdetail).style.display=='none' || document.getElementById(strwdetail).style.display=='')
{document.getElementById(strwdetail).style.display='block';}
else
{document.getElementById(strwdetail).style.display='none';}
}


function sendpwdlclick()
{
document.getElementById('divforgotpwd').style.display='block';
}

function sendpwdclick()
{
document.getElementById('divloadings').style.display='none';
document.getElementById('spanloginusernamef').style.display='none';
document.getElementById('loginusernamef').value=jtrim(document.getElementById('loginusernamef').value);
if (document.getElementById('loginusernamef').value=='')
   {
     document.getElementById('spanloginusernamef').style.display='block';
     document.getElementById('spanloginusernamef').innerHTML='Please enter the User Name!';
     document.getElementById('loginusernamef').focus();
     return false;
   }
document.getElementById('divloadings').style.display='inline-block';
ajaxfunction('sendpassword.asp','txtuseremail='+document.getElementById('loginusernamef').value,sendpwdfunction);
}

function sendpwdfunction()
{
   if (xmlHttp.readyState==4)
   { 
    if (xmlHttp.status ==200)
     {
         if (xmlHttp.responseText=='Y')
         {
          document.getElementById('divloadings').style.display='none';
          document.getElementById('spanloginusernamef').innerHTML='Password has been sent to you!';
          document.getElementById('spanloginusernamef').style.display='block';
         }
         else
         {
          document.getElementById('divloadings').style.display='none';
          document.getElementById('spanloginusernamef').innerHTML=xmlHttp.responseText;
          document.getElementById('spanloginusernamef').style.display='block';
          }
     }
    }
}



function jtrim(str){  //delete right and left sapce
 return str.replace(/(^\s*)|(\s*$)/g, "");
}
function jltrim(str){  //delete left space
 return str.replace(/(^\s*)/g,"");
}
function jrtrim(str){  //delete rightspace
 return str.replace(/(\s*$)/g,"");
}

 function istxtnum(event)
{
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
 if ((keyCode <   48   ||   keyCode >57) && (keyCode!=8) && (keyCode!=46))
{
  return false;
 }

}

var OldIntQty=1;
var intbettertotalprice=0;

function setqtyhave(intqty)
{
    if (isNaN(intqty)==true || intqty=='' || intqty==0)
    {
    document.getElementById('TxtQTY').value=OldIntQty;
    }
}

function QtyTotalPrice(intqty,intloadprice)
{
    if (isNaN(intqty)==true)
    {intqty=OldIntQty;
     document.getElementById('TxtQTY').value=OldIntQty;
    }
    if (intqty!='' && intqty!=0 && intqty!=OldIntQty)
    {
     if(parseInt(intloadprice)>100 && parseInt(intqty)>1)
     {intloadprice=intloadprice-parseInt((parseInt(intloadprice)/100));}
     document.getElementById('txtTotalprice').value=jparsefloat(jparsefloat(intqty,intloadprice,'product'),intbettertotalprice,'and');
     OldIntQty=intqty;
    }

}

function jparsefloat(float1,float2,themethods)
{
   var float11,float12,float21,float22,floatout;

   float1=''+float1;
   float2=''+float2;

   if(float1.split('.').length==2)
   {
      float11=parseInt(float1.split('.')[0],10);
      float12=float1.split('.')[1];

      if(float12.length==1)
      {
        float12=parseInt(float12,10)*10;
      }
      else
      {
      float12=parseInt(float12,10);
      }
      if(float1.split('-').length==2)
      {float12=-float12;}
    }
   else
   {
     float11=parseInt(float1.split('.')[0],10);
     float12=0;
   }

   if(float2.split('.').length==2)
   {
      float21=parseInt(float2.split('.')[0],10);
      float22=float2.split('.')[1];

      if(float22.length==1)
      {
        float22=parseInt(float22,10)*10;
      }
      else
      {
      float22=parseInt(float22,10);
      }
      if(float2.split('-').length==2)
      {float22=-float22;}
   }
   else
   {
      float21=parseInt(float2.split('.')[0],10);
      float22=0;
   }

   if(themethods=='and')
   {
      floatout=((float11+float21)*100+float12+float22)/100;
   }
   else if(themethods=='product')
   {
     floatout=((float11*float21*10000)+(float11*float22*100)+(float12*float21*100)+(float12*float22))/10000
   }

   return floatout;

}

function txtmaxlen(intmaxlen)
{
if (document.getElementById('txtmessage').value.length>intmaxlen)
{document.getElementById('txtmessage').value=document.getElementById('txtmessage').value.substring(0,intmaxlen);}
}
