
//====================================================================================================
//	File Name		:	functions.js
//----------------------------------------------------------------------------------------------------
//	Purpose			:	Javascript Utility functions
//====================================================================================================
var ie4=document.all&&navigator.userAgent.indexOf("Opera")==-1;
var ns6=document.getElementById&&navigator.userAgent.indexOf("Opera")==-1;
var ns4=document.layers;

//====================================================================================================
//	Function Name	:	popupWindowURL
//	Purpose			:	Whenever you wanna open a link into a new window just call this function
//								you need to pass some arguemnts as described below.
//	Parameters		:
//								url  = url to be open in the new window
//								winname = winname is the window name for the reference of that window
//								w is the width
//								h is the height
//								menu is the parameter, if you want menubar to be enabled on the window
//								resize if you wanna resize the window
//								scroll i fyou needed
//	Return			:	true or false
//----------------------------------------------------------------------------------------------------

function popupWindowURL(url, winname,  w, h, menu, resize, scroll) {

    var x = (screen.width-w)/2;
    var y = (screen.height-h)/3;

	if (winname == null) winname = "newWindow";
	if (w == null) w = 800;
	if (h == null) h = 600;
	if (resize == null) resize = 1;

	menutype   = "nomenubar";
	resizetype = "noresizable";
	scrolltype = "noscrollbars";
	if (menu) menutype = "menubar";
	if (resize) resizetype = "resizable";
	if (scroll) scrolltype = "scrollbars";
	cwin=window.open(url,winname,"top=" + y + ",left=" + x + ",screenX=" + x + ",screenY=" + y + "," + "status," + menutype + "," + scrolltype + "," + resizetype + ",width=" + w + ",height=" + h);

	if (!cwin.opener) cwin.opener=self;
	cwin.focus();

	return true;
}

//====================================================================================================
//	Function Name	:	sprintf
//	Purpose			:	Provide the functionality of sprintf
//	Parameters		:		
//	Return			:	true or false
//----------------------------------------------------------------------------------------------------
function sprintf()
{
    if (!arguments || arguments.length < 1 || !RegExp)
    {
        return;
    }
    var str = arguments[0];
    var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
    var a = b = [], numSubstitutions = 0, numMatches = 0;
    while (a = re.exec(str))
    {
        var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
        var pPrecision = a[5], pType = a[6], rightPart = a[7];

        //alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);

        numMatches++;
        if (pType == '%')
        {
            subst = '%';
        }
        else
        {
            numSubstitutions++;
            if (numSubstitutions >= arguments.length)
            {
                alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
            }
            var param = arguments[numSubstitutions];
            var pad = '';
                   if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
              else if (pPad) pad = pPad;
            var justifyRight = true;
                   if (pJustify && pJustify === "-") justifyRight = false;
            var minLength = -1;
                   if (pMinLength) minLength = parseInt(pMinLength);
            var precision = -1;
                   if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
            var subst = param;
                   if (pType == 'b') subst = parseInt(param).toString(2);
              else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
              else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
              else if (pType == 'u') subst = Math.abs(param);
              else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
              else if (pType == 'o') subst = parseInt(param).toString(8);
              else if (pType == 's') subst = param;
              else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
              else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
        }
        str = leftpart + subst + rightPart;
    }
    return str;
}

//====================================================================================================
//	Function Name	:	Record_Focus
//	Purpose			:	Provide the functionality for record selection
//	Parameters		:	mouseState		-	State of the mouse	
//						rowLostFocus	-	Class name to be use when row lost focus
//						rowGetFocus		-	Class name to be use when row get focus
//						rowSelected		-	Class name to be use when row get selectd
//						curRow			-	Id of current row
//						lastSelection	-	Id of last selection
//	Return			:	None
//----------------------------------------------------------------------------------------------------
function Record_Focus(mouseState, rowLostFocus, rowGetFocus, rowSelected, curRow, lastSelection)
{
	switch(mouseState)
    {
    	case 0:		// Row Get Focus
        	if(curRow.className != rowSelected)
				curRow.className = rowGetFocus;
        	break;
        case 1:		// Row Lost Focus
        	if(curRow.className != rowSelected)
				curRow.className = rowLostFocus;
        	break;
        case 2:		// Row Get clicked
        	if(curRow.className != rowSelected)
            {
				curRow.className = rowSelected;
                if(lastSelection.value)
		            document.getElementById(lastSelection.value).className = rowLostFocus;

	            lastSelection.value = curRow.id;
            }
            else
			{
	            curRow.className = rowGetFocus;
                if(lastSelection.value)
		            lastSelection.value = '';
			}				

        	break;
    }
}

