var wx=0;

var sendReq = getXmlHttpRequestObject();
var _documentid='content';
//var act=document.getElementById('userid').value;
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		mosila=true;
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert('Status: Cound not create XmlHttpRequest Object.  Consider upgrading your browser.');
	}
}


var ShipAddress1 = "";
var ShipAddress2 = "";
var ShipCity = "";
var ShipCountry = "";
var ShipState = "";
var ShipStateIndex = 0;
var ShipZip = "";

//var ShipFirst = "";
//var ShipLast = "";
//var ShipEmail = "";
//var ShipCompany = "";
//var ShipTelephone = "";

function InitSaveVariables(form4) {
ShipAddress1 = form4.sadd1.value;
ShipAddress2 = form4.sadd2.value;
ShipCity = form4.scity.value;
ShipCountry = form4.scountry.value;
ShipStateIndex = form4.sstate.selectedIndex;
ShipState = form4.sstate.value;
ShipZip = form4.szip.value;

//ShipFirst = form.ShipFirst.value;
//ShipLast = form.ShipLast.value;
//ShipEmail = form.ShipEmail.value;
//ShipCompany = form.ShipCompany.value;
//ShipTelephone = form.ShipTelephone.value;




}

function handleSendContact() {
	if (sendReq.readyState == 4 || sendReq.readyState == 0) 
   {
    if (sendReq.status == 200) 
    {
      var docid=new Array();
      var i;
      docid=_documentid.split('~');
      
     
       var responsetxt=new Array();
       responsetxt=sendReq.responseText.split('|');
       
       for(i=0;i<docid.length;i++)
      {
        document.getElementById(docid[i]).innerHTML=''+responsetxt[i];
       }
    } //else alert('There was a problem with the request.');
   }else {alert('There was a problem with the request '+http_request.readyState);
	//if(act=='login1') window.location='index.php';
	//window.location.reload();
	//showpic(act);
   return false;}
   doRequest('get', 'include/pos/get_detail.php?ui='+ui,'text', 'content2');
}// JavaScript Document


function changed(kode,pi){
//alert(document.getElementById('s_detail').value);
var str=document.getElementById('s_detail').value;
if(str.search(/PHONE/)==-1 && str.search(/FAX/)==-1 && str.search(/LOCATION/)==-1){
	//alert(pi);
document.getElementById('adddetail2').innerHTML='<input name="t_detail" type="text" id="t_detail" size="40" /><input name="indi" type="hidden" id="indi" value="1" />&nbsp;<a href="#" onclick="saveDetail(\''+kode+'\',\''+pi+'\'); return false;"><img src="../images/check.jpg" border="0" title="Save" /></a>';
}else if((str.search(/PHONE/)!=-1 || str.search(/FAX/)!=-1)&& str.search(/LOCATION/)==-1){
document.getElementById('adddetail2').innerHTML=' <input name="t_n1" type="text" id="t_n1" size="5" maxlength="10" />&nbsp;'+
'<input name="t_n2" type="text" id="t_n2" size="10" maxlength="10" />&nbsp;'+
'<input name="t_n3" type="text" id="t_n3" size="20" maxlength="60" /><input name="indi" type="hidden" id="indi" value="2" />[country]-[area]-[phone number]&nbsp;<a href="#" onclick="saveDetail(\''+kode+'\',\''+pi+'\'); return false;"><img src="../images/check.jpg" border="0" title="Save" /></a>';
}else{
document.getElementById('adddetail2').innerHTML='<img src="images/lbloading.gif" alt="loading" width="32" height="32">';
doRequest('get', 'include/add_address.php?ui='+kode+'&pi='+pi,'text', 'adddetail2');
}
}

function searchParty(){
	//alert("tes");
	if (document.getElementById('custx_id').checked == false && document.getElementById('custx_name').checked==false && document.getElementById('compx').checked==false && document.getElementById('countryx').checked==false && document.getElementById('classx').checked==false && document.getElementById('userx').checked==false && document.getElementById('emailx').checked==false && document.getElementById('disax').checked==false && document.getElementById('checkZip').checked==false) 
	{
	alert('Please choose search by option!');
	return false;
	}
	else{
		document.getElementById('form1').submit();
	}

}

