//javascript
/***************************
 * VICfast common functions.
 * 
 *
 **************************/

//////// topics covered
/***************************
 1-	browser sniffing
 2-	openning new browser windows/pop-ups.
 3-	swapping images.
 4-	property changing and manipulating
 5-	page redirection and location url.
 6-	form actions.
 7-	string functions .
 8-	date & time.
 *
 **************************/

/***************************
 1-	browser sniffing
 *
 **************************/
// browser type/sniffer (incomplete)
var N = (document.all) ? 0 : 1;

var voidFunction = "javascript:void(0)";


//searching and finding objects
function MM_findObj(n, d) { //v4.01
		  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
			d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
		  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
		  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
		  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


/***************************
 2-	openning new browser windows/pop-ups.
 *
 **************************/

//openning new browser window
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
function OpenTownSearchWindow(){
	MM_openBrWindow('TownSearch.aspx','TownSearch','scrollbars=yes,width=400,height=250');
}
//opens the help window
function OpenHelpWindow(helplink){
	features = "scrollbars=yes,width=430,height=500";
	MM_openBrWindow(helplink,'app_help',features);
}
//opens Offered Contract Pdf document wiondow
function OpenApplicationContractWindow(filename, mode){
	features = (mode==1)?"":"scrollbars=yes,width=800,height=800";
//	aurl = "RncAppBreif.aspx?ItemRef=" + appnum + "&mode="+mode; 
	MM_openBrWindow(filename,'app_contract',features);
}

function OpenConnotePdfWindow(theURL) { //v2.0
	features = "scrollbars=yes,titlebar=no,status=yes,width=800,height=600";
	window.open(theURL,'_connote', features);
}

function OpenAdminLabelsReportWindow(bnum, cnum, bid) { //v2.0
	url = 'WBAdminViewLabels.aspx?bnum=' + bnum + '&cnum=' + cnum +'&bid='+bid;
	features = "scrollbars=yes,titlebar=no,status=yes,width=600,height=400";
	window.open(url,'_bookingitems', features);
}


function OpenLabelsReportWindow(bnum) { //v2.0
	url = 'ViewLabels.aspx?bnum=' + bnum;
	features = "scrollbars=yes,titlebar=no,status=yes,width=600,height=400";
	window.open(url,'_bookingitems', features);
}

function OpenRemoveConnoteWindow(bnum) { //v2.0
	url = 'RemoveConnote.aspx?bnum=' + bnum;
	features = "scrollbars=yes,titlebar=no,status=yes,width=440,height=230";
	window.open(url,'_removeconnote', features);
}

function OpenLinkWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


/***************************
 3-	swapping images.
 *
 **************************/

//swaping images
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/***************************
 4-	property changing and manipulating
 *
 **************************/
 
function MM_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}


// text box value
function MM_setTextOfTextfield(objName,x,newText) { //v3.0
  var obj = MM_findObj(objName); if (obj) obj.value = newText;
}

// showing and hiding divs
function DisplayTheDiv(name, setting){//index){
	var divObj = MM_findObj(name);//divname);
	if(divObj!=null)
		divObj.style.display = setting;
}//javascript:DisplayThisDi

//grabbing property!!
//MM_changeProp(fields[i],'','style.backgroundColor',setColor,'INPUT/TEXT');
function MM_getProp(objName,x,theProp){//  ,theValue) { //v6.0
  var obj = MM_findObj(objName);
  var res=  '';
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
	res = eval("obj."+theProp);//+".value");
  }
  return res;
}

///divs and layers
function ToggleBox(szDivID, iState) // 1 visible, 0 hidden
{
    if(document.layers)	   //NN4+
    {
       document.layers[szDivID].visibility = iState ? "show" : "hide";
    }
    else if(document.getElementById)	  //gecko(NN6) + IE 5+
    {
        var obj = document.getElementById(szDivID);
        obj.style.visibility = iState ? "visible" : "hidden";
    }
    else if(document.all)	// IE 4
    {
        document.all[szDivID].style.visibility = iState ? "visible" : "hidden";
    }
}

// showing and hiding divs
// seeting:
//		"" (empty string): display block
//		"none" : hide
function DisplayThisDiv(name, setting){//index){
	var divObj = MM_findObj(name);//divname);
	if(divObj==null){
		return;
	}
	divObj.style.display == setting;
}

////// enableing and disabling controls

/// enabling disabling
function SetElementEnabled(name, settings){
	var elem = MM_findObj(name);// document.getElementById(name);
	if(elem==null){
		return;
	}
	/*
	if(document.all){
		
	}*/
	elem.disabled = settings;
}
/*
 var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }*/
/// faux disabling
function SimulateDisable(id){
	obj = document.getElementById(id);
	obj.onfocus = function preventFocus(e) { this.blur(); };
}
function UnSimulateDisable(id){
	obj = document.getElementById(id);
	obj.onfocus = null;//function preventFocus(e) { this.blur(); };
}

function ChangeObjectClass(objid, objclass){
	var theObj = document.getElementById(objid);
	if(theObj)
		theObj.className = objclass;
}

function ChangeObjectClassFindByMM(objid, objclass){
	var theObj = MM_findObj(objid);
	if(theObj==null)
		return;
	theObj.className = objclass;
}


/***************************
 5-	page redirection and location url.
 *
 **************************/

// redirect the page
function RedirectPage(url){
	window.location.href=url;
}

