// JavaScript Document
function setAsDefaultPage(frm)
{
	document.forms[frm].style.behavior='url(#default#homepage)';
	document.forms[frm].setHomePage('http://www.orchestratravelcambodia.com');
}

function openPage(url)
{
	location = url;	
}

function openPageWithConfirm(url,title)
{	
	var ask = "Are you sure that you want to "+title+"?";
	var i = confirm(ask);	
	if(i)
	{
		location = url;	
	}
}

function windowAction(f,url,bln,elseurl)
{
	if(bln)
	{
		document.forms[f].action = url;
	}
	else
	{
		document.forms[f].action = elseurl;
	}
}

// wintitle use when in the popup no have title
function openPopup(url,w,h,moreoption)
{	
	var sw = screen.width;
	var sh = screen.height;
	var left = (sw - w)/2;	
	var top = (sh - h)/2;
	window.open(url,'','width='+w+'px,height='+h+'px,left='+left+',top='+top+','+moreoption);
}

// wintitle use when in the popup no have title
function openPopupTab(url,valTab,w,h,moreoption)
{	
	var sw = screen.width;
	var sh = screen.height;
	var left = (sw - w)/2;
	var top = (sh - h)/2;	
	window.open(url+'&acttab='+valTab,'','width='+w+'px,height='+h+'px,left='+left+',top='+top+','+moreoption);
}

function printDocument(id)
{	
	$(id).hide();
	var obj = window;
	var i = obj.print();
	/*if(i)
	{
		alert("Yes");
	}
	else
	{
		alert("No");
	}*/
}
	
function refreshPage()
{
	window.location.reload();
}

function deleteOrPublished(f,chk_id,pre_id,totalrec,hddelandpub,hdval)
{	
	var intCheck = 0;
	var strTitle = '';
	
	if(hdval==1)
		strTitle = 'Delete';
	else
		strTitle = 'Active';
	
	if($(chk_id).checked == false)
	{
		for(var i=1;i<=$(totalrec).value;i++)
		{
			if($(pre_id+i).checked == true)
			{
				intCheck++;				
			}
		}
		if(intCheck==0)
		{
			alert("No have check box was checked!");
		}
		else
		{
			var i = confirm("Are you sure to "+strTitle+" all?");
			if(i)
			{
				$(hddelandpub).value = hdval;
				document.forms[f].submit();	
			}
		}
	}
	else
	{
		var i = confirm("Are you sure to "+strTitle+" all?");
		if(i)
		{
			$(hddelandpub).value = hdval;
			document.forms[f].submit();
		}
	}		
}

function saveOrder(f,hdsaveorder,hdval)
{
	$(hdsaveorder).value = hdval;
	document.forms[f].submit();
}

function orderUpdate(id)
{
	$(id).value = "update";
}

function checkAll(pre_id,totalrec)
{
	if($('deleteall').checked == true)
	{
		for(var i=1;i<=$(totalrec).value;i++)
		{
			$(pre_id+i).checked = true;
		}				
	}
	else
	{
		for(var i=1;i<=$(totalrec).value;i++)
		{
			$(pre_id+i).checked = false;
		}
	}
}

function checkAllNewStyle(pre_id,totalrec,delallid)
{
	if($(totalrec).value>0&&$(totalrec).value!='')
	{
		if($(delallid).checked == true)
		{
			for(var i=1;i<=$(totalrec).value;i++)
			{
				$(pre_id+i).checked = true;
			}				
		}
		else
		{
			for(var i=1;i<=$(totalrec).value;i++)
			{
				$(pre_id+i).checked = false;
			}
		}
	}
}

function autoLoad(url,pars,id,idVal)
{
	var parlink = "";
	if($(idVal).value != "")
		parlink = 'id='+$(idVal).value;	
		
	pars = pars+parlink;	
	var __MyAjax=new Ajax.Request(
			url, 
			{
				method: 'post', 
				parameters: pars,
				onLoaded: showWaiting(id),
				onComplete: function(__ObjXmlHttpRequest)
				{
					$(id).innerHTML = __ObjXmlHttpRequest.responseText;
				}
			});
}

//==== Show Message Waiting Sending
function showWaiting(id)
{	
	$(id).innerHTML = "Loading ...";
}

function first(which,strValue,f_rec_param,params,vals)
{
	var pars = '';
	if(params!='')
	{
		var arrParam = Array();
		var arrVal = Array();
		arrParam = params.split(',');
		arrVal = vals.split(',');
		for(var i=0;i<arrParam.length;i++)
		{	
			if(i==(arrParam.length-1))
				pars += arrParam[i]+'='+arrVal[i];
			else
				pars += arrParam[i]+'='+arrVal[i]+'&';
		}
	}
	var f_record=parseInt(strValue);
	if(pars!='')
		location = 'index.php?which='+which+'&'+f_rec_param+'='+f_record+'&'+pars;
	else
		location = 'index.php?which='+which+'&'+f_rec_param+'='+f_record;
}