function beforeNewCus(){
		sl=true;
		showDialog();
		document.getElementById('xx').style.width='1px';
		document.getElementById('content_max').innerHTML='';
		showSlow1()
}
function showSlow1(){
		wx += 15;
		if (wx <= 75) {
			document.getElementById('xx').style.width = wx + '%';
			document.getElementById('xx2').style.height = wx * 10 + 'px'
			document.getElementById('xx').style.height = wx * 10 + 'px'
			timer = setTimeout("showSlow1()", 1);
		}else
		{
			document.getElementById('xx').style.width = 93 + '%';
			document.getElementById('xx2').style.height=max_h-28;
			document.getElementById('xx').style.height=max_h;
			newCus();
			sl=false;
			clearTimeout(timer);
			wx = 0;
		}
}

function showDialog(){
var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }

var viewportwidth;
 var viewportheight;
 
 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
 
 if (typeof window.innerWidth != 'undefined')
 {
      viewportwidth = window.innerWidth,
      viewportheight = window.innerHeight
 }
 
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

 else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
 {
       viewportwidth = document.documentElement.clientWidth,
       viewportheight = document.documentElement.clientHeight
 }
 
 // older versions of IE
 
 else
 {
       viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
       viewportheight = document.getElementsByTagName('body')[0].clientHeight
 }


var winl=(viewportwidth-800)/2;
var wint=(viewportheight-500)/2+scrOfY;


/*document.getElementById('pic').style.left=winl+'px';
document.getElementById('pic').style.top=wint+'px';
document.getElementById('pic').style.display='block';*/
ph=new Array();
ph=getPageSizeWithScroll();
if(ph[1]<viewportheight) ph[1]=viewportheight;
document.getElementById('maximize').style.height=ph[1]+'px';
document.getElementById('maximize').style.width=ph[0]+'px';
document.getElementById('maximize1').style.height=ph[1]+'px';
document.getElementById('maximize1').style.width=ph[0]+'px';
//alert(ph[0]);
//alert(ph[1]);
//alert(viewportheight);
if (890 <= (viewportheight - 30)) {
	document.getElementById('xx').style.height = '890px';
	document.getElementById('xx2').style.height='868px';
	max_h=890;
	
}
else {
	document.getElementById('xx').style.height = (viewportheight - 45) + 'px';
	document.getElementById('xx2').style.height=(viewportheight - 65) + 'px';
	max_h=viewportheight - 45;
}
//document.getElementById('xx2').style.height='868px';
document.getElementById('xx').style.margin='5px auto';
document.getElementById('blok_frame').style.display='block';
document.getElementById('xx3').innerHTML='<div onclick="document.getElementById(\'maximize\').style.display=\'none\'; document.getElementById(\'blok_frame\').style.display=\'none\'; return false;" ><table width="100%" border="0" cellspacing="0" cellpadding="0">'+
              '<tr>'+
                '<td width="30" align="right"><img src="images/pos/arrow1.jpg" alt="Arrow" width="29" height="30" /></td>'+
                '<td height="29" align="center" valign="middle"><a href="#" onclick="document.getElementById(\'maximize\').style.display=\'none\'; document.getElementById(\'blok_frame\').style.display=\'none\'; return false;" style="color:#ff8400; text-decoration:none;">Back To Main Screen</a></td>'+
                '<td width="30" align="left"><img src="images/pos/arrow.jpg" alt="Arrow" width="31" height="30" /></td>'+
              '</tr>'+
            '</table></div>';
if(sl=='') slow(); else document.getElementById('xx').style.width='93%';
document.getElementById('xx2').style.backgroundColor='#65aff8';
document.getElementById('maximize').style.display='block';
}

function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}

function newCus(){
	
	sl=true;
	showDialog();
	sl=false;
		document.getElementById('content_max').innerHTML='<table width="100%" border="0" cellspacing="0" cellpadding="0">'+
    '<tr>'+
      '<td height="500" align="center" valign="middle"><img src="images/loading.gif" width="30" height="30" alt="loading" style="filter:alpha(opacity=100); opacity:1;" /></td>'+
    '</tr>'+
 ' </table>';
	doRequest('get', 'include/new_customer1.php', 'text', 'content_max');
}

