﻿// JScript File

//Position welcome image for Autocomplete. Luke: hide forever 12 Oct 2009
function positionMessage(){
//    var divSuburb = document.getElementById('divEnterSuburb');
//    var txt = document.getElementById('ctl00_ContentPlaceHolder1_txtPostcode');
//    if (divSuburb != null && txt != null){
//        var pos = _b.DOM.getPos(txt);
//        divSuburb.style.left = pos.x + "px";
//        divSuburb.style.top = (pos.y + txt.offsetHeight - 5) + "px";
//        divSuburb.style.width = txt.offsetWidth + "px";
//        if(txt.value.length == 0){divSuburb.style.visibility = "visible";}
//            else{divSuburb.style.visibility = "hidden";}
//    }
}

//position service style popup and set visibility
/*
function switchSType(event){
    var div = document.getElementById('divSType');
    var lnk = document.getElementById('lnkSType');
    if (div && lnk){
        var pos = _b.DOM.getPos(lnk);
        div.style.left = (pos.x - 50) + "px";
        div.style.top = (pos.y + lnk.offsetHeight) + "px";
    }    
    
    if(div.style.display == "none" || div.style.display == ""){div.style.display = "block";}else{div.style.display = "none";}
    var img = $get("imgSType");
    if (img){
        if(div.style.display == "none"  || div.style.display == ""){img.src="http://carservice.com.au:8080/images/icons/nav_close.gif"}else{img.src="http://carservice.com.au:8080/images/icons/nav_open.gif"}
    }
}
function hideSType(){
    var div = document.getElementById('divSType');
    var img = $get("imgSType");
    if (div && img && div.style.display == "block"){
        div.style.display = "none";
        img.src="http://carservice.com.au:8080/images/icons/nav_close.gif";
    }
}
*/
//No spaces in sName
function OpenWindow(sURL, sName, iWidth, iHeght){
	var wnd=window.open(sURL,sName,'width=' + iWidth + ',height=' + iHeght,false);
	wnd.focus();
}

//No spaces in sName
function OpenWindowScroll(sURL, sName, iWidth, iHeight){
	var wnd=window.open(sURL,sName,'scrollbars=yes,top=20,left=20,width=' + iWidth + ',height=' + iHeight,false);
	wnd.focus();
}

function OpenRatingWindow(iProvID){
	var wnd=window.open('Rating2.aspx?ProvID=' + iProvID ,'Rating','scrollbars=yes,top=20,left=20,width=630,height=800',false);
	wnd.focus();
}

//Confirm delete a car from car-service-user-page.aspx
function ConfirmDeleteCar(e)
{
    if (!window.confirm("You are about to delete a car from the list.\nClick OK to proceed, Cancel otherwise."))
    {
        e.returnValue = false;
        if (e.preventDefault)
            e.preventDefault();
        return false;
    }
}

//Hide or show Master page login/password
function MasterLoginVisible(Value){
	if (Value == true){document.getElementById('tblMasterLogin').style.display = "";}
	else{document.getElementById('tblMasterLogin').style.display = "none";}
}

//Switch visibility of Master page login/password section
function SwitchMasterLoginVisible(){
	if (document.getElementById('tblMasterLogin').style.display == ""){document.getElementById('tblMasterLogin').style.display = "none";}
	else {document.getElementById('tblMasterLogin').style.display = "";}
}

//Show element (visible=true)
function ShowElement(elementID){
	alert('ShowElement: ' + elementID);
	var elem = document.getElementById(elementID);
	alert('Element: ' + elem);
	if (elem != null){elem.style.display = "";}
}

//Hide element (visible=false)

//Disable all elements on the form
function DisableAll()
{
	var count = document.forms[0].elements.length;
	for (i=0; i<count; i++) 
	 {
		var element = document.forms[0].elements[i]; 
		
	}
}

//Enable all elements on the form
function EnableAll()
{
	var count = document.forms[0].elements.length;
	for (i=0; i<count; i++) 
	 {
		var element = document.forms[0].elements[i]; 
		if (element != null) {element.disabled=false;};
	}
}