//====================================================================================================
//	Function Name	:	Record_Focus
//	Purpose			:	Provide the functionality for record selection
//	Parameters		:	mouseState		-	State of the mouse	
//						rowLostFocus	-	Class name to be use when row lost focus
//						rowGetFocus		-	Class name to be use when row get focus
//						rowSelected		-	Class name to be use when row get selectd
//						curRow			-	Id of current row
//						lastSelection	-	Id of last selection
//	Return			:	None
//----------------------------------------------------------------------------------------------------

/////change statelist function by selecting country

function change_state_list(frm,lstcountry,stateSel,state_combo_name)
{
	cat = lstcountry.options[lstcountry.selectedIndex].value;
	with(frm)
	{
		for(i=0; i<arrState.length; i++)
		{
			if(arrState[i][0] == cat)	break;
		}
	
		if(i!=arrState.length)
		{
			state_combo_name.disabled=false;
			state_combo_name.length = 1;
			for(j=1; j<arrState[i].length; j+=3)
			{
				state_combo_name.length++;
				state_combo_name[state_combo_name.length-1].value = arrState[i][j];
				state_combo_name[state_combo_name.length-1].text = arrState[i][j+1];
				if(stateSel == arrState[i][j])
				{
					state_combo_name[state_combo_name.length-1].selected = true;
				}
			}
	
		}
		else
		{
			state_combo_name.length = 1;
			state_combo_name.disabled=true;
		}
		
	}
}

//====================================================================================================
//	Function Name	:	CheckUncheck_Click
//	Purpose			:	Provide the functionality Check OR UnCheck the Check Box
//	Parameters		:	fld	- [ THE CHECKBOX NAME ]
//						status - [CHECK | UNCHECK]
//	Return			:	None
//----------------------------------------------------------------------------------------------------

function CheckUncheck_Click(fld, status)
{
	if(fld)
	{
		if(fld.length)
			for(i=0; i < fld.length; i++)
				fld[i].checked = status;
		else
				fld.checked = status;
	}
}


function CheckUncheck_Click1(fld, len, status)
{
	if(fld)
	{
		if(len)
			for(i=0; i < len; i++)
				fld[i].checked = status;
		else
			fld.checked = status;
	}
}

//====================================================================================================
//	Function Name	:	Location drop down functions list
//	Purpose			:	Provide the functionality Check OR UnCheck the Check Box
//	Parameters		:	fld	- [ THE CHECKBOX NAME ]
//						status - [CHECK | UNCHECK]
//	Return			:	None
//----------------------------------------------------------------------------------------------------

function change_city_list(lststate)
{
	
	with(document.frmClubLink)
	{
		cat = lststate.options[lststate.selectedIndex].value;
		submit();
	}
}


function DeleteChecked_Click(frm,fieldname,actType)
{
	with(frm)
	{
		var flg=false;

		if(document.all[fieldname])
		{
			if(document.all[fieldname].length)
			{
				for(i=0; i < document.all[fieldname].length; i++)
				{
					if(document.all[fieldname][i].checked)
						flg = true;
				}
				if(!flg)
				{
					alert('Please Select Records First');
					return false;
				}
			}
			else
			{
				if(!document.all[fieldname].checked)
				{
					alert('Please Select Records First');
					return false;
				}
			}
				
			if(confirm('Are You Sure Want To Delete All Selected Records ?'))
			{
				Action.value = actType;
				submit();
			}
		}
		return false;
	}
}

/* location change functions*/
function Country_Change(frm,stateSel)
{
	with(frm)
	{
		window.frames['dataframe'].window.location.replace( Site_URL + 'location.php?action=loader&country_id=' + frm.country_id.value + '&stateSel='+stateSel ); 
		state_id.length = 1;
		state_id[state_id.length-1].text = '---- Loading Data -----';
	}
}

