var isNN=(navigator.appName.indexOf("Netscape")!=-1);
var isIE=(navigator.appName.indexOf("Internet Explorer")!=-1);

Number.prototype.NaN0=function(){return isNaN(this)?0:this;}
Number.prototype.NaNBlank=function(){return isNaN(this)?'':this;}


function mouseOnMenu(td){
	mouseOnRow(td);
}

function mouseOffMenu(td){
	mouseOffRow(td);
}

function mouseOnRow(td){
	td.oldclass=td.className;
	td.className=td.className+'_hotrow';
}

function mouseOffRow(td){
	td.className=td.oldclass;
}

function gotoPage(url){
	top.location.href=url;
}

function GetKeySet(DataString, Key, Splitters){
	var arrRec=DataString.split(Splitters[0]);
	for(var i=0;i<arrRec.length;i++){
		var arrTmp=arrRec[i].split(Splitters[1]);
		if(arrTmp[0]==Key) return arrTmp;
	}
	return null;
}

function formatNumber(number, decPoints){
	var tmpRT=number.toString(10);
	if(tmpRT.indexOf('.')<0)
	tmpRT+='.';
	var tmp=tmpRT.substr(tmpRT.indexOf('.')+1,tmpRT.length);
	if(tmp.length>2)
	tmpRT=tmpRT.substr(0,tmpRT.indexOf('.')+3);
	for(var j=tmp.length;j<2;j++)tmpRT+='0';
	return tmpRT;
}

function validateField(field,message,regexp){
	var bEmpty=false;
	var strAction='enter';
	var field_value='';
	var xField=(field.length>0 && !field.type)?field[0]:field;
	var selectedIndex=-1;
	
	switch(xField.type){
		case 'textarea':
		case 'hidden':
		case 'password':
		case 'file':
		case 'text':{
			field_value=field.value;
			bEmpty=(field_value.length<1);
			break;
		}
		case 'select-one':{
			if(field.selectedIndex>=0) field_value=field.options[field.selectedIndex].value;
			bEmpty=(field.selectedIndex<0 || field_value.length<1);
			strAction='select';
			break;
		}
		case 'checkbox':{
			strAction='select';
			bEmpty=!xField.checked;
			break;
		}
		case 'radio':{
			strAction='select';
			bEmpty=true;
			if(xField!=field){
				for(var intIdx=0;intIdx<field.length;intIdx++){
					if(field[intIdx].checked){
						bEmpty=false;
						selectedIndex=intIdx;
						break;
					}
				}
			}
			else
				bEmpty=!xField.checked;
				selectedIndex=bEmpty?-1:0;
			break;
		}
		default:{
			bEmpty=true;
			alert('form element \''+xField.name+'\' ['+xField.type+'] is not supported by checkEmpty() function');
			break;
		}
	}
	
	if(regexp && !bEmpty){
		bEmpty=!regexp.test(field_value);
	}
	
	if(bEmpty){
		if(message) alert(message);
		else alert('Please '+strAction+' a value for field \''+xField.name+'\'');
		if(xField.type!='hidden') xField.focus();
	}
	
	return (xField.type=='radio')?selectedIndex:!bEmpty;
}

//Remove spaces from start and end of a string
function trim(value) {
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
   var obj = / +/g;
   temp = temp.replace(obj, " ");
   if (temp == " ") { temp = ""; }
   return temp;
}

function ValidateNumeric(control){
	control.value=trim(control.value);
	if(!IsNumeric(control.value)){
		alert('Please enter numeric values only');
		control.focus();}}

function removeChar(Target){
	var re=/[^0-9]/g;
	Target=Target.replace(re,'');
	return Target;}

function removeNonNum(Target){
	var re=/[^0-9|\^.|\^-]/g;
	Target=Target.replace(re,'');
	return Target;}

function removeDigit(Target){
	var re=/[0-9]/g;
	Target=Target.replace(re,'');
	return Target;}

function IsEmail(strEmail)
{	
	var status = false;
	var strEmailAddress = new String(strEmail);	
	var strEmailPattern = (/^([a-z0-9_\.\-])+\@(([a-z0-9_\.\-])+\.)+([a-z0-9]{2,4})+$/i);

	if (strEmailAddress.search(strEmailPattern)==0)
	{
		status = true;
	}
	return status;
}

function IsCreditCard(strCC){
	strCC=removeChar(strCC);
	if(strCC.length<16) return false;
	if(strCC.length > 19) return false;
	
    sum = 0; mul = 1; l = strCC.length;
	for (i = 0; i < l; i++){
		digit = strCC.substring(l-i-1,l-i);
		tproduct = parseInt(digit ,10)*mul;
		if (tproduct >= 10) sum += (tproduct % 10) + 1;
		else sum += tproduct;
		if (mul == 1) mul++;
		else mul--;
	}
	if ((sum % 10) != 0) return false;

 	return true;
}

var fileTypes=["csv"];

function checkFileType(what){
  var source=what.value;
  var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
  for (var i=0; i<fileTypes.length; i++) if (fileTypes[i]==ext) break;
  globalPic=new Image();
  if (i<fileTypes.length) globalPic.src=source;
  else {
    //globalPic.src=defaultPic;
    alert("That is not a valid CSV\nPlease upload another one:\n\n");
  }
}