//Search page - new design. Switch Info/enquiry in infoWindow (gmaps)
function infoWindowParser(provId){
	//var txtName, txtEmail, txtPhone, txtNameMain, txtEmailMain, txtPhoneMain, txtProvMain, txtModeMain; 
	var divInfo, divEnquiry, btnSwitch, btnSend, divThanks;
	var cboCarline
	
	divInfo = document.getElementById('divInfo' + provId);
	divEnquiry = document.getElementById('divEnquiry' + provId);
	divThanks = document.getElementById('divThanks' + provId);
	btnSwitch = document.getElementById('btnSwitch' + provId);
	btnSend = document.getElementById('btnSend' + provId);
	cboCarline = document.getElementById('cboCarline' + provId);
	
	divThanks.style.visibility = 'hidden';
	
	var divError = document.getElementById('divError' + provId);
	divError.style.visibility = 'hidden';
	
	if (divInfo.style.visibility != 'hidden'){
	    divInfo.style.visibility = 'hidden';
	    divEnquiry.style.visibility = 'visible';
	    btnSwitch.value = 'Back';
	    btnSend.style.visibility = 'visible';
	    cboCarline.focus();
	}
	else {
	    divInfo.style.visibility = 'visible';
	    divEnquiry.style.visibility = 'hidden';
	    btnSwitch.value = 'Enquiry';
	    btnSend.style.visibility = 'hidden';
	}
	
	// report to GA
	//urchinTracker('/clicks/Enquiry/ForProvID=' + provId);
}

//Search page - new design. Set values for main form fields (and reload the form to send enquiry <- removed)
function keepEnquiry(provId){
	//Validation
	var result = "Error"

	var divError = document.getElementById('divError' + provId);
	if (checkEnquiry(provId) == false) {
	    divError.style.visibility = 'visible';
	    return result;
	}
	
	divError.style.visibility = 'hidden';
	
	var txtName, txtEmail, txtPhone, txtNameMain, txtEmailMain, txtPhoneMain, txtProvMain, txtModeMain;
	var cboModel, cboService,txtModelMain, txtServiceMain, txtComments, txtCommentsMain, cboMake; 
	var divInfo, divEnquiry, btnSwitch;
	txtName = document.getElementById('txtName' + provId);
	txtEmail = document.getElementById('txtEmail' + provId);
	txtPhone = document.getElementById('txtPhone' + provId);
	cboModel = document.getElementById('cboModel' + provId);
	cboService = document.getElementById('cboService' + provId);
	txtComments = document.getElementById('txtComments' + provId);
	
	divInfo = document.getElementById('divInfo' + provId);
	
	txtNameMain = document.getElementById('ctl00_ContentPlaceHolder1_txtNameMain');
	txtNameMain.value = txtName.value;
	
	txtEmailMain = document.getElementById('ctl00_ContentPlaceHolder1_txtEmailMain');
	txtEmailMain.value = txtEmail.value;	
	
	txtPhoneMain = document.getElementById('ctl00_ContentPlaceHolder1_txtPhoneMain');
	txtPhoneMain.value = txtPhone.value;
	
	txtModelMain = document.getElementById('ctl00_ContentPlaceHolder1_txtModelMain');
	txtModelMain.value = cboModel.options[cboModel.selectedIndex].value;
	
	txtServiceMain = document.getElementById('ctl00_ContentPlaceHolder1_txtServiceMain');
	txtServiceMain.value = cboService.options[cboService.selectedIndex].value;
		
	txtProvMain = document.getElementById('ctl00_ContentPlaceHolder1_txtProvMain');
	txtProvMain.value = provId;
	
	txtModeMain = document.getElementById('ctl00_ContentPlaceHolder1_txtModeMain');
	txtModeMain.value = 'enquiry';
	
	txtCommentsMain = document.getElementById('ctl00_ContentPlaceHolder1_txtCommentsMain');
	txtCommentsMain.value = txtComments.value;

	cboMake = document.getElementById('ctl00_ContentPlaceHolder1_cboMake');

    // disabled - prevent page refresh
	//document.forms[0].submit(); 
	
	// report to GA
	//urchinTracker('/clicks/SendEnquiry/ForProvID=' + provId);

	result = txtName.value + '|' + txtEmail.value + '|' + txtPhone.value + '|' + txtProvMain.value + '|' + 
	    txtModelMain.value + '|' + txtServiceMain.value + '|' + txtComments.value + '|' + cboMake.options[cboMake.selectedIndex].value;
	
	return result
}

