/*
* This function search array with binary serahc
*/
function  binarySearch( a, x )  {
        var low = 0;
        var high = a.length - 1;
        var mid;

        while( low <= high ){
            mid = Math.round(( low + high ) / 2);

            if( a[ mid ]< x )
                low = mid + 1;
            else if( a[ mid ]> x  )
                high = mid - 1;
            else
                return mid;
        }

        return -1;     // NOT_FOUND = -1
}

/*
* This function search array with binary serahc
*/
function  binarySearchStartWith( a, x )  {
        var low = 0;
        var high = a.length - 1;
        var mid;

        while( low <= high ){
            mid = Math.round(( low + high ) / 2);
			//alert(a[ mid ]+" "+(a[ mid ]< x)+" "+x+" "+ mid );
            if( a[ mid ]< x )
                low = mid + 1;
            else if( a[ mid ]> x  )
                high = mid - 1;
            else{
				if(Math.round(mid)==mid){
	                return mid;
				}else{
					break;
				}
			}
        }
		//alert(mid+" "+x);
		if(a[mid-1]!=null && a[mid-1].indexOf(x)==0){
			return mid-1;
		}
		if(a[mid]!=null && a[mid].indexOf(x)==0){
			return mid;
		}
		if(a[mid+1]!=null && a[mid+1].indexOf(x)==0){
			return mid+1;
		}

        return -1;     // NOT_FOUND = -1
}


/*
 * This method will check the correctness of a upc
 */

function  isValidUPC(upc){
        var result=false;
        if(upc!=null){
            //upc=upc.trim();
                if(upc.length==12){
                    var upcNum=Number(upc);
                    var oddsDigitSum=sumForMeTheDigitsWithJump(upcNum,1);
                    //if(upc!=null && upc.equals("012562721040"))System.out.println("oddsDigitSum"+oddsDigitSum);
                    var evenDigitSum=sumForMeTheDigitsWithJump(upcNum,2);
                    //if(upc!=null && upc.equals("012562721040"))System.out.println("evenDigitSum"+evenDigitSum);
                    var upcSum=(oddsDigitSum*3) + evenDigitSum;
                    //if(upc!=null && upc.equals("012562721040"))System.out.println("upcSum"+upcSum);
                    var roundDecimalUp=Math.round(parseFloat(upcSum)/10+0.5)*10;
                    //if(upc!=null && upc.equals("012562721040"))System.out.println("roundDecimalUp"+roundDecimalUp);
					//alert(oddsDigitSum+" "+evenDigitSum +" "+ upcSum);
                    if((roundDecimalUp-upcSum)%10==upcNum%10){
                        result=true;
                    }
                }
        }
        return result;
    }

    //this function will sum the digits in the even places starting with the rigthmost digit.
    function sumForMeTheDigitsWithJump(num,from){
        var result=0;
		var numArr=num.toString().split("").reverse();
		for(var i=from;i<numArr.length;i=i+2){
				result=result+parseInt(numArr[i]);
		}
        return result;
    }



function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function 


function replaceAll( str1, toStr2,source ) {
	 var s = source + "";
	 while (s.indexOf(str1) >= 0) {
	  s = s.replace(str1, "§");
	 };
	 while (s.indexOf("§") >= 0) {
	  s = s.replace("§", toStr2);
	 };
	 return s;
}


function emailInputChecker(obj) {
obj.value=trim(obj.value);
var mail=trim(obj.value),i=0,c='',q=false;if(mail.length<6) return badmailInputChecker(obj);if(mail.charAt(0)=='\"') {q=true;i++;} if(mail.charAt(0)=='.') return badmailInputChecker(obj);
  while(c!='@' && i<mail.length) {c=mail.charAt(i); if(q) {if(c=='\"') {c = mail.charAt(i+1); if(c!='@') return badmailInputChecker(obj); i++;} else {i++; if(c=='\\') i++; c='';}} else {if(c!='@') {if(c=='\\') i+=2; else {i++; if(!is_cInputChecker(c)) return badmailInputChecker(obj);}}}}
  if(i==mail.length || mail.charAt(i-1)=='.') {return badmailInputChecker(obj)} var dom=mail.substring(i+1); if(dom.indexOf('.') == -1) return badmailInputChecker(obj); if(!is_domainInputChecker(dom)) return badmailInputChecker(obj); return true;}
