/**************************** FUnctions used in GIP -- Start ******************************************/

String.prototype.startsWith = function(str)
{
	//alert(this.value);
	var strInput=str.toLowerCase(); 
	var strOrig=this.toLowerCase();;
	return (strOrig.match("^"+strInput)==strInput)
}


//-------------------------------
//To trim the user entered string
//---------------------------------
function strim(strString)
{ 
   // copy the string so that original string is preserved
   var strCopy = new String(strString);

   // trim leading whitespace
   strCopy = strCopy.replace(/^\s+/, "");

   // trim trailing whitespace
   strCopy = strCopy.replace(/\s+$/, "");
   
   // return trimmed string
  
   strString = strCopy;
   return strString;
}

function validateEmailId(strObj)
{
  var strValue,strEmail,arrEmail,strEmailAddr;
  var i,j,strRegExp,strFlag;
  strFlag="true";
  strRegExp=/^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/;
  strValue=strim(strObj.value);
  strEmail="";
  for(i=0;i<strValue.length;i++)
	{
		if(strValue.charCodeAt(i)!=13 && strValue.charCodeAt(i)!=10)
		{
			strEmail=strEmail + strValue.charAt(i);
		}
	}  
  strEmail = strEmail.replace(/,/g,";");
strObj.value=strEmail ;
	strEmailAddr="";
	arrEmail = strEmail.split(";");
	for(var j=0;j<arrEmail.length;j++)
  {
    strEmailAddr=strim(arrEmail[j]);
    strEmailAddr=strEmailAddr.toLowerCase();
    if (! strEmailAddr.match(strRegExp)) 
    {
      strFlag="false";
      break;
    }
  }
  if(strFlag=="false")
  {
    alert("Please enter valid email id");
    strObj.select();
    return false;
  }
  else
    return true;
}


function setText(elem,text)
{   
    ///alert("set");
	if(elem.value=="" || elem.value==text)
	{
		elem.value=text;
		elem.style.fontStyle='italic';
		if(elem.id=="txt_cc")
		    document.getElementById("td_cc_flag").innerHTML="";
		//elem.style.color='#959595';
	}
}
function clearText(elem,text)
{
    //alert("set");
	if(elem.value=="" || elem.value==text)
	{
		elem.value="";
		elem.style.fontStyle='normal';
		//elem.style.color='black';
	}
} 

//*******************************************************************************************************************/
/*  Author : Taher N D
/*  Purpose : To make a Dynamic Div (which appears at the position where mouse is click. Data can be populated in that /*           div (in form of table) using Ajax call.

/*  Inputs : --> Invoice id (or any kind of unique id). This id will be used as the div id.  
/*           --> Url. This url will be used to make Ajax call and get the data for the div. The url should be complete i.e with all the query string parameters  defined.

//********************************************************************************************************************/

var strPrevId="";
var origPosx=0; // These orig (meaning variables holding original values) will hold the mouse coordinates for displaying processing bar
var origPosY=0; // Same as above
var posx = 0;
var posy = 0;
var ajaxObj;
var Gbl_id;
var Gbl_pos;
var width;
var height;