function doSubmit(){
	
	var gender='';
		if(document.form4.gender[0].checked){
		gender=document.form4.gender[0].value;	
		}else if(document.form4.gender[1].checked){
			gender=document.form4.gender[1].value;
		}
	//alert('test '+gender);
	
	
	var class_pty='';
		//alert('test2');
		for (var i=0; i < document.form4.class_pty.length; i++){
			if (document.form4.class_pty[i].checked){
					class_pty=document.form4.class_pty[i].value;
			}
		}	
	
	var filter=/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	if(gender==''){
	alertMsg('Please select gender!','');	
	}else if(document.getElementById('fname').value==''){
		alertMsg('Please input First name!','');
		document.getElementById('fname').focus();
	}else if(document.getElementById('company').value==''){
		alertMsg('Please input company name!','');
		document.getElementById('company').focus();
	}else if(class_pty==''){
		alertMsg("Please select classification!",'');
	}else if(document.getElementById('email').value==''){
		alertMsg('Please input email address!','');
		document.getElementById('email').focus();
	}else if(!filter.test(document.getElementById('email').value)){
		alertMsg('Please input the correct email','');
		document.getElementById('email').foucs();
	}else if(document.getElementById('psw1').value==''){
		alertMsg('Please input password!','');
		document.getElementById('psw1').focus();
	}else if(document.getElementById('psw2').value==''){
		alertMsg('Please input confirm password!','');
		document.getElementById('psw2').focus();
	}else if(document.getElementById('psw1').value!=document.getElementById('psw2').value){
		alertMsg('Password and Confirm password must be the same!','');
		document.getElementById('psw1').focus();
	}else if(document.getElementById('p1').value=='' || document.getElementById('p2').value=='' || document.getElementById('p3').value=='' ){
		alertMsg('Please input phone number!','');
		document.getElementById('p1').focus();
	}else if(document.getElementById('badd1').value==''){
		alertMsg('Please input Billing address 1!','');
		document.getElementById('badd1').focus();
	}else if(document.getElementById('bcity').value==''){
		alertMsg('Please input Billing address city!','');
		document.getElementById('bcity').focus();
	}else if(document.getElementById('bzip').value==''){
		alertMsg('Please input billing address zip code!','');
		document.getElementById('bzip').focus();
	}else if(document.getElementById('bcountry').value==''){
		alertMsg('Please input billing address country!','');
		document.getElementById('bcountry').focus();
	}else if(document.getElementById('sadd1').value==''){
		alertMsg('Please input shipping address 1!','');
		document.getElementById('sadd1').focus();
	}else if(document.getElementById('scity').value==''){
		alertMsg('Please input shipping address city!','');
		document.getElementById('scity').focus();
	}else if(document.getElementById('szip').value==''){
		alertMsg('Please input shipping address zip code!','');
		document.getElementById('szip').focus();
	}else if(document.getElementById('scountry').value==''){
		alertMsg('Please input shipping address country!','');
		document.getElementById('scountry').focus();
	}else if (document.getElementById('c_owner').checked == false && document.getElementById('c_man').checked == false && document.getElementById('c_emp').checked == false && document.getElementById('c_buyer').checked == false && document.getElementById('c_sale').checked == false) {
		alertMsg('Please choose position in company','');
	}else{
	doSave1();
	}
	
}