function previous(which,strValue,f_rec_param,params,vals)
{
	var pars = '';
	if(params!='')
	{
		var arrParam = Array();
		var arrVal = Array();
		arrParam = params.split(',');
		arrVal = vals.split(',');
		for(var i=0;i<arrParam.length;i++)
		{		
			if(i==(arrParam.length-1))
				pars += arrParam[i]+'='+arrVal[i];
			else
				pars += arrParam[i]+'='+arrVal[i]+'&';
		}
	}
	var f_record=parseInt(strValue)-1;
	if(pars!='')
		location = 'index.php?which='+which+'&'+f_rec_param+'='+f_record+'&'+pars;
	else
		location = 'index.php?which='+which+'&'+f_rec_param+'='+f_record;
}

function next(which,strValue,f_rec_param,params,vals)
{
	var pars = '';
	if(params!='')
	{		
		var arrParam = Array();
		var arrVal = Array();
		arrParam = params.split(',');
		arrVal = vals.split(',');
		for(var i=0;i<arrParam.length;i++)
		{		
			if(i==(arrParam.length-1))
				pars += arrParam[i]+'='+arrVal[i];
			else
				pars += arrParam[i]+'='+arrVal[i]+'&';
		}
	}
	var f_record=parseInt(strValue)+1;
	if(pars!='')
		location = 'index.php?which='+which+'&'+f_rec_param+'='+f_record+'&'+pars;
	else
		location = 'index.php?which='+which+'&'+f_rec_param+'='+f_record;
}

function last(which,strValue,f_rec_param,params,vals)
{
	var pars = '';
	if(params!='')
	{
		var arrParam = Array();
		var arrVal = Array();
		arrParam = params.split(',');
		arrVal = vals.split(',');
		for(var i=0;i<arrParam.length;i++)
		{		
			if(i==(arrParam.length-1))
				pars += arrParam[i]+'='+arrVal[i];
			else
				pars += arrParam[i]+'='+arrVal[i]+'&';
		}
	}
	var f_record=parseInt(strValue);
	if(pars!='')
		location = 'index.php?which='+which+'&'+f_rec_param+'='+f_record+'&'+pars;
	else
		location = 'index.php?which='+which+'&'+f_rec_param+'='+f_record;
}

function currentPage(which,f_record,f_rec_param,params,vals)
{
	var pars = '';
	if(params!='')
	{
		var arrParam = Array();
		var arrVal = Array();
		arrParam = params.split(',');
		arrVal = vals.split(',');
		for(var i=0;i<arrParam.length;i++)
		{
			if(i==(arrParam.length-1))
				pars += arrParam[i]+'='+arrVal[i];
			else
				pars += arrParam[i]+'='+arrVal[i]+'&';
		}
	}
	if(pars!='')
		location = 'index.php?which='+which+'&'+f_rec_param+'='+f_record+'&'+pars;
	else
		location = 'index.php?which='+which+'&'+f_rec_param+'='+f_record;
}

function makeButtonSubmit(frm,btn,var1,var2,var3,var4)
{
	var errormessage1 = new String();
	var i = confirm('Are you sure to want to save? because after you save your data cannot edit.');
	if(i)
	{
		var product=$(var1);		
		var amount=$(var2);
		var total=$(var3);
		var receive=$(var4);
		if(isEmpty(receive)){errormessage1 += "\n\nPlease, Input data into Cash Receive Fields.";}
		if(isEmpty(amount)){errormessage1 += "\n\nPlease, Select Product Fields.";}
		if(isEmpty(product)){errormessage1 += "\n\nPlease, Input data into Amount Fields.";}		
		
		if(errormessage1.length<2)
		{			
			if(parseInt(receive.value)>=parseInt(total.value))
				document.forms[frm].elements[btn].click();
			else
				alert("Sorry, your Cash Receive is less than Cash Total [Cash Receive < Cash Total] !!!");
		}
		else
		{
			alert(errormessage1);
		}
		
	}
}

function isNumberInput(field, event) 
{
  var key, keyChar;

  if (window.event)
    key = window.event.keyCode;
  else if (event)
    key = event.which;
  else
    return true;
  // Check for special characters like backspace
  if (key == null || key == 0 || key == 8 || key == 13 || key == 27)
    return true;
  // Check to see if it's a number
  keyChar =  String.fromCharCode(key);  
  if (/\d/.test(keyChar)) 
    {
     window.status = "";
     return true;
    } 
  else 
   {
    window.status = "Field accepts numbers only.";
    return false;
   }
}

function isStringInput(field, event) 
{
  var key, keyChar;

  if (window.event)
    key = window.event.keyCode;
  else if (event)
    key = event.which;
  else
    return true;
  // Check for special characters like backspace
  if (key == null || key == 0 || key == 8 || key == 13 || key == 27)
    return true;
  // Check to see if it's a number
  keyChar =  String.fromCharCode(key);  
  if (/[a-z,A-Z]/.test(keyChar)) 
    {
     window.status = "";
     return true;
    } 
  else 
   {
    window.status = "Field accepts numbers only.";
    return false;
   }
}

