
//
// General utility functions
//
//
//

//
// Constructs the current datetime in the format we need
//
function currentDate4Db() {
	var m_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
	var d = new Date();
	var curr_date = d.getDate();
	var curr_month = d.getMonth();
	var curr_year = d.getFullYear();
	var curr_hour = d.getHours();
	var curr_min = d.getMinutes();
	var finalDate = curr_date + " " + m_names[curr_month] + " " + curr_year + " " + curr_hour + ":" + curr_min;
	return finalDate;
}

//
// Constructs the datetime object into a parseable string for server side.
//
function getDate4Db(datetimeObject) {

	if (datetimeObject == null)
	{
		return null;
	}
	var m_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

	var curr_date = datetimeObject.getDate();
	var curr_month = datetimeObject.getMonth();
	var curr_year = datetimeObject.getFullYear();
	var curr_hour = datetimeObject.getHours();
	var curr_min = datetimeObject.getMinutes();
	var finalDate = curr_date + " " + m_names[curr_month] + " " + curr_year + " " + curr_hour + ":" + curr_min;
	return finalDate;
}


//
// Pass in datetime object to get a yyyyMMdd string.
//
function getDateAsyyyyMMdd(datetimeObject, dateSeparator) {

    var curr_date = datetimeObject.getDate();
    var curr_month = datetimeObject.getMonth() + 1; //months are zero based
    var curr_year = datetimeObject.getFullYear();
    var addMonthZero = '';
    if (curr_month < 10) addMonthZero = '0';
    var addDateZero = '';
    if (curr_date < 10) addDateZero = '0';
    return curr_year + dateSeparator + addMonthZero + curr_month + dateSeparator + addDateZero + curr_date;  //'20100215';
}

//
// when the file is upload delete the input box.
//
function fileUploaded(sender, args) {
    $find('RadAjaxManager1').ajaxRequest();
    $telerik.$(".invalid").html("");
    //setTimeout(function () { sender.deleteFileInputAt(0); }, 10);
}

//
// validates the files in a asyncupload control
//
function AsyncUpload_ValidationFailed(sender, args) {
    $telerik.$(".invalid")
        .html("Invalid extension, please choose a picture file");
    sender.deleteFileInputAt(0);
}

//
// Used by jquery ajax requests in the error function
//
function findWhatError(x, e)
{
	var result = 'Unknown Error.\n' + x.responseText;
	if(x.status==0) {
		result = 'You are offline!!\n Please Check Your Network.';
	} else if(x.status==404) {
		result = 'Requested URL not found.';
	} else if(x.status==500) {
		result = 'Internal Server Error.';
	} else if(e=='parsererror') {
		result = 'Error.\nParsing JSON Request failed.';
	} else if(e=='timeout') {
		result = 'Request Time out.';
	}
	return result;
}


// find a starting point for web calls
function findRelativeBasePath() {
    var newlocation = '';
    // Cheap check to see where we are..
    var currentPage = window.location.pathname.split('/');
    // if we are in dev mode and in gamescollection for example test for path greater than 3
    // if we are in gamescollection on live, length will be 1 minus this.
    if ((currentPage[1].indexOf(".") > -1 && currentPage.length > 3) ||
        (currentPage[1].indexOf(".") == -1 && currentPage.length > 2)) {
        newlocation = '../';
    }
    return newlocation;
}

//
// Generic function to call webmethod or webservices
//
function ajaxServiceCall(serviceMethodCall, paramsObject, contentFormat, alertRawResult)
{
	var result = null;
	var params = null;

	if (paramsObject != null)
	{
		params = JSON.stringify(paramsObject);
	}

	// call back to web page method
	$.ajax({
		type: "POST",
		url: findRelativeBasePath() + serviceMethodCall,
		contentType: "application/json; charset=utf-8",
		data: params,
		timeout : 8000,
		cache: false,
		dataType: contentFormat,
		async: false,
		success: function (data) {
			if (data != null)
			{
				if (contentFormat == "xml")
				{
					result = data;
				}
				else
				{
					result = data.d;
					if (alertRawResult) alert(result);
				}
			}
		},
		error: function (xhr, status, error) {
			// Don't highlight error in production
			if (alertRawResult) alert(findWhatError(xhr, status) + " - method call " + serviceMethodCall + " Failed" + error);
		}
	});
	return result;
}

//
// Check the captcha
//
function IsCaptchaValid(userCaptcha)
{
	var captchaParams = {};
	captchaParams['userCaptcha'] = userCaptcha;

	var result = ajaxServiceCall("webservices/MonadelAccess.asmx/IsCaptchaValid", captchaParams, "json", false);
	if (result==true) 
	{
		return 1;
	}
	return 0;
}


//
// Login
//
function Login(username, password)
{
	var loginParams = {};
	loginParams['user'] = username;
	loginParams['password'] = password;

	// call back to web page method
	var result = ajaxServiceCall("webservices/MonadelAccess.asmx/Login", loginParams, "json", false);
	if (result==true)
	{
		return 1;
	}
	return 0;
}

//
// Logout
//
function Logout(username, password)
{
	// call back to web page method
	var result = ajaxServiceCall("webservices/MonadelAccess.asmx/Logout", null, "json", false);
	if (result==true)
	{
		return 1;
	}
	return 0;
}