//Search page - autocomplete call back
function ItemSelected( source, eventArgs ) 
{

   //alert( " Key : "+ eventArgs.get_text() +"\r\n Value :  "+eventArgs.get_value() +"\r\n Source:" + source._serviceMethod); 
   
   //Used serviceMethod because ID and Name were returing incorrect data. 
    //if(source._serviceMethod == "GetHighSchoolCodes")
    //{
    //    var controlToPopulate = $get("<%= SelectedHighSchoolValue.ClientID %>");  
    //}
    
   // if(controlToPopulate != null)
    //{
    //    controlToPopulate.value = eventArgs.get_value();
    //}
    
}
/*
//Resize GMap control on form resize
function mapResize() {
	return
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	//IE 6+ in 'standards compliant mode'
	myWidth = document.documentElement.clientWidth;
	myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	//IE 4 compatible
	myWidth = document.body.clientWidth;
	myHeight = document.body.clientHeight;
	}
	//window.alert( 'Width = ' + myWidth );
	//window.alert( 'Height = ' + myHeight );
	var ucMap = document.getElementById('divGmap');
	//if (ucMap == null){alert('null');}else{alert('not null')}

	var vertOffset = 150, horOffset = 250;
	var minWidth = 200, myHeight = 100;
	if (ucMap == null ){}
		else 
		{
		
		if (myWidth - horOffset > 0){ucMap.style.width = (myWidth - horOffset) + 'px';}
			//else {ucMap.style.width = minWidth + 'px';}
		if (myHeight - vertOffset > 0){ucMap.style.height = (myHeight - vertOffset) + 'px';}
			//else {ucMap.style.height = minHeight + 'px';}		
		}
}


//Function to resize Main table to fit browser window.
function fitSizeOLD(){
	var myWidth = 0, myHeight = 0;
	
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
	alert('Width: ' +  myWidth + ' Height:' + myHeight);
	
	// Enquiry web page
    //var ucMap = $('#divGmap');
    //if (ucMap){fitSizeEnquiry(myWidth, myHeight, ucMap)}
    
    //fitSizeEnquiry();
    
    
}
*/

//Function to resize Forum page to fit browser window.
function fitSizeForum(myWidth, myHeight, frmForum){    
	var topAndBottomHeight = 105;
	var heightOffSet = 200;
    
    frmForum.width = myWidth;
    frmForum.height = (myHeight - heightOffSet);
}

//Function to resize Enquiry page to fit browser window.
function fitSize(){ 
    var ucMap = $('#divGmap');
	var totalHeight = 67 + 30 + 86 + 31;//Logo + NavBut + Top Red Strip + Bottom Red Strip = 214px
	var provWidth = 320, provHeaderHeight = 25, spHeaderHeight = 25;
    var minHeight = 270; minWidth = 1016;//2 top images + margins; 1016 to fit 1024 res
	var loc = $("#ctl00_ContentPlaceHolder1_pnlLocator"); //locator panel
	var locUP = $("#ctl00_ContentPlaceHolder1_upLocator");
	var locHeight = 0; if (loc.length>0) {locHeight=30;locUP.css('display','block');} else {locHeight=0;locUP.css('display','none');}
	
	
	if (($(window).width()) > minWidth)ucMap.width($(window).width() - provWidth)
	    else ucMap.width(minWidth - provWidth)
		
	//alert('window width: ' + $(window).width() + ' map:' + ucMap.width())
		
	//var pnlFilter = $('#ctl00_ContentPlaceHolder1_pnlFilter')
	//if (pnlFilter){pnlFilter.css({marginLeft: 0, marginTop: 0, top: ucMap.offset().top, left: ucMap.offset().left, width: ucMap.width() });}
		
    var listProv = $("#ctl00_ContentPlaceHolder1_pnlProviderList"); 
    var listSponsor = $("#ctl00_ContentPlaceHolder1_pnlSponsorList"); 
	if ($(window).height() - totalHeight - locHeight > minHeight)ucMap.height($(window).height() - totalHeight - locHeight)
	    else ucMap.height(minHeight)
	
    if (listProv.length>0) {
        if (listSponsor.length>0){
            listProv.height(ucMap.height() - provHeaderHeight + locHeight - listSponsor.height() - spHeaderHeight); 
        }else{
            listProv.height(ucMap.height() - provHeaderHeight + locHeight);
        }
    }	
	    
	var map = getMap();    
	if (map){map.checkResize();} 
}

