/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}

var http = createObject();

/* -------------------------- */
/* LOGIN */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;
function login() {
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
var user_email = encodeURI(document.getElementById('user_email').value);
var user_password = encodeURI(document.getElementById('user_password').value);
var login_user = encodeURI(document.getElementById('login_user').value);
// Set te random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
http.open('get', '/includes/users/query_login.php?user_email='+user_email+'&user_password='+user_password+'&login_user='+login_user+'&nocache='+nocache);
http.onreadystatechange = loginReply;
http.send(null);
}

function loginReply() {
if(http.readyState == 4){
var response = http.responseText;
if(response == 0){
// if login fails
document.getElementById('login_error').innerHTML = 'Invalid Information';
// else if login is ok show a message: "Welcome + the user name".
} else {
populate_login_options();
location.reload(true);
}
}
}

/* -------------------------- */
/* POPULATE LOGIN MENU */
/* -------------------------- */
function populate_login_options() {
// Set te random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
http.open('get', '/includes/media_page/user_login2.php');
http.onreadystatechange = login_optionsReply;
http.send(null);
}

function login_optionsReply() {
if(http.readyState == 4){
var response = http.responseText;
document.getElementById('login_options').innerHTML = response;
}}

/* -------------------------- */
/* POPULATE PHOTOS */
/* -------------------------- */
function populate_photos(page_id) {
// Optional: Show a waiting message in the layer with ID ajax_response
document.getElementById('photos').innerHTML = ""
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
//var album_id_view = encodeURI(document.getElementById('album_id_view').value);
var page_id = page_id;
if(page_id == null || page_id < 1){ page_id = 1;}
// Set te random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
//if(album_id_view > 0){
//document.getElementById('album_option').innerHTML = "[<a href=\"#\" onclick=\"decision('Are you sure you want to remove this Album?','http://admin.trendpimp.com/includes/users/query/query_album_delete.php?aid="+album_id_view+"')\">Remove Album</a>]";
//}else{
//document.getElementById('album_option').innerHTML = '';
//}
http.open('get', '/includes/users/query/query_photos_populate.php?&page='+page_id+'&nocache = '+nocache);
//http.open('get', '/includes/users/query/query_photos_populate.php?album_id_view='+album_id_view+'&page='+page_id+'&nocache = '+nocache);
http.onreadystatechange = populate_photoReply;
http.send(null);
}

function populate_photoReply() {
if(http.readyState == 4){
var response = http.responseText;
if(response == 0){
// if fails
document.getElementById('photos').innerHTML = 'Populate failed. Please Refresh Page';
// else if login is ok show a message: "Welcome + the user name".
} else {
document.getElementById('photos').innerHTML = response;
}
}
}

/* -------------------------- */
/* REMOVE PHOTOS */
/* -------------------------- */
function remove_photo(photo_id,album_id,pageNum) {
if(confirm('Remove Photo from album?')){
var photo_id = photo_id;
var album_id = album_id;
var pageNum = pageNum;
// Set te random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
http.open('get', '/includes/users/query/query_photo_delete.php?pid='+photo_id+'&aid='+album_id+'&page='+pageNum+'&nocache='+nocache);
http.onreadystatechange = remove_photoReply;
http.send(null);
}}

function remove_photoReply() {
if(http.readyState == 4){
var response = http.responseText;
document.getElementById('photos').innerHTML = response;
populate_photos(response);
}
}

/* -------------------------- */
/* POPULATE FAVORITES */
/* -------------------------- */
function populate_fav(page_id) {
// Optional: Show a waiting message in the layer with ID ajax_response
document.getElementById('favorites').innerHTML = ""
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
var page_id = page_id;
if(page_id == null || page_id < 1){ page_id = 1;}
// Set te random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
http.open('get', '/includes/users/query/query_favorite_populate.php?nocache = '+nocache+'&page='+page_id);
http.onreadystatechange = populate_favReply;
http.send(null);
}

function populate_favReply() {
if(http.readyState == 4){
var response = http.responseText;
if(response == 0){
// if login fails
document.getElementById('favorites').innerHTML = 'Populate failed. Please Refresh Page';
// else if login is ok show a message: "Welcome + the user name".
} else {
document.getElementById('favorites').innerHTML = response;
}
}
}

/* -------------------------- */
/* REMOVE FAVORITES */
/* -------------------------- */
function remove_fav(fav_id,pageNum) {
if(confirm('Remove this video from your favorites?')){
// Optional: Show a waiting message in the layer with ID ajax_response
//document.getElementById('framework_response').innerHTML = "Removing Favorite..."
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
var media_id = fav_id;
var pageNum = pageNum;
// Set te random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
http.open('get', '/includes/users/query/query_favorite_remove.php?media_id='+media_id+'&page='+pageNum+'&nocache = '+nocache);
http.onreadystatechange = remove_favReply;
http.send(null);
}}

function remove_favReply() {
if(http.readyState == 4){
var response = http.responseText;
//document.getElementById('framework_response').innerHTML = 'Removed '+response;
populate_fav(response);
}
}


function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}}}



/* -------------------------- */
/* CONTACT FORM */
/* -------------------------- */
var nocache = 0;
function contact_form() {
nocache = Math.random();
var form_name = encodeURI(document.getElementById('form_name').value);
var form_email = encodeURI(document.getElementById('form_email').value);
var user_name = encodeURI(document.getElementById('user_name').value);
var form_account = encodeURI(document.getElementById('form_account').value);
var subjects = encodeURI(document.getElementById('subjects').value);
var query = encodeURI(document.getElementById('query').value);
var user_os = encodeURI(document.getElementById('user_os').value);
var user_browser = encodeURI(document.getElementById('user_browser').value);
var form_message = encodeURI(document.getElementById('form_message').value);

var params = 'form_name='+form_name+'&form_email='+form_email+'&user_name=+'+user_name+'&form_account='+form_account+'&subjects='+subjects+'&query='+query+'&user_os='+user_os+'&user_browser='+user_browser+'&form_message='+form_message;


http.open('get', '/includes/contact/query_contact.php?'+params+'&nocache='+nocache);
http.onreadystatechange = contact_formReply;
http.send(null);
}


function contact_formReply() {
if(http.readyState == 4){
var response = http.responseText;
var str = response;
var pos_name = str.indexOf("error_name");
var pos_email = str.indexOf("error_email");
var pos_message = str.indexOf("error_message");
var pos = str.indexOf("no_errors");

if (pos_name>=0){document.getElementById('error_name').innerHTML = 'Please enter your name';}else{document.getElementById('error_name').innerHTML = '';}
if (pos_email>=0){document.getElementById('error_email').innerHTML = 'Enter a valid email';} else{document.getElementById('error_email').innerHTML = '';}
if (pos_message>=0){document.getElementById('error_message').innerHTML = 'Enter your message';} else{document.getElementById('error_message').innerHTML = '';}
if (pos>=0){document.getElementById('form_data').innerHTML = 'Thank you, your message has been sent to our support team.';} 
}
}