function doSave1(){
	//act=document.getElementById('act').value;
	//alert('test1');
	if (sendReq.readyState == 4 || sendReq.readyState == 0) {
		sendReq.open("POST", 'include/pos/new_customer.php', true);
		sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		sendReq.onreadystatechange = handleSendContact;
		var param = 'username=' + document.getElementById('email').value;
		param += '&firstname='+document.getElementById('fname').value;
		param +='&lastname='+document.getElementById('lname').value;
		param +='&company='+document.getElementById('company').value;
		param +='&phone_country='+document.getElementById('p1').value;
		param +='&phone_area='+document.getElementById('p1').value;
		param +='&phone_number='+document.getElementById('p3').value;
		param +='&password='+document.getElementById('psw1').value;
		param +='&bill_address1='+document.getElementById('badd1').value;
		param +='&bill_address2='+document.getElementById('badd2').value;
		param +='&bill_city='+document.getElementById('bcity').value;
		param +='&bill_state='+document.getElementById('bstate').value;
		param +='&bill_zip='+document.getElementById('bzip').value;
		param +='&bill_country='+document.getElementById('bcountry').value;
		param +='&ship_address1='+document.getElementById('sadd1').value;
		param +='&ship_address2='+document.getElementById('sadd2').value;
		param +='&ship_city='+document.getElementById('scity').value;
		param +='&ship_state='+document.getElementById('sstate').value;
		param +='&ship_zip='+document.getElementById('szip').value;
		param +='&ship_country='+document.getElementById('scountry').value;
		var gender='';
		if(document.form4.gender[0].checked){
		gender=document.form4.gender[0].value;	
		}else{
			gender=document.form4.gender[1].value;
		}
		param +='&gender='+gender;
		param +='&country_fax='+document.getElementById('f1').value;
		param +='&area_fax='+document.getElementById('f2').value;
		param +='&phone_fax='+document.getElementById('f3').value;
		param +='&country_mobile='+document.getElementById('m1').value;
		param +='&area_mobile='+document.getElementById('m2').value;
		param +='&phone_mobile='+document.getElementById('m3').value;
		param +='&website='+document.getElementById('website').value;
		param +='&action='+document.getElementById('action').value;
		param+='&country_bali='+document.getElementById('b1').value;
		param+='&area_bali='+document.getElementById('b2').value;
		param+='&number_bali='+document.getElementById('b3').value;
		var pos='';
			if(document.getElementById('c_emp').checked){
				pos+=document.getElementById('c_emp').value+'-';
			}
			if(document.getElementById('c_man').checked){
				pos+=document.getElementById('c_man').value+'-';
			}
			if(document.getElementById('c_buyer').checked){
				pos+=document.getElementById('c_buyer').value+'-';
			}
			if(document.getElementById('c_owner').checked){
				pos+=document.getElementById('c_owner').value+'-';
			}
			if(document.getElementById('c_sale').checked){
				pos+=document.getElementById('c_sale').value+'-';
			}
		
		var class_pty='';
		//alert('test2');
		for (var i=0; i < document.form4.class_pty.length; i++){
			if (document.form4.class_pty[i].checked){
					class_pty=document.form4.class_pty[i].value;
			}
		}
		param +='&class_pty='+class_pty;
		param +='&discount='+document.getElementById('disc').value;
		param +='&pos='+pos;
		//alert ('pos'+pos);
		//alert('test3');
		sendReq.send(param);
		document.getElementById('content_max').innerHTML='<img src="images/lbloading.gif" alt="loading" width="32" height="32">';
		
	}	
}

function ShipToBillPerson(form4) {
	
if (form4.copy.checked) {
	
InitSaveVariables(form4);
//alert(form1.badd1.value);
form4.sadd1.value = form4.badd1.value;
form4.sadd2.value = form4.badd2.value;
form4.scity.value = form4.bcity.value;
form4.scountry.value = form4.bcountry.value;
form4.sstate.value = form4.bstate.value;
form4.szip.value = form4.bzip.value;

//form.ShipFirst.value = form.BillFirst.value;
//form.ShipLast.value = form.BillLast.value;
//form.ShipEmail.value = form.BillEmail.value;
//form.ShipCompany.value = form.BillCompany.value;
//form.ShipTelephone.value = form.BillTelephone.value;




}
else 
{
form4.sadd1.value = ShipAddress1;
form4.sadd2.value = ShipAddress2;
form4.scity.value = ShipCity;
form4.scountry.value = ShipCountry;
form4.sstate.value = ShipState;
form4.szip.value = ShipZip;  

//form.ShipFirst.value = ShipFirst;
//form.ShipLast.value = ShipLast;
//form.ShipEmail.value = ShipEmail;
//form.ShipCompany.value = ShipCompany;
//form.ShipTelephone.value = ShipTelephone;
     



   }
}

function ShipToBillPerson2(form_prof) {
	
if (form_prof.copy.checked) {
	
InitSaveVariables(form_prof);
//alert(form1.badd1.value);
form_prof.sadd1.value = form_prof.badd1.value;
form_prof.sadd2.value = form_prof.badd2.value;
form_prof.scity.value = form_prof.bcity.value;
form_prof.scountry.value = form_prof.bcountry.value;
form_prof.sstate.value = form_prof.bstate.value;
form_prof.szip.value = form_prof.bzip.value;

//form.ShipFirst.value = form.BillFirst.value;
//form.ShipLast.value = form.BillLast.value;
//form.ShipEmail.value = form.BillEmail.value;
//form.ShipCompany.value = form.BillCompany.value;
//form.ShipTelephone.value = form.BillTelephone.value;




}
else 
{
form_prof.sadd1.value = ShipAddress1;
form_prof.sadd2.value = ShipAddress2;
form_prof.scity.value = ShipCity;
form_prof.scountry.value = ShipCountry;
form_prof.sstate.value = ShipState;
form_prof.szip.value = ShipZip;  

//form.ShipFirst.value = ShipFirst;
//form.ShipLast.value = ShipLast;
//form.ShipEmail.value = ShipEmail;
//form.ShipCompany.value = ShipCompany;
//form.ShipTelephone.value = ShipTelephone;
     



   }
}