function disabledFile(fpic,chkpic)
{
	var pic = $(fpic).value;
	if(pic != "")
	{
		$(chkpic).disabled = true;
	}
	else
	{
		$(chkpic).disabled = false;
	}
}

function giveUpdate(id)
{
	$(id).value = "update";
}

function userValidation(f)
{
	var errormessage1 = new String();
	var strError="textbox_error";
	var strNormal="textbox";
	var strError1="cov_combobox_error";
	var strNormal1="cov_combobox";
		
	var signin=$('signin');
		
	var password=$('password');
	
	var repassword=$('repassword');
		
	var userlevel=$('userlevel'); var covuserlevel=$('cov_userlevel');
		
	var fullname=$('fullname');			
	
	var day=$('day'); var covday=$('cov_day');
	
	var month=$('month'); var covmonth=$('cov_month');
	
	var year=$('year'); var covyear=$('cov_year');
	
	var position=$('position');
		
	var email=$('email');		
	
	var phone=$('phone');
	
	var address=$('address');
	
	if(isEmpty(address)){errormessage1 += "\n\nPlease, Fill the Arrival Fields.";address.className=strError;address.focus();}
	else{address.className=strNormal;}
	if(emailValidator(email)){errormessage1 += "\n\nPlease, Fill the Arrival Fields.";email.className=strError;email.focus();}
	else{email.className=strNormal;}
	if(isEmpty(phone)){errormessage1 += "\n\nPlease, Fill the Arrival Fields.";phone.className=strError;phone.focus();}
	else{phone.className=strNormal;}
	if(isEmpty(position)){errormessage1 += "\n\nPlease, Fill the Arrival Fields.";position.className=strError;position.focus();}
	else{position.className=strNormal;}
	if(madeSelection(year)){errormessage1 += "\n\nPlease, Fill the Arrival Fields.";covyear.className=strError1;year.focus();}
	else{covyear.className=strNormal1;}
	if(madeSelection(month)){errormessage1 += "\n\nPlease, Fill the Arrival Fields.";covmonth.className=strError1;month.focus();}
	else{covmonth.className=strNormal1;}
	if(madeSelection(day)){errormessage1 += "\n\nPlease, Fill the Arrival Fields.";covday.className=strError1;day.focus();}
	else{covday.className=strNormal1;}
	if(isEmpty(fullname)){errormessage1 += "\n\nPlease, Fill the Arrival Fields.";fullname.className=strError;fullname.focus();}
	else{fullname.className=strNormal;}
	if(madeSelection(userlevel)){errormessage1 += "\n\nPlease, Fill the Arrival Fields.";covuserlevel.className=strError1;userlevel.focus();}
	else{covuserlevel.className=strNormal1;}
	if(isEmpty(repassword)){errormessage1 += "\n\nPlease, Fill the Arrival Fields.";repassword.className=strError;repassword.focus();}
	else{repassword.className=strNormal;}
	if(isEmpty(password)){errormessage1 += "\n\nPlease, Fill the Arrival Fields.";password.className=strError;password.focus();}
	else{password.className=strNormal;}
	if(isEmpty(signin)){errormessage1 += "\n\nPlease, Fill the Arrival Fields.";signin.className=strError;signin.focus();}
	else{signin.className=strNormal;}
	
	if(errormessage1.length<2)
	{
		if(password.value!=repassword.value)
		{
			alert("Your Password and Retype-password is not Match!!!");
		}
		else
		{				
			document.forms[f].submit();
		}
	}
}

function isEmpty(elem){
	if(elem.value.length > 0){				
		return false;
	}	
	return true;	
}

function isNumeric(elem){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){		
		return true;
	}else{		
		return false;
	}
}

function isAlphanumeric(elem){
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		return false;
	}
}

function madeSelection(elem){		
	if(elem.value != ""){			
		return false;
	}else{		
		return true;
	}
}

function emailValidator(elem){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return false;
	}else{		
		return true;
	}
}

function isNumberInput(field, event) 
{
  var key, keyChar;

  if (window.event)
    key = window.event.keyCode;
  else if (event)
    key = event.which;
  else
    return true;
  // Check for special characters like backspace
  if (key == null || key == 0 || key == 8 || key == 13 || key == 27)
    return true;
  // Check to see if it's a number
  keyChar =  String.fromCharCode(key);  
  if (/\d/.test(keyChar)) 
    {
     window.status = "";
     return true;
    } 
  else 
   {
    window.status = "Field accepts numbers only.";
    return false;
   }
}

function changeImageOver(e,id,fn)
{
	e.className='menu_hover';
	$(id).src = "images/tools/"+fn+"_hover.gif";
}

function changeImageOut(e,id,fn)
{
	e.className='menu';
	$(id).src = "images/tools/"+fn+".gif";
}