function syncValues(elm, prefix1, prefix2){
	var frm=elm.form;
	var bSame=false;
	var strId=(elm.id.length<1)?elm.name:elm.id;
	var value=null;
	var re=null;
	
	if(frm.syncFlag){
		if(frm.syncFlag.type=='hidden') bSame=(frm.syncFlag.value.length>0);
		else bSame=frm.syncFlag.checked;
	}
	
	if(!bSame) return;
	
	re=new RegExp('^'+prefix1, '');
	if(strId.match(re)){
		strId=strId.split(re);
		strId[0]=prefix2;
	}
	else{
		re=new RegExp('^'+prefix2, '');
		if(strId.match(re)){
			strId=strId.split(re);
			strId[0]=prefix1;
		}
		else return;
	}
	
	var newelm=document.getElementById(strId.join(''));
	if(!newelm) return;
	switch(newelm.type){
		case 'textarea':
		case 'hidden':
		case 'password':
		case 'file':
		case 'text':{
			newelm.value=elm.value;
			break;
		}
		case 'select-one':{
			newelm.selectedIndex=elm.selectedIndex;
			break;
		}
		case 'radio':
		case 'checkbox':{
			newelm.checked=elm.checked;
			break;
		}
		default: return; break;
	}
}

function syncNow(elm){
	var frm=elm.form;
	
	if(!elm.checked) return;
	
	syncValues(frm.bilStret, 'bil', 'ships');
	syncValues(frm.bilcity, 'bil', 'sip');
	syncValues(frm.bilState, 'bilS', 'ships');
	syncValues(frm.bilZip, 'bilZ', 'shipz');
}

function showInventory(id){
	var popwidth=390;
//	var popwidth=960;
	var popleft=(screen.width-popwidth)/2;
	var popheight=170;
//	var popheight=930;
	var popright=(screen.height-popheight)/2;
	var strUrl='inventory.pop.php?itemid='+id;
	
	var win=window.open(strUrl, 'pop_inventory', 'status=1, height='+popheight+', width='+popwidth+' resizable=0,scrollbars=yes, left='+popleft+', top='+popright);
	win.focus();
}

function swapClass(elm, newClass){
	if(newClass && newClass.length>0){
		elm.setAttribute('__backupclass', elm.className);
		elm.className=newClass;
	}
	else elm.className=elm.getAttribute('__backupclass');
}

var __inputGuideText='';
function inputGuide(elm, clear){
	if(clear && elm.value==__inputGuideText) elm.value='';
	if(!clear && elm.value.length<1) elm.value=__inputGuideText;
}


function viewInvoice(id){
	var width=768;
	var height=672;
	var top=(screen.height-height)/2;
	var left=(screen.width-width)/2;
	var strParams='menubar=1, toolbar=0, status=1,, scrollbars=1 ,width='+width+',height='+height+',top='+top+',left='+left;
	var win=window.open('/admin/view.invoice.php?id='+id, 'winInvoice', strParams);
	win.focus();
}

function viewUnpaid(id){
	var width=768;
	var height=672;
	var top=(screen.height-height)/2;
	var left=(screen.width-width)/2;
	var strParams='menubar=1, toolbar=0, status=1,, scrollbars=1 ,width='+width+',height='+height+',top='+top+',left='+left;
	var win=window.open('/admin/view.unpaid.php?id='+id, 'winUnpaid', strParams);
	win.focus();
}

function shortPhara(val, len){
	if(isNaN(len) || len<1) return val;
	
	val=trim(val);
	if(val.length>len){
		val=trim(val.substr(0, len))+'...';
	}
	
	return val;
}

function viewItemReference(itemId){
	var width=800;
	var height=600;
	var top=(screen.height-height)/2;
	var left=(screen.width-width)/2;
	var strParams='menubar=0, toolbar=0, status=0, scrollbars=1 ,width='+width+',height='+height+',top='+top+',left='+left;
	var win=window.open('/admin/item.reference.php?itemid='+itemId, 'winItemRef', strParams);
	win.focus();
}

function showOnOrder(poCode){
	var width=600;
	var height=300;
	var top=(screen.height-height)/2;
	var left=(screen.width-width)/2;
	var strParams='menubar=0, toolbar=0, status=0, scrollbars=1 ,width='+width+',height='+height+',top='+top+',left='+left;
	var win=window.open('/admin/item.on.order.php?pocode='+poCode, 'winItemOnOrder', strParams);
	win.focus();
}

function popComment(itemId, edit){
	var width=475;
	var height=300;
	var top=(screen.height-height)/2;
	var left=(screen.width-width)/2;
	var strParams='menubar=0, toolbar=0, status=0, scrollbars=1 ,width='+width+',height='+height+',top='+top+',left='+left;
	var win=window.open('/admin/item.comment.php?itemid='+itemId+'&edit='+(edit?1:0), 'winItemComment', strParams);
	win.focus();
}

function popInventoryDetails(id){
	var popwidth=800;
	var popleft=(screen.width-popwidth)/2;
	var popheight=600;
	var popright=(screen.height-popheight)/2;
	var strUrl='rpt.inventory.details.php?itemid='+id;
	
	var win=window.open(strUrl, 'pop_ivd', 'status=1, height='+popheight+', width='+popwidth+' resizable=0,scrollbars=yes, left='+popleft+', top='+popright);
	win.focus();
}

function locationMove(itemId, locId){
	var width=325;
	var height=180;
	var top=(screen.height-height)/2;
	var left=(screen.width-width)/2;
	var strParams='menubar=0, toolbar=0, status=0, scrollbars=1 ,width='+width+',height='+height+',top='+top+',left='+left;
	var win=window.open('/admin/move.location.php?itemid='+itemId+'&currentLocation='+locId, 'winMoveLocation', strParams);
	win.focus();
}