function openDiv(id,url,posInd,ht,wd,e)
{
    //id=will be used for creating elemets
    //url=will be used for getting data (in AJAX Call)
    //ht=Height of the div
    //wd=width of the div
    Gbl_id=id;
    Gbl_pos=posInd; // it will indicate where should the div come i.e. on left side of mouse click or on right side. (Currently only for left its taken care. Modify the conditions as per need)
    width=wd;
    height=ht;  
    
    if(!e)
    {
      //For IE
      e=window.event;
    }
  
	if (e.pageX || e.pageY) 	
	{
    //Mozilla
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	
	{
    //IE
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	
	origPosx=posx; /// Here we are storing mouse coordinates in orig variables so that we can do manipulations on posx and posy
	origPosY=posy; // Same as above
	
	posx=parseInt(posx)/0.75;
	posy=parseInt(posy)*0.5; // This is only done for a specific page. Change it as per your needs
	
	if(strPrevId != "") {hideDiv(strPrevId);} // If any div is created previously then hide that before showing this new one. We have the id of last created div in strPrevId.

  //Remove the previously created div from DOCUMENT tree.
  if(document.getElementById("Container"))
  {
      var d=document.getElementById("Container");
      if(d)
        document.body.removeChild(d); 
  }   
      
	makeDiv(id,1,origPosx,origPosY,"75","20","White","block","999","<img src='../Images/Pross_crcl.gif' alt='Loading' />");
	callAjaxPage(url,getData4Div);
    
}
function hideDiv(id)
{			
    //id= the id of the div which we want to hide.
	var Elements = document.getElementsByTagName('Div');		
			
	for(var i=0;i<Elements.length;i++)
	{
	    if(Elements[i].id==id || Elements[i].id=="Container") //Hiding the previously created <div>
	    {
        $("#Container").fadeOut("normal");			
      }	
	}									
}		 
function makeDiv(id,border,left,top,width,height,bgColor,visibility,zIndex,strHTML) 
{
    /*
    id=id of the div
    border --> 1=yes ; 0=no
    left=left co ordinate of the div (for positioning)
    top=top co ordinate of the div (for positioning);
    strHtml= the  content to be displayed inside the div
    */
   
    var ObjDIV;
    var strBorder;
    var exDiv,exDivEnd;
    if(border==0){strBorder=""}
    else{strBorder="1px solid gray";} 
    
    //height:'+height+'px;
    
    //exDiv='<DIV border="0" style="cursor:move;display:'+visibility+';background-color:#C0C0C0;Z-INDEX:'+zIndex+';width:'+width+'px;POSITION: absolute;top:'+top+'px;left:'+left+'px" id="Container-inner" onmousedown="beginDrag(this)">'+'<DIV boder="0" id="shadow-container"><DIV id="shadow1" class="shadow1"><DIV id="shadow2" class="shadow2"><DIV id="shadow3" class="shadow3">'
    exDiv='<DIV border="0" style="cursor:move;background-color:#C0C0C0;width:'+width+'px;" id="Container-inner" onmousedown="beginDrag(this)">'+'<DIV boder="0" id="shadow-container"><DIV id="shadow1" class="shadow1"><DIV id="shadow2" class="shadow2"><DIV id="shadow3" class="shadow3">'
    
    exDivEnd='</DIV></DIV></DIV></DIV></DIV>';

    var div= exDiv+'<div id='+id+' class="container">' + strHTML + '</div>'+exDivEnd
    		
    //document.body.insertAdjacentHTML("BeforeEnd",div); // Doesnt work in MOZILLA
    
     
    ObjDIV = document.createElement('div');
    ObjDIV.setAttribute('id','Container');
    
    ObjDIV.style.display = visibility;
    ObjDIV.style.position = 'absolute';
    ObjDIV.style.zIndex=zIndex;
    ObjDIV.style.width=width+'px';
    ObjDIV.style.top=top+'px';
    ObjDIV.style.left=left+'px';  
    
    ObjDIV.innerHTML = div;
    
    
    var obody = document.getElementsByTagName('body')[0];
    obody.appendChild(ObjDIV);

    ObjDIV.onmousedown=beginDrag;

    $("#Container").fadeIn("normal"); //Show the div (fadeIn) using jQuery
    
    strPrevId=id; //Storing the id. It will be used to hide the div when creating another div.
   
}
function getData4Div()
{
	if (ajaxObj.readyState==4)// || ajaxObj.status==200)
    {   
       
         if(ajaxObj.responseText=="|^|EXP|^|") //This is done for a specific page. Change it as per need. It is done to check whether session has expired or not
         {
            window.document.location.reload(true);
            //return;
         }
        hideDiv(strPrevId);
        
        //Before making another div of the same id remove the previously created <div>.
        //Because if there are multiple elements with same id (i.e Container) then jquery will not work.
        if(document.getElementById("Container"))
        {
          var d=document.getElementById("Container");
          document.body.removeChild(d); 
        }
               
        if(Gbl_pos != "" && Gbl_pos == "LEFT")
        {
          makeDiv(Gbl_id,0,posx-500,posy,width,height,'#c0c0c0','none','999',ajaxObj.responseText)
        }  
        else
        {
          makeDiv(Gbl_id,0,posx,posy,width,height,'#c0c0c0','none','999',ajaxObj.responseText)
        }  
    } 	 
} 
function callAjaxPage(strUrl,strFnName)
{
      strUrl=strUrl+"&QRd="+Math.floor(Math.random()*1000);
      //ajaxObj=IntializeAjax();
      ajaxObj=InitializeRequest(strFnName);
      ajaxObj.onreadystatechange=strFnName;
      ajaxObj.open("GET",strUrl,true);
      ajaxObj.send(null); 
} 

function setOpacity(obj,value)
{

    obj.style.opacity = (value / 100);
		obj.style.MozOpacity = (value / 100);
		obj.style.KhtmlOpacity = (value / 100);
		obj.style.filter = 'alpha(opacity=' + value + ')';
}


/**************************** FUnctions used in GIP -- END ******************************************/