function Fill_State_Data(arrState,stateSel)
{
	with(document.all)
	{
		if(arrState.length > 0)
		{
			state_id.length 			= 1;
			state_id.disabled			= false;
			state_id.style.display 		= "block";
			text_state_id.style.display = "none";
			
			//city_id.disabled			= true;
			//city_id.length 				= 1;
			//city_id[city_id.length-1].text = '---- Select City -----';
			//city_id.style.display 		= "block";
			//text_city_id.style.display 	= "none";
			city_id.style.display 	= "none";
			text_city_id.style.display 	= "block";
			
			
			for(i=0; i < arrState.length; i++)
			{
				state_id.length++;
				state_id[state_id.length-1].value 	= arrState[i][0];
				state_id[state_id.length-1].text 	= arrState[i][1];
				
				///for default state selection
				if(stateSel == arrState[i][0])
				{
					state_id[state_id.length-1].selected = true;
				}
			}

			state_id[0].text = '----- Select State -----';
		}
		else
		{
			state_id.disabled			= true;
			city_id.disabled			= true;
			state_id.style.display 		= "none";
			text_state_id.style.display = "block";
			city_id.style.display 		= "none";
			text_city_id.style.display 	= "block";
		}
	}
	
}

function State_Change(frm,citySel)
{
	with(frm)
	{
			window.frames['dataframe'].window.location.replace( Site_URL + 'location.php?action=loader&country_id=' + frm.country_id.value + '&state_id=' + frm.state_id.value + '&citySel='+citySel ); 
			//city_id.length = 1;
			//city_id[city_id.length-1].text = '---- Loading Data -----';
			
			//text_city_id.text = '';
			
	}
}

function Fill_City_Data(arrCity,citySel)
{
	with(document.all)
	{
		//alert(arrCity.length);
		if(arrCity.length > 0)
		{
			city_id.disabled=false;
			city_id.length 				= 1;
			city_id.style.display 		= "block";
			text_city_id.style.display 	= "none";
		
			for(i=0; i < arrCity.length; i++)
			{
				city_id.length++;
				city_id[city_id.length-1].value = arrCity[i][0];
				city_id[city_id.length-1].text = arrCity[i][1];
				
				///for default state selection
				if(citySel == arrCity[i][0])
				{
					city_id[city_id.length-1].selected = true;
				}
			}
			city_id[0].text = '----- Select City -----';
		}
		else
		{
			//city_id.disabled=true;
			city_id.style.display 	= "none";
			text_city_id.style.display 	= "block";
		}
	}
}

function Get_City_Combo(citySel,frm)
{
	with(document.all)
	{
		if(state_id.disabled==false)
		{
			State_Change(frm,citySel);//state change	
			clearInterval(timer)
		}
	}
}
//////



function ComboBox_Change(frm, fld)
{
	

	with(frm)
	{
		if(fld.name == 'country_id')
		{
			window.frames['dataframe'].window.location.replace('../../location.php?action=loader&' + fld.name + '=' + fld.value+'&flag=1');
			state_id.length = 1;
			state_id[state_id.length-1].text = '---- Loading Data -----';
		}
		else if(fld.name == 'state_id')
		{
			window.frames['dataframe'].window.location.replace('../../location.php?action=loader&' + fld.name + '=' + fld.value+'&flag=1');
			city_id.length = 1;
			city_id[city_id.length-1].text = '---- Loading Data -----';
		}
		else
		{
			// Do nothing
		}
	}
}

function SetTime()
{
	if(!document.getElementById('timeId'))	return;

	var Hours;
	var Mins;
	var Time;

	Stamp = new Date();

	Hours = Stamp.getHours();
	
	if (Hours >= 12)
		Time = " PM";
	else
		Time = " AM";
	
	if (Hours > 12)
		Hours -= 12;
	
	if (Hours == 0)
		Hours = 12;
	
	Mins = Stamp.getMinutes();

	if (Mins < 10)
		Mins = "0" + Mins;

	Sec = Stamp.getSeconds();
	if (Sec < 10)
		Sec = "0" + Sec;

	document.getElementById('timeId').innerHTML = ("&nbsp;" + Hours + ":" + Mins + ":" + Sec + Time);
}

setInterval('SetTime()',1000);

function getDate(parmDate)
{
	var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September","October", "November", "December");

	var curr_date = parmDate.getDate();

	var sup = "";

	if (curr_date == 1 || curr_date == 21 || curr_date ==31)
	{
	   sup = "st";
	}
	else if (curr_date == 2 || curr_date == 22)
	{
	   sup = "nd";
	}
	else if (curr_date == 3 || curr_date == 23)
	{
	   sup = "rd";
	}
	else
	{
	   sup = "th";
	}

	var curr_month 	= parmDate.getMonth();
	var curr_year 	= parmDate.getFullYear();

	return (curr_date + "<SUP>" + sup + "</SUP> " + m_names[curr_month] + " " + curr_year);
}	