// Digital Lines Ltd - Jscript Tool Pack V1.00

// #############################################################################
// CommPHP_Routed(TargetURL, Parameters,CallThisFunction)
// Ajax PHP Comm Function
// #############################################################################

// Associated PHP file TPackCommCore.php                                            
   function CommPHP_Routed(url,parameters,CallThisFunction) {	   
      var http_request = false;
      var http_response = "";
      if (parameters.search(/http:/i) >= 0) {parameters = parameters.replace(/&/g,"|-|-|-|");};
      if (window.XMLHttpRequest) {
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
             }
          catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
                }
             catch (e) {alert("Sorry but your browser does not support this service!");
			 return false;}
         }
      }
	  			  
      if (!http_request)
        {
		 throw ("[1001]");
        };
      if (parameters!=""){parameters+='&mrrm='+Math.random()}

      http_request.onreadystatechange=function()
        {
           if (http_request.readyState==4)
             {
             http_response = http_request.responseText;
             // HTTP Return char(10) + 2 white space - fix
              if (http_response.substr(0,3)==( chr(10)+chr(32)+chr(32))  )
              {http_response = http_response.substr(3,http_response.length-3)};
			// http_response = http_response.replace(chr(10),"");
			// http_response = http_response.replace(chr(13),"");			 
			// http_response = http_response.replace(/"/g,'\\"');			 
			// http_response = http_response.replace(/\n/g,"");			 			 
			// http_response = http_response.replace(/\r/g,"");			 			 			 
			// http_response = http_response.replace(/'/g,"\\'");	
			// INSTEAD ==> escape(http_response)  
     	 	 http_response = escape(http_response);
             CallThisFunction = CallThisFunction.replace(/"TX_RESPONSE_PLACEHOLDER"/,"TX_RESPONSE_PLACEHOLDER");
             CallThisFunction = CallThisFunction.replace(/TX_RESPONSE_PLACEHOLDER/,'"'+http_response+'"');			 
 		     //document.write(  CallThisFunction   ) ;
             //alert(CallThisFunction);
             eval( CallThisFunction );
			 
             };
        };
        http_request.open('GET', url + parameters, true);
        http_request.send(null);
   };


// CommPHP FunctionRouter for updating elements with ID's using PopPage
   function CommPHPUpdateElm(OutputID,OutputAttr,OutputPreValue,OutputPostValue,TXRepsonse) {
	 TXRepsonse = unescape(TXRepsonse);
     if (OutputPreValue == 'undefined' || OutputPreValue == null) {OutputPreValue='';};	 
     if (OutputPostValue == 'undefined' || OutputPostValue == null) {OutputPostValue='';};	 	 
     PopPage(OutputID,OutputAttr,OutputPreValue+TXRepsonse+OutputPostValue);
   };




// #############################################################################
// GetFormVarByName(VarName)
// The GetVar Function is a quick way of getting the form variable by name.
// You can also use this to get any parameter (eg x=y) from the URL
// #############################################################################

 function GetFormVarByName(VarName)
  {

    var data = new Array();
    var FormVarPart = new Array();
    var URLStrBuff = window.location.search;
    var URLStrBuff = document.location.search;

    if (URLStrBuff.substring(0, 1) == '?')
        {
        URLStrBuff = URLStrBuff.substring(1);
        URLStrBuff = unescape(URLStrBuff);
        var data = URLStrBuff.split('&');
        for (i = 0; (i < data.length); i++)
          {
          FormVarPart = data[i].split('=');
          if (VarName == FormVarPart[0])
           {return FormVarPart[1];};
          };

        };
  return ("");
  };





// #############################################################################
// PopPage() AND PopPageArr()
// These functions replace text in the HTML
//
// #############################################################################

var ElmValArr = new Array()

function PopPage(ElmId,ElmAttr,ElmVal)
 {
    if (ElmAttr=="innerHTML")
         {
         document.getElementById(ElmId).innerHTML=ElmVal;
         }
    else
         {
          var TargElm = document.getElementById(ElmId)
          TargElm.setAttribute(ElmAttr,ElmVal);
         }
 };

function PopPageArr(ElmId,ElmAttrStr,ElmValArr)
 {
    var ElmAttrArr = new Array()
    ElmAttrArr = ElmAttrStr.split(',');

    for (i = 0; (i < ElmAttrArr.length); i++)
       {
            var TargElm = document.getElementById(ElmId)
            TargElm.setAttribute(ElmAttrArr[i],ElmValArr[i]);
       };
 };




// ###################################################################################
// BuildSelectOption(SelectId,SelectName,SelectClass,OptionAttribs,OptionSelected)
// This function builds a <SELECT> <OPTION></OPTION> </SELECT> HTML set, FeildAttibs
// is an array of attributes as : ["name","id","option Text","selected"]
// Note: The default selected item can be referenced in two ways?
// ###################################################################################


function BuildSelectOption(SelectId,SelectName,SelectClass,OptionAttribs,OptionSelected)
{
    var CRows = 0;
    document.write('<select id="'+SelectId+'" name="'+SelectName+'" class="'+SelectClass+'">');
    for (CRows=0;CRows < OptionAttribs.length ;CRows++)
        {
         document.write('<option');
         document.write(' value="'+OptionAttribs[CRows][1]+'"');
         if (OptionAttribs[CRows][3]=="selected")
            { document.write(' selected="selected" ') };
         if (OptionSelected == OptionAttribs[CRows][1])
            { document.write(' selected="selected" ') };
         document.write('>'+OptionAttribs[CRows][2]+'</option>');
        }
    document.write('</select>');
};





// #############################################################################
// CheckBoxBool(Id)
// Flips the state/value of a checkbox from true to false etc
//
// #############################################################################


function CheckBoxBool(CBoxObj)
{
if (CBoxObj.checked == true)
 {CBoxObj.value = 'true'};
if
(CBoxObj.checked == false)
 {CBoxObj.value = 'false'};
};



// #############################################################################
// Show & hide Fuctions V2
// Shows and Hides elements
//
// #############################################################################


// var TagIds=new Array('part1','part2','part3','part4');   //       ===>  Sets array for hideall


function switchid(id){
	hideallids();
	showdiv(id);
}

function hideallids(){
	//loop through the array and hide each element by id
	for (var i=0;i<TagIds.length;i++){
		hidediv(TagIds[i]);
	}
}

function hidediv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function showdiv(id) {
	//safe function to show an element with a specified id

	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
};


// Chr ASCII return function
function chr( ascii ) {
    return String.fromCharCode(ascii);
};


// Check Email Format
function CheckEmail(EmailAddr){
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
		if (!EmailAddr.match(re) && EmailAddr.match(re_two)){
			return (-1);
		}
};



// Set Cookie
function setCookie(c_name,value,expiredays)
{
	path="/";
var exdate=new Date();
if (path=="") {path="/"}
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : " ; path="+path+"; expires="+exdate.toGMTString());
};