/*
//Function to resize Enquiry page to fit browser window.
function fitSizeEnquiryOLD(myWidth, myHeight, ucMap){    
	
	//document.getElementById('ctl00_pnlContentPlaceHolder1').style.height = (myHeight - topAndBottomHeight) + 'px';
	
	var topAndBottomHeight = 105;
	var topOffset = 107, horOffset = 321, provListHeaderFooter = 30;//60;
	var minWidth = 759; //579 - old value
    var minHeight = 270;
    var pnlMap = document.getElementById('pnlGmap')
	    
	if (myWidth - horOffset > minWidth){
	    ucMap.style.width = (myWidth - horOffset) + 'px';
	    if (pnlMap != null) {
	        document.getElementById('pnlGmap').style.width = ucMap.style.width;
	    }
	}
	else {
	    ucMap.style.width = minWidth + 'px'; 
	    if (pnlMap != null) {
	        document.getElementById('pnlGmap').style.width = ucMap.style.width;
	    }
	}
		
    var listProv = document.getElementById('ctl00_ContentPlaceHolder1_pnlProviderList')
    
	if (myHeight - (topAndBottomHeight + topOffset) > minHeight){
		ucMap.style.height = (myHeight - (topAndBottomHeight + topOffset)) + 'px';
	    if (listProv != null) {
	        listProv.style.height = (myHeight - (topAndBottomHeight + topOffset + provListHeaderFooter)) + 'px';
	    }
	}
	else {
	    ucMap.style.height = minHeight + 'px';
	    if (listProv != null) {
	        listProv.style.height = (minHeight - provListHeaderFooter) + 'px';
	    }
	}
}
*/
//infoWindow Enquiry check.
    function checkEnquiry(provId){
		var lblError = document.getElementById('tdError' + provId);
		lblError.innerHTML = '';
		
		var txtName = document.getElementById('txtName' + provId);
		var txtEmail = document.getElementById('txtEmail' + provId);
		var txtPhone = document.getElementById('txtPhone' + provId);
		var cboModel = document.getElementById('cboModel' + provId);
		var cboService = document.getElementById('cboService' + provId);
    
		//Check Name
		if (txtName.value == ''){lblError.innerHTML = 'Name cannot be empty.';return false}
		if (txtName.value.length > 100){lblError.innerHTML = 'Name cannot be longer than 100 characters.';return false}
		//Check Email
		if (txtEmail.value == ''){lblError.innerHTML = 'Email cannot be empty.';return false}
		if (txtEmail.value.length > 100){lblError.innerHTML = 'Email cannot be longer than 150 characters.';return false}
		if(validateEmail(txtEmail.value) == false){lblError.innerHTML = 'Invalid Email format.';return false}
		//Check Phone
		if (txtPhone.value == ''){lblError.innerHTML = 'Phone cannot be empty.';return false}
		if (txtPhone.value.length > 100){lblError.innerHTML = 'Phone cannot be longer than 15 characters.';return false}
        if (validatePhone(txtPhone.value) == false){lblError.innerHTML = 'Invalid Phone format.';return false}		
		//Check Model combo
		if (cboModel.options[cboModel.selectedIndex].value < 0){lblError.innerHTML = 'Model should be specified.';return false}  
		//Check Service combo[\d\s\(\)]{8,15}
		if (cboService.options[cboService.selectedIndex].value < 0){lblError.innerHTML = 'Service should be specified.';return false}  
		
		return true
    }

