//Javascript
(function ($) {
  $.fn.fadeTransition = function(options) {
    var options = $.extend({pauseTime: 5000, transitionTime: 2000}, options);

    Trans = function(obj) {
      var timer = null;
      var current = 0;
      var els = $("> *", obj).css("display", "none").css("left", "0").css("top", "0").css("position", "absolute");
      $(obj).css("position", "relative");
      $(els[current]).css("display", "block");

      function transition() {
        var next = (current + 1) % els.length | 0;
        $(els[current]).fadeOut(options.transitionTime);
        $(els[next]).fadeIn(options.transitionTime);
        current = next;
        cue();
      };

      function cue() {
        if ($("> *", obj).length < 2) return false;
        if (timer) clearTimeout(timer);
        timer = setTimeout(transition, options.pauseTime);
      };

      cue();
    };

    return this.each(function() {
      var t = new Trans(this);
    });
  }
})(jQuery);


function confirmo(msg)
{
	var messages = msg;
	if(confirm(messages))
		{
			return false;
		}
	else
		{
		 	exit;
		}
		
		return true;
}


function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));


    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl + "/branch/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }

}

function notEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		checkElemVal(elem);
		elem.focus();// set the focus
		return false;
	} 
	return true;
}

function checkElemVal(elem){
	
	var s_len=elem.value.length ;
	if(s_len == 0){
		elem.style.backgroundColor="#ffdeb3";
	}else{
		elem.style.backgroundColor="white";
	}
}

function clear(elem){
	elem.value == "";
}