function is_aInputChecker(c) {return (  (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') );}
function is_cInputChecker(c) {return !(is_sInputChecker(c) || c == ' ');}
function is_dInputChecker(c) {return (c >= '0' && c <= '9');}
function is_sInputChecker(c) {var list = '<>()[]\\,;:@"'; return (list.indexOf(c) != -1 || c < ' ' || c > '~');}
function is_domainInputChecker(s) {if(s.charAt(0)=='[' && s.charAt(s.length-1)==']') {return(is_dotnumInputChecker(s.substring(1,s.length-1)));} var i = s.indexOf('.'); if(i == -1) return is_simple_nameInputChecker(s); return (is_domainInputChecker(s.substring(i+1)) && is_simple_nameInputChecker(s.substring(0,i)));}
function is_simple_nameInputChecker(s) {var l=s.length; if(l==1) {return(is_aInputChecker(s.charAt(0)));} else {if(l==2) {return(is_aInputChecker(s.charAt(0)) && is_let_digInputChecker(s.charAt(1)));} else {return(is_aInputChecker(s.charAt(0)) && is_ldh_strInputChecker(s.substring(1,l-1)) && is_let_digInputChecker(s.charAt(l-1)));}}}
function is_ldh_strInputChecker(s) {var l=s.length; if(l==1) {return is_let_dig_hypInputChecker(s.charAt(0));} else {return (is_let_dig_hypInputChecker(s.charAt(0)) && is_ldh_strInputChecker(s.substring(1,l)));}}
function is_let_digInputChecker(c) {return(is_aInputChecker(c) || is_dInputChecker(c));}
function is_let_dig_hypInputChecker(c) {return(is_let_digInputChecker(c) || c=='-');}
function is_dotnumInputChecker(s) {var m=0,n=0; for(var i=0; i<3; i++) {n=s.indexOf('.',m); if(n==-1) return false; if(!is_snumInputChecker(s.substring(m,n))) return false; m=n+1;} return is_snumInputChecker(s.substring(m,s.length));}
function is_snumInputChecker(s) {var n=parseInt(s); return !(isNaN(n) || n<0 || n>255);}
function badmailInputChecker(obj) {alert('Please enter a valid Email.');obj.focus();return false;}


function stringLengthInputChecker(obj,msg,leng) {
	if(trim(obj.value).length<leng){
		alert(msg);
		obj.focus();
		return false;
	}
	return true;
}

function floatLayer1(layer1,obj1){
	var whereX=findPosX1(obj1);
	var whereY=findPosY1(obj1)+obj1.offsetHeight;

    if(layer1==null)return;
	//alert(whereX+" "+whereY+" "+obj1.offsetHeight);
    var L=layer1;	
    L.style.left=(whereX)+'px';//-2 for cellpedding
    L.style.top=(whereY)+'px';//-2 for cellpedding
}

function findPosX1(obj){
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY1(obj){
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

var lastLayerMoved=null;
var lastLayerMovedObj=null;
var lastLayerMovedName=null;
var attrbMenuHeight=400;
var maxAttrbRows=20;
var wasEnterNowAttrbMenu=0;
var maskAllScreen=null;
var maskAllScreen2=null;
var maskAttButt=null;//for opera
var secondTimeOpen=0;
function attrbMenu(layer1,obj1,number1){
	if(maskAllScreen==null){
		maskAllScreen=document.getElementById("maskAllScreen");
	}
	if(maskAllScreen2==null){
		maskAllScreen2=document.getElementById("maskAllScreen2");
	}
	if(maskAttButt==null){
		maskAttButt=document.getElementById("maskAttButt");
	}

	var nowCount=fetchAndInc.f();
	lastLayerMovedName=layer1;
	//alert(lastLayerMovedObj.className);
	if(lastLayerMovedObj!=null  && lastLayerMovedObj!=obj1 && lastLayerMovedObj.className=="ov1"){
		lastLayerMovedObj.className="men";
	}

	lastLayerMovedObj=obj1;
	var inscroll=document.getElementById(layer1+"inscroll");
	layer1= document.getElementById(layer1);
	//alert(layer1);
	if(layer1!=null && obj1!=null){
		/*
		maskAttButt.onclick=obj1.onclick;
		maskAttButt.onmouseover=obj1.onmouseover;
		maskAttButt.style.width=(obj1.offsetWidth)+"px";
		maskAttButt.style.height=(obj1.offsetHeight+50)+"px"
		maskAttButt.style.top=(findPosY1(obj1))+"px";
		maskAttButt.style.left=(findPosX1(obj1))+"px";
		maskAttButt.style.display="block";
		*/
		maskAllScreen.style.width="950px";
		maskAllScreen.style.height="650px";
		maskAllScreen.style.top=(findPosY1(obj1)+obj1.offsetHeight)+"px";
		maskAllScreen.style.display="block";

		maskAllScreen2.style.width="950px";
		maskAllScreen2.style.height=(findPosY1(obj1))+"px";
		maskAllScreen2.style.top="0px";
		maskAllScreen2.style.left="0px";
		maskAllScreen2.style.display="block";

		if(lastLayerMoved!=null && layer1!=lastLayerMoved ){
			lastLayerMoved.style.display="none";
		}
		if(layer1==lastLayerMoved ){
			//fetchAndInc.f();
		} 
		secondTimeOpen=(secondTimeOpen==0||number1==null?1:2);
		if(	layer1==lastLayerMoved){
			floatLayer1(layer1,obj1);
		}
		if(layer1!=lastLayerMoved && inscroll!=null && inscroll.style.height!=attrbMenuHeight+'px'&& inscroll.childNodes.length >maxAttrbRows){
			inscroll.style.height=attrbMenuHeight+'px';
			if(navigator.userAgent.indexOf("Opera")>-1 ){
					inscroll.style.overflow="auto";
			}
			inscroll.scrollTop=0;
		}
		lastLayerMoved=layer1;
		//alert(lastLayerMovedName+" +++ 1");
		setTimeout("if(secondTimeOpen==2 && '"+lastLayerMovedName+"'==lastLayerMovedName ){lastLayerMoved.style.display='block';if(lastLayerMovedObj.className=='men')lastLayerMovedObj.className='ov1';}",300);
		//alert(secondTimeOpen+" +++ 1"+lastLayerMovedName)
		if(secondTimeOpen==1){
			setTimeout("if(secondTimeOpen==1 && '"+lastLayerMovedName+"'==lastLayerMovedName ){attrbMenu(lastLayerMovedName,lastLayerMovedObj,'1');}",100);
		}
	}
}

function attrbMoveOutMenu(timeToWait){
		secondTimeOpen=0;
		var nowCount=fetchAndInc.f();
		//alert(wasEnterNowAttrbMenu+" "+nowCount+" +++ 2");
		if(timeToWait==null){
			timeToWait=500;
		}
		//alert(navigator.userAgent);
		if(navigator!=null && (navigator.userAgent.indexOf("Opera")>-1 )&& timeToWait<100){
			maskAttButt.style.display="none";
			maskAllScreen.style.display='none';
			maskAllScreen2.style.display='none';
		}
		setTimeout("if( wasEnterNowAttrbMenu=="+nowCount+" && lastLayerMoved!=null){maskAllScreen.style.display='none';maskAllScreen2.style.display='none';maskAttButt.style.display='none';if(lastLayerMovedObj.className=='ov1'){lastLayerMovedObj.className='men';}lastLayerMoved.style.display='none';}",timeToWait);
}

function fecthIncFunc() { 
    wasEnterNowAttrbMenu++;
	return wasEnterNowAttrbMenu;
               }
var fetchAndInc = { f:fecthIncFunc  };