//Email address validation	
function validateEmail(str) {

 // Function is used to verify if the given value is a possible valid email address. 
 // This function thus simply makes sure the email address has one (@), atleast one (.). 
 // It also makes sure that there are no spaces, extra '@'s or a (.) just before or after the @. 
 // It also makes sure that there is atleast one (.) after the @.

    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)


    if (str.indexOf(at)==-1){
       return false
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
       return false
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
    }

     if (str.indexOf(at,(lat+1))!=-1){
	    return false
     }

     if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
     }

     if (str.indexOf(dot,(lat+2))==-1){
	    return false
     }

     if (str.indexOf(" ")!=-1){
	    return false
     }
     return true					
}

function validatePhone(str) {
    var patt = new RegExp("^[0-9\(\)| ]{8,15}");
    var matc = patt.exec(str);
    return (str == matc);
}

//Limit characters to enter to text field.
// copyright 1999 Idocs, Inc. http://www.idocs.com
// Distribute this script freely but keep this notice in place
// onKeyPress="return numbersonly(this, event);"
function numbersonly(myfield, e, dec)
{
	var key;
	var keychar;
	
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);
	// control keys
	if ((key==null) || (key==0) || (key==8) || 
		(key==9) || (key==13) || (key==27) )
	   return true;
	
	// numbers
	else if ((("0123456789() -").indexOf(keychar) > -1))
	   return true;
	
	// decimal point jump
	else if (dec && (keychar == "."))
	   {
	   //myfield.form.elements[dec].focus();
	   //return false;
	   }
	else
	   return false;
}

//Only digits:0-9
// onKeyPress="return digitsonly(this, event);"
function digitsonly(myfield, e, dec)
{
	var key;
	var keychar;
	
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);
	// control keys
	if ((key==null) || (key==0) || (key==8) || 
		(key==9) || (key==13) || (key==27) )
	   return true;
	
	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
	   return true;
	
	// decimal point jump
	else if (dec && (keychar == "."))
	   {
	   //myfield.form.elements[dec].focus();
	   //return false;
	   }
	else
	   return false;
}

//Show JS pop-up error panel. 
function showErrorMessage(message){
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }     
    var divError = document.getElementById('divErrorMessage');
    if (divError){
        divError.style.left = (myWidth/2 - 150) + 'px';
        divError.style.top = myHeight/2 + 'px';
        divError.style.display = 'block';
        var tdMessage = document.getElementById('tdErrorMessage');
        tdMessage.innerHTML = message;
    }
}

//Hide JS pop-up error panel. 
function hideErrorMessage(){

    var divError = document.getElementById('divErrorMessage');
    if (divError){divError.style.display = 'none';}
}

//Hide imgStartHere - image shown for the first time under make combo. 
function hideStartHere(){
//    var img = document.getElementById('imgStartHere');
//    if (img != null){img.style.visibility = 'hidden';}
}

//hide/show div element
function hideDiv(divId) {
    var div = document.getElementById(divId);
    if (div == null){return};
    
    if (document.getElementById) { // DOM3 = IE5, NS6
        div.style.visibility = 'hidden';
    }
    else {
        if (document.layers) { // Netscape 4
        div.visibility = 'hidden';
    }
    else { // IE 4
        div.style.visibility = 'hidden';
    }
    }
}

function showDiv(divId) {
    var div = document.getElementById(divId);
    if (div == null){return};
    
    if (document.getElementById) { // DOM3 = IE5, NS6
        div.style.visibility = 'visible';
    }
    else {
        if (document.layers) { // Netscape 4
        div.visibility = 'visible';
    }
        else { // IE 4
        div.visibility = 'visible';
    }
    }
} 