function emailValidator(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphabet(elem, helperMsg){
	var alphaExp = /^[a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function setValues(){
   	var w = document.conotes.cmb_destination.selectedIndex;
   	var selected_text = document.conotes.cmb_destination.options[w].text;
	convertLoc(selected_text, document.conotes.consignee_kota);
	document.conotes.consignee_kota.disabled = true;
}

function convertLoc(loc, func){
	//this is LOCATION 	
	var curLoc = document.conotes.cmb_destination.value;
	if (loc == "")
	{
		func.value = "";
		func.style.backgroundColor="#ffdeb3";
	}
	else
	{
		func.style.backgroundColor="#cecece";
		func.value = curLoc;
	}
}

function checkNum(x)
	{
	var s_len=x.value.length ;
	var s_charcode = 0;
	for (var s_i=0;s_i<s_len;s_i++)
	{
	s_charcode = x.value.charCodeAt(s_i);
	if(!((s_charcode>=48 && s_charcode<=57)))
	{
	x.value='';
	x.focus();
	return false;
	}
	}
	x.onkeyUp = checkElemVal(x);
	return true;
}

function checkFreight()
{
		if(document.conotes.weight.value == ""){
				alert('Please input weight of the packages first!');
				document.conotes.weight.focus();
		}
		return true;
}

function setEmpty(id){
	document.conotes.getElementById(id).value == "";
}

function convertRp(duit){
	var lengthDuit = duit.length;
}

function calculate(){

//pieces and weight
weight = parseInt(document.conotes.weight.value, 10);
pieces = parseInt(document.conotes.pieces.value, 10);

	indexDiscount = parseInt(document.conotes.cmb_discount.selectedIndex, 10);
	discount = parseInt(document.conotes.cmb_discount.options[indexDiscount].value, 10);
	indexPpn = parseInt(document.conotes.cmb_ppn.selectedIndex, 10);
	ppn = parseInt(document.conotes.cmb_ppn.options[indexPpn].value, 10);
	charge = parseInt(document.conotes.charge.value, 10);
	freight = parseInt(document.conotes.freight.value, 10);
alert(weight);
tempdiscount = freight*discount/100;
total = freight-tempdiscount;
if(isNaN(total)){
	total = "";
};
sampleFreight = (weight * 12000) * pieces;
if(isNaN(sampleFreight)){
	sampleFreight = "";
};
document.conotes.freight.value = sampleFreight + ' (Sample)';
document.conotes.total_payment.value = total;
return total;

}

function checkConForm(form){
	
	var acc_number = document.getElementById('acc_number');
	var weight = document.getElementById('weight');
	var piece = document.getElementById('piece');
	var s_fullname = document.getElementById('shipper_fullname');
	var s_lastname = document.getElementById('shipper_lastname');
	var s_address = document.getElementById('shipper_address');
	var s_kota = document.getElementById('shipper_kota');
	var s_kode_pos = document.getElementById('shipper_kode_pos');
	var s_phone_no = document.getElementById('shipper_phone_no');
	var s_mobile = document.getElementById('shipper_mobile_no');
	var c_fullname = document.getElementById('consignee_fullname');
	var c_address = document.getElementById('consignee_address');
	var c_kota = document.getElementById('consignee_kota');
	var c_kode_pos = document.getElementById('consignee_kode_pos');
	var c_phone_no = document.getElementById('consignee_phone_no');
	var c_mobile = document.getElementById('consignee_mobile_no');
	var pcs = document.getElementById('pcs');
	var weight = document.getElementById('weight');
	var b_amount = document.getElementById('bar_amount');
	if(notEmpty(acc_number, "Account number harus diisi.")) {
		if(notEmpty(s_fullname, "Nama depan pengirim harus diisi.")) {
		if(notEmpty(s_lastname, "Nama belakang pengirim harus diisi.")) {
		if(notEmpty(s_address, "Alamat pengirim harus diisi.")) {
			if(notEmpty(s_kota, "Kota pengirim harus diisi.")) {
				if(notEmpty(s_kode_pos, "Kode Pos pengirim harus diisi.")) {
					if(notEmpty(s_phone_no, "Telepon pengirim harus diisi.")) {
						if(notEmpty(s_mobile, "No HP pengirim harus diisi.")) {
							if(notEmpty(c_fullname, "Nama penerima harus diisi.")) {
								if(notEmpty(c_address, "Alamat penerima harus diisi.")) {
									if(notEmpty(c_kota, "Kota penerima harus diisi. Pilih salah satu kode tujuan pada combobox Tujuan/Destination diatas.")) {
										if(notEmpty(c_kode_pos, "Kode pos penerima harus diisi.")) {
											if(notEmpty(c_phone_no, "Telepon penerima harus diisi.")) {
												if(notEmpty(c_mobile, "No HP penerima harus diisi.")) {
													if(notEmpty(pcs, "Jumlah barang yang dikirim harus diisi.")) {
														if(notEmpty(weight, "Berat barang yang akan dikirim harus diisi.")) {
											document.conotes.submit();
											return true;
															}
														}
													}
												}
											}
										}
								}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	return false;
}
		var drz;
        var result;
        function GetXmlHttpObject(){
        if (window.XMLHttpRequest){
        return new XMLHttpRequest();
        }
        if (window.ActiveXObject){
        return new ActiveXObject("Microsoft.XMLHTTP");
        }
        return null;
        }
        
        function cari(str)
        {
        if(str == ""){
        	exit();
        }
        drz = GetXmlHttpObject();
        if (drz == null){
        alert ("HTTP Request Error");
        return;
        }
        var url= ""+getBaseURL()+"public/online/js/userdata.php";
        url=url+"?id="+str;
        drz.onreadystatechange=stateChanged;
        drz.open("GET",url,true);
        drz.send(null);
        }
        
        function stateChanged(){
        if (drz.readyState==4){
        result = drz.responseText;
        if(result){
        result = drz.responseText.split("|");
        document.getElementById("shipper_fullname").value = result[0];
        document.getElementById("shipper_lastname").value = result[0];
        document.getElementById("shipper_address").value = result[1];
        document.getElementById("shipper_kode_pos").value = result[2];
        document.getElementById("shipper_phone_no").value = result[3];
        document.getElementById("shipper_mobile_no").value = result[4];
        document.getElementById("shipper_kota").value = result[5];
		
        }else{
        alert('Invalid Customer ID!');
		//document.getElementById("acc_number").value = '';
		//document.getElementById("acc_number").focus();
        }
        }
        }
		
		function cthis(str)
		{ $.post("rpc.php", {queryString: ""+inputString+""}, 
				 function(data){ 
				 	if(data.length >0) { 
						$("#autoSuggestionsList").html(data); 
					} 
				});
		}
		
		function barcodeList(str)
        {
        if(str == ""){
        	document.getElementById("result_barcode").innerHTML = "";
        }
        adrz = GetXmlHttpObject();
        if (adrz == null){
        alert ("HTTP Request Error");
        return;
        }
		var url=""+getBaseURL()+"public/online/js/userdata.php";
        url=url+"?b_amount="+str;
		adrz.onreadystatechange=listCode;
        adrz.open("get",url,true);
        adrz.send(null);
        }
		
		function listCode()
		{
			if (adrz.readyState==4){
        		result = adrz.responseText;
        		if(result){
					document.getElementById("result_barcode").innerHTML = result;
					if(result == "")
					{
						document.getElementById("msg_b_code").innerHTML = '';
					}
					else
					{
						document.getElementById("msg_b_code").innerHTML = "Insert your barcode here :";
						document.getElementById("print_code").innerHTML = "<a href='javascript:window.print();'>Print Code(s)</a>";
					}

				}
			}
		}
	  
	  	function generate_bcode(str,divId)
        {
        if(str == ""){
			exit();
        }
        bardrz = GetXmlHttpObject();
        if (bardrz == null){
        alert ("HTTP Request Error");
        return;
        }
		var divId = divId;
        var url=""+getBaseURL()+"public/online/js/userdata.php";
        url=url+"?barcode="+str+"&a="+divId;
        bardrz.onreadystatechange=showBarcode;
        bardrz.open("GET",url,true);
        bardrz.send(null);
        }
		
		function showBarcode()
		{
			if (bardrz.readyState==4){
        		result = bardrz.responseText;
        		if(result){
					result = bardrz.responseText.split("|");
					document.getElementById('b_code'+result[1]).innerHTML = "<img src='"+getBaseURL()+"public/online/js/barcode-gen.php?barcode="+result[0]+"&quality=100&width=260&height=60' width='220' height='80' />";
				}
			}
		}
		
		function fillCustList(elem)
		{
			var list = elem.value.split("#");
			document.getElementById("acc_number").value = list[0];
			document.getElementById("shipper_fullname").value = list[1];
			document.getElementById("shipper_lastname").value = list[7];
			document.getElementById("shipper_address").value = list[2];
			document.getElementById("shipper_address2").value = list[8];
			document.getElementById("shipper_corp_name").value = list[9];
			document.getElementById("shipper_building_name").value = list[10];
            document.getElementById("shipper_area_phone_no").value = list[11];
            document.getElementById("shipper_area_mobile_number").value = list[12];
			document.getElementById("shipper_kode_pos").value = list[3];
			document.getElementById("shipper_phone_no").value = list[4];
			document.getElementById("shipper_mobile_no").value = list[5];
			document.getElementById("shipper_kota").value = list[6];
            
		}
		
		function clearCustData()
		{
			document.getElementById("acc_number").value = "";
			document.getElementById("shipper_fullname").value = "";
			document.getElementById("shipper_lastname").value = "";
			document.getElementById("shipper_address").value = "";
			document.getElementById("shipper_address2").value = "";
			document.getElementById("shipper_corp_name").value = "";
			document.getElementById("shipper_building_name").value = "";
			document.getElementById("shipper_kode_pos").value = "";
			document.getElementById("shipper_phone_no").value = "";
			document.getElementById("shipper_mobile_no").value = "";
			document.getElementById("shipper_kota").value = "";
		}

		//MANIFEST
		
		function changeBg(elem, pointer)
		{
			elem.style.backgroundColor = "#FEE4C2";
			if(!pointer){
			elem.style.cursor = "pointer";
			}
		}
		
		function changeBgs(elem, setColor, pointer)
		{
			var colors = setColor;
			elem.style.backgroundColor = "#"+colors+"";
			if(!pointer){
			elem.style.cursor = "pointer";
			}
		}
		
		function resetBgs(elem,setColor)
		{
			elem.style.backgroundColor = "#"+setColor+"";
		}
		
		function resetBg(elem)
		{
			elem.style.backgroundColor = "";
		}
		
		function storeManifest()
		{
			document.getElementById('manifest_detail').innerHTML = "Test Manifest";
		}
		
		function fullviewmanifest()
		{
			$('#manifest_content').hide('slow');
			var content = document.getElementById('manifest_content');
			var detail = document.getElementById('container_manifest');
			var linker = document.getElementById('str');
			linker.setAttribute("OnClick","expandManifest();");
			linker.innerHTML = "Expand View";
			detail.style.height = "580px";
			
		}
		
		function expandManifest()
		{
			$('#manifest_content').show('fast');
			var content = document.getElementById('manifest_content');
			var detail = document.getElementById('container_manifest');
			detail.style.height = "370px";
			var linker = document.getElementById('str');
			linker.setAttribute("OnClick","fullviewmanifest();");
			linker.innerHTML = "Full View";
		}
		
		function userInput(elem)
		{
			var s = document.frmHomeLogin.elements;
			if(elem.value == "username" || elem.value == "password")
			{
				elem.value = '';
			}
			if(elem.value == ''){
						elem.setAttribute("onblur","setValuesw(this);");
						elem.style.backgroundColor="#D9FFFF";
						elem.style.color="#000000";
						elem.defaultValue = elem.value;
			}
			
			return true;			
		}
		
		function passInput(elem)
		{
			if(elem.value == "password")
			{
				elem.value = '';
			}
			if(elem.value == '')
			{
					elem.setAttribute("onblur","setValuesPass(this);");
					elem.setAttribute("type","password");
					elem.style.backgroundColor="#D9FFFF";
					elem.style.color="#000000";
					elem.defaultValue = elem.value;
			}
			
			return true;
		}
		
		function setInput()
		{
			var username = document.getElementById('home_username');
			var password = document.getElementById('home_password');
			username.value = "username";
			//username.style.fontStyle="italic";
			username.setAttribute("type","text");
			username.setAttribute("onClick","userInput(this)");
			username.setAttribute("onFocus","userInput(this)");
			password.value = "password";
			password.setAttribute("onClick","passInput(this)");
			password.setAttribute("onFocus","passInput(this)");
		}
		
		function setValuesPass(elem)
		{
			if(elem.value == '')
			{
				elem.style.backgroundColor="#F4F4F4";
				elem.style.color="#CCCCCC";
				elem.setAttribute("type","text");
				elem.value = "password";
			}
			return false;
		}
		
		function setValuesw(elem)
		{
			if(elem.value == '')
			{
						elem.value = "username";
						elem.style.backgroundColor="#F4F4F4";
						elem.style.color="#CCCCCC";
			}
			else
			{
				return false;
			}
			return true;
		}
		
		/*
		function arrivalCounter(field) {
		var countfield;
		var maxlimit = 12;
		if (field.value.length > maxlimit) // if too long...trim it!
				field.innerHTML = +\n+;
		
		//field.value = field.value.substring(0, maxlimit);
		// otherwise, update 'characters left' counter
		else 
		countfield = maxlimit - field.value.length;
		}
		*/
		
		function CheckAll(fmobj)
		{
			for (var i=0;i<fmobj.elements.length;i++)
			{
				var e = fmobj.elements[i];
				if ((e.name != 'allbox') && (e.type=='checkbox') && (!e.disabled))
				{
					e.checked = fmobj.allbox.checked;
				}
			}
		}

		function CheckCheckAll(fmobj)
		{	
			var TotalBoxes = 0;
			var TotalOn = 0;
			for (var i=0;i<fmobj.elements.length;i++)
			{
				var e = fmobj.elements[i];
				if ((e.name != 'allbox') && (e.type=='checkbox'))
				{
					TotalBoxes++;
					if (e.checked)
					{
						TotalOn++;
					}
				}
			}
			
			if (TotalBoxes==TotalOn)
			{
				fmobj.allbox.checked=true;
			}
			else
			{
				fmobj.allbox.checked=false;
			}
		}
		
		function destNull()
		{
			var cc = document.getElementById("counter");
			alert(cc.value);
		}
		
		// MAIN ASSETS
		function checkDest()
		{
			var dest = document.getElementById("dest");
			if(dest.value == ""){
				dest.style="border:1px solid red";
				alert("Please choose the destination.");
				dest.focus();
				exit();
			}
			else{
			
			//document.frmWhmanifest.submit();
			alert('OK');
			
			}
			
		}
		
		function required(field,alerttxt)
		{
		with (field)
		  {
		  if (value==null||value=="")
			{
			alert(alerttxt);return false;
			}
		  else
			{
			return true;
			}
		  }
		}
		
		function cekCombos(thisform, elem)
		{
			with (thisform)
			  {
			  if (required(elem,"Destination empty")==false)
			  {elem.focus();return false;}
			  }
		}
		
		function hover(elem)
		{
			elem.style.border="1px solid red";
		}
		
		function unhover(elem)
		{
			elem.style.border="";
		}
	
	function checkDest(fobj)
	{
			var TotalOn = 0;
			for (var i=0;i<fobj.elements.length;i++)
			{
				var e = fobj.elements[i];
				var ed = document.getElementById("dest");
				if(e.value == '')
				{
					alert('Please choose the '+e.title+' destination.');
					return false;
				}
			}
		return true;
	}
	
	function checkCourier(fobj)
	{
		for (var i=0;i<fobj.elements.length;i++)
			{
				var e = fobj.elements[i];
				if(e.value == '')
				{
				    if(e.title == 'reciever name'){
					alert('The '+e.title+' field is required.');
					e.focus();
					return false;
                    }
                    else if(e.title == 'date'){
					alert('The '+e.title+' field is required.');
					e.focus();
					return false;
                    }
                    else
                    {
                        return true;
                    }
				}
                
			}
		return true;
	}
    
    function checkfCourier(fobj)
	{
		for (var i=0;i<fobj.elements.length;i++)
			{
				var e = fobj.elements[i];
				if(e.value == '')
				{
				    if(e.title == 'date'){
					alert('The '+e.title+' field is required.');
					e.focus();
					return false;
                    }
                    else if(e.title == 'reason'){
					alert('The '+e.title+' field is required.');
					e.focus();
					return false;
                    }
                    else
                    {
                        return true;
                    }
				}
                
			}
		return true;
	}
	
		function formatCurrency(num) {
		num = num.toString().replace(/\$|\,/g,'');
		if(isNaN(num))
		num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
		if(cents<10)
		cents = "0" + cents;
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+'.'+
		num.substring(num.length-(4*i+3));
		return (((sign)?'':'-') + '' + num + ''); // voilaa!! dapet duitnya
		}
        
    function checkWeight(elem,x)
    {
        if(elem.value == '')
        {
        elem.value = x;
        }
        
    }
    
    function forceMin(elem,x)
    {
        var yx = elem.value;
        var errormsg = document.getElementById("min_weight");
        if(yx < x)
        {
            elem.value = x;
            errormsg.innerHTML = "The minimum value for pickup is : "+x+" Kg.";
        }
        else
        {
            errormsg.innerHTML = "";
        }
        
        return false;
    }
    
    function refreshParent() {
        window.opener.location.href = window.opener.location.href;
                
        if (window.opener.progressWindow)
  		{
            window.opener.progressWindow.close()
        }
        
        window.close();
    
    }
    function clearSD(elem)
        {
            if(elem.value == "username...")
            {
                elem.value = "";
            }
        }
        function getSD(elem)
        {
            var ds = elem.value;
            if(ds == "")
            {
                elem.value="username..."
            }
        }
        function clearPS(elem)
        {
            if(elem.value == "password...")
            {
                elem.value = "";
                elem.setAttribute("type","password");
            }
        }
        function getPS(elem)
        {
            var df = elem.value;
            if(df == "")
            {
                elem.setAttribute("type","text");
                elem.value="password..."
            }
        }
        function getRed(elem)
        {
            elem.style.background = "#fffecd";
        }
        function clearBd(elem)
        {
            elem.style.background = "#fff";
        }