// redirect the top page in frameset
function RedirectTopTo(url){
	top.location.href = url;
}

//page and forms url and query strings
function GetPageTitle(cName, doc){
	var name = doc.document.title;
	name = name.substring(cName.length, name.length);
	return name;
}

function StripUrlOfParameters(url){
  var s = url;
  if ( url.indexOf("?") > -1 ){
	s = url.substring(0, url.indexOf("?"));
  }
  return s;
}

function StripParametersOfUrl(url){
	var s = url;
	if ( url.indexOf("?") > -1 ){
		s = url.substring(url.indexOf("?")+1);
	}
	return s;
}

function AddUrlParameters(url, para, val){
	s = url;
	if(s.indexOf(para+"="+val)>s.indexOf("?"))
		return s;
	if(s.indexOf(para)<0){
		if(s.indexOf("?")==-1)
			s+="?";
		else
			s+="&";
		s+=para+"="+val;
	}else{
		s = StripUrlOfParameters(url);
		query = StripParametersOfUrl(url).split("&");
		for(i=0; i<query.length; i++){
			j = query[i].indexOf(para);
			if(j==0 && query[i].indexOf(para+"=")==0)
				query[i] = para+"="+val;
			s+=(i==0)?"?":"&";
			s+=query[i];
		}
		
	}
	return s;
}

//history functions
function ReturnToRefererWhenFlaged(name){
	txtObj = MM_findObj(name);
	if(txtObj.value=='1'){
		history.go(-1);
	}
}

/***************************
 6-	form actions.
 *
 **************************/

/*
function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (
		aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return strReturn;
}
*/

/// args[0] = form id
function SetFormActionUrl(){
	args = SetFormActionUrl.arguments;
	if(args.length<2)
		return;
	formObj = document.getElementById(args[0]);
	s = GetURLParam()+"?";
	isfirst = true;
	for(i = 1; i<args.length;i=i+2){
		anobj = MM_findObj(args[i]);
		if(anobj!=null && anobj.value.length>0){
			if(!isfirst)
				s+="&";
			s+=args[i+1]+"="+anobj.value;
			isfirst=false;
		}
	}
	formObj.action = s;
}


/***************************
 7-	string functions .
 *
 **************************/

// trim white spaces on the left
function leftTrim(sString)
{
	while (sString.substring(0,1) == ' ')
		sString = sString.substring(1, sString.length);
	return sString;
}


// trim white spaces on the right
function rightTrim(sString)
{
	while (sString.substring(sString.length-1, sString.length) == ' ')
		sString = sString.substring(0,sString.length-1);
	return sString;
}

// trim white spaces on the left and right
function trimAll(sString) 
{
	return leftTrim(rightTrim(sString));

}

/// replaces all occurancesd of the old text to the new text
/// casesensitive will ensure that only the exact text in the exact case will be detected and replaced
/// recursive means the function will look for the old text from the start of the text each time, 
/// not recursive means the function will look for the old text from the last found index+ the length of the new text.
/// for example, the sentence: "This is the Memomo Memomo", when replacing: "Memo" with "Me", the results:
	// recursive result is: "This is the Me Me"
	// non-recursive result is: "This is the Memo Memo"
function replaceAll(src, oldtxt, newtxt,casesensitive, recursive){
	if(oldtxt==newtxt)
		return src;
	else if(!casesensitive && oldtxt.toLowerCase()==newtxt.toLowerCase())
		return src;
	var txt1 = '';
	var txt2='';
	var rep = '';
	var downsrc = (casesensitive)?src:src.toLowerCase();
	var downold = (casesensitive)?oldtxt:oldtxt.toLowerCase();
	var i = downsrc.indexOf(downold);
	while(i>=0){
		txt1 = (i>0)?src.substring(0,i):'';
		txt2 = ((i+oldtxt.length)<=src.length)?src.substring(i+oldtxt.length, src.length):'';
		
		src = txt1+newtxt+txt2;
		downsrc = (casesensitive)?src:src.toLowerCase();
		if(recursive)i=downsrc.indexOf(downold, i);
		else i=downsrc.indexOf(downold, i+newtxt.length);
	}
	return src;
}


//comparing strings
//args[0] & [1]: strings to compare; args[2]==1: ignore case
function CompareTwoStrings(){
	var args = CompareTwoStrings.arguments;
	var str0 = (args[2]==1)?args[0].toLowerCase():args[0];
	var str1 = (args[2]==1)?args[1].toLowerCase():args[1];
	if(str0 == str1)
		return 1;
	return -1;
}


//finds index of a string in a string array
function GetStringIndexInArray(anArr, str){
	for(i=0;i<anArr.length;i++){
		if(CompareTwoStrings(anArr[i],str,1)>0)
			return i;
	}
	return -1;
}



/***************************
 8-	date & time.
 *
 **************************/
// date and time
function GetCurrentDateTime(){
	var mydate=new Date()
	var year=mydate.getYear()
	if (year < 1000)
		year+=1900
	var day=mydate.getDay()
	var month=mydate.getMonth()
	var daym=mydate.getDate()
	if (daym<10)
		daym="0"+daym
	var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
	var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
	
	document.write(mydate)
}


/***************************
 9-	focusing and bluring.
 *
 **************************/
function BlurOnFucus(id){
	obj = document.getElementById(id);
	if(obj)
		obj.blur();
}