//Set position for div elemnt. If no coordinates specified - position to the mouse cursor.
//ev - windows event, divId - id of div element
//top/left - coordinates
//offsetX/offsetY - hor/vert offset
function positionDiv(ev,divId,top,left,offsetX,offsetY) {
    var div = document.getElementById(divId);
    if (div != null){
        if(offsetX == null){offsetX = 0};
        if(offsetY == null){offsetY = 0};
        
        if (ev.pageX || ev.pageY) {
            left = offsetX + ev.pageX ;
            top = offsetY + ev.pageY ;
        } else if (ev.clientX || ev.clientY) {
            left = offsetX + ev.clientX + document.body.scrollLeft;
            top = offsetY + ev.clientY + document.body.scrollTop;
        }    
        
        if (document.getElementById) { // DOM3 = IE5, NS6
            div.style.top = top + 'px';
            div.style.left = left + 'px';
        }
        else {
            if (document.layers) { // Netscape 4
            div.top = top + 'px';
            div.left = left + 'px';
        }
            else { // IE 4
            div.top = top + 'px';
            div.left = left + 'px';
        }
        }    
    }
}

//Additional service functionality. Use to hide shown additional service panel on scroll and open new panel.
var divSrvShownId; //id of shown additional service panel.
function hideSrvShown(){
    if (divSrvShownId != null)
        {
            hideDiv(divSrvShownId);
            divSrvShownId = null;   
        }
}

//infoWindow. iEnqType: 1 - Booking, 2 - Quote, 3 - Skype, 4 - SMS, 5 - Special1, 6 - Special2, 7 - Special3, 8 - Photo, 9 - SponsorAdd
//onclick=document.getElementById("frFace").src="../util/ServiceQuote.aspx?provid=2612&makeid=3&stype=1";jQuery.facebox({div:"#divIFrame"});
function showEnquiry(iEnqType, iProvID, iMakeID, iSType){
    var fr = document.getElementById("frFace");
    var div = document.getElementById("divIFrame");
    if (fr && div){
        switch(iEnqType)
        {
        case 1:
          fr.src = '/util/ServiceBooking.aspx?provid='+iProvID+'&makeid='+iMakeID+'&stype='+iSType;
          fr.style.width = '850px';
          fr.style.height = '400px';
          break;
        case 2:
          fr.src = '/util/ServiceQuote.aspx?provid='+iProvID+'&makeid='+iMakeID+'&stype='+iSType;
          fr.style.width = '400px';
          fr.style.height = '370px';
          break;
        case 3:
          fr.src = '/util/Skype.aspx?provid='+iProvID;
          fr.style.width = '600px';
          fr.style.height = '400px';
          break;
        case 4:
          fr.src = '/util/SMS.aspx?provid='+iProvID+'&makeid='+iMakeID+'&stype='+iSType;
          fr.style.width = '380px';
          fr.style.height = '300px';
          break;
        case 5:
          fr.src = '';
          break;  
        }
        jQuery.facebox({div:"#divIFrame"});
    }
}

/*************** Client-side access to querystring name=value pairs ***************/
/* USAGE
// Parse the current page's querystring
var qs = new Querystring()
// Parse a given querystring
var qs2 = new Querystring("name1=value1&name2=value2")

var v1 = qs2.get("name1")
var v3 = qs2.get("name3", "default value")
if (qs2.contains("name1")){ alert(qs2.get("name1"));}
*/
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = {};
	
	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);
		
		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;
		
		this.params[name] = value;
	}
}

Querystring.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}


//***Cross browser attach event function. For 'evt' pass a string value with the leading "on" omitted
//***e.g. AttachEvent(window,'load',MyFunctionNameWithoutParenthesis,false);
function AttachEvent(obj,evt,fnc,useCapture){
	if (!useCapture) useCapture=false;
	if (obj.addEventListener){
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	} else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
	else{
		MyAttachEvent(obj,evt,fnc);
		obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
	}
} 