// 
// Constants
//

// For testing - clientappstest
// var loc_scripts_base = 'http://clientappstest.kickapps.com/bewell/php';

// Production
var loc_scripts_base = 'http://clientapps.kickapps.com/bewell/php';

// Check box handler code

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if (!radioObj)
		return "";
	var radioLength = radioObj.length;
	if (radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for (var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

// 
// Cookie Handling Routines
//
function getCookieValue (cookieName) {
	return Ka.Cookie.read(cookieName);
}

function writeSessionCookie (cookieName, cookieValue) {
	Ka.Cookie.create(cookieName, cookieValue, 7);
}

function deleteCookie (cookieName) {
	Ka.Cookie.erase(cookieName);
}

// 
// Handle additional registration fields save
//
function cleanupCookies() {
	deleteCookie("username");
	deleteCookie("email");
	deleteCookie("zip_code");
	deleteCookie("gender");
}

// Handle the JSONRequest Response
function profResponse(responseJSONStr) {
	
	try {
		if (responseJSONStr.status == '1') {
			// Cleanup the cockies if saved
			cleanupCookies();
		}
		else {
			// alert(responseJSONStr.error);
		}
	} catch (e) {
		// Do nothing
	}

}

function saveBewellRegistration() {
	var passedParams = '';
	
	if (Ka.Info.USERID != '') {
	
		// Get the user information
		var username = Ka.Info.USERNAME;
		var userid = Ka.Info.USERID;
		var asid = Ka.Info.AFFILIATESITEID;
	
		// Read From the cookie
		var email = getCookieValue("email");
		var zip_code = getCookieValue("zip_code");
		var gender = getCookieValue("gender");
	
		// Setup the user info.
		passedParams = passedParams + '&username=' + encodeURIComponent(username);
		passedParams = passedParams + '&userid=' + encodeURIComponent(userid);
		passedParams = passedParams + '&email=' + encodeURIComponent(email);
	
		// Setup the passed parameters
		passedParams = passedParams + '&postalcode=' + encodeURIComponent(zip_code);
		passedParams = passedParams + '&gender=' + encodeURIComponent(gender);
	
		// Handle the update
		request = loc_scripts_base + '/updateUserInfoRegister.php?callback=profResponse&as=' + asid + passedParams;

		aObj = new JSONscriptRequest(request);
		aObj.buildScriptTag();
		aObj.addScriptTag();
		
	}
}

// 
// Handle the setting and saving of the registration fields
//
if ((window.location.href.indexOf('displayMyPlace') != -1) || (window.location.href.indexOf('displayKickPlace') != -1)) {
	var hasProfInfo = getCookieValue('zip_code');
	if (hasProfInfo != null && hasProfInfo != '') {
		saveBewellRegistration();
	}
}

function setProfileCookies(){
	
	// Standard Fields
	var username = document.getElementById("username").value;
	var email = document.getElementById("email").value;

	// Custom Fields
	var zip_code = document.getElementById("zip_code").value;
	var gender = document.getElementById("gender").value;

	// Write to the cookie
	writeSessionCookie('username', username);
	writeSessionCookie('email', email);
	writeSessionCookie('zip_code', zip_code);
	writeSessionCookie('gender', gender);

	// Submit the form
	document.RegisterUser.submit();
} 

function setBewellRegistration() {
	
	// Read From the cookie
	document.getElementById("username").value = getCookieValue("username");
	document.getElementById("email").value = getCookieValue("email");
	document.getElementById("zip_code").value = getCookieValue("zip_code");
	document.getElementById("gender").value = getCookieValue("gender");
	
}

// Put the cookie setting function into effect
$j(document).ready(function() {
	if (Ka.Info.PAGE == 'login/registerUser.jsp') {
		$j('#RegisterUser').attr('onvalid','setProfileCookies');

		// Check to see if was an error and refill in the fields
		// if the cookies are still there
		var hasProfInfo = getCookieValue('zip_code');
		if (hasProfInfo != null && hasProfInfo != '') {
			setBewellRegistration();
		}
	}
});

// Handle the expert setting for media

// Handle the adminTag calback
function gotIsExpert(json) {
    var adminTags = json.admintags;
	
	// Check to see if this is an Expert
	if (adminTags.indexOf("expert") != -1) {	
		// If so then set the media admin tag to expert
		var tagsElem = document.getElementById("mediaAdminTags");	
		if (tagsElem) {
			tagsElem.value = "srchexpert";
		}
	};
}

// Call to check if a user is an expert
function getIsExpert() {
	
	// Setup
	userid = Ka.Info.USERID;
	siteid = Ka.Info.AFFILIATESITEID;
	atagURL = loc_scripts_base + "/getUserAdminTags.php?as=" + siteid + "&callback=gotIsExpert&u=" + userid;
    
	// Handle the request
	aObj = new JSONscriptRequest(atagURL);
    aObj.buildScriptTag();
    aObj.addScriptTag();
}

// Media Upload checks
if ((Ka.Info.PAGE == 'pages/newVideoUpload.jsp' || Ka.Info.PAGE == 'pages/newAudioUpload.jsp' || Ka.Info.PAGE == 'pages/newPhotoUpload.jsp') || (Ka.Info.PAGE == 'pages/addBlog.jsp')) {
	// Handle comments
	$j(document).ready(function() {
		getIsExpert();
	});
}
