/** ******************************************************************************** **/
/** JS Functions                                                                     **/
/** -------------------------------------------------------------------------------- **/
/** A collection of Javascript functions to be used throughout the web application.  **/
/** ******************************************************************************** **/

// Detects the resolution of the screen and the viewport height and width.
// This function is used to fill in hidden fields during the login process.
// The information is later used to center popup windows.
function getResolution() {
	var vp_height = document.getElementById("vp_height");
	var vp_width = document.getElementById("vp_width");
	
	if (typeof window.innerWidth != 'undefined') {
		var viewportheight = window.innerHeight;
	} else {
		var viewportheight = document.documentElement.clientHeight;
	}

	if (typeof window.innerWidth != 'undefined') {
		var viewportwidth = window.innerWidth;
	} else {
		var viewportwidth = document.documentElement.clientWidth;
	}
	
	vp_height.value = viewportheight;
	vp_width.value = viewportwidth;

	var resEleW = document.getElementById("RES_W");
	var resEleH = document.getElementById("RES_H");
	resEleW.value = screen.width;
	resEleH.value = screen.height;
	
	return;
}

// Asks the user if they are sure they wish to perform the action requested
function areYouSure(item) {
    var choice;
    var message;
    if(item == "reset") {
        message = "Are you sure you wish to reset?";
    } else if(item == "default") {
        message = "Are you sure?";
    } else if(item == "album") {
        message = "Are you sure you wish to delete this album?\n\n**ALL IMAGES IN THIS ALBUM WILL BE DELETED**";
    } else {
        message = "Are you sure you wish to delete: "+item+"?";
    }
    
    choice = confirm(message)
    if(choice == true) {
        return true;
    } else {
        return false;
    }
}

// Detects if a character is a number or not. Returns True or False
function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
    return false;
    
    return true;
}

function update_pwd_admin(new_pwd_id,con_pwd_id,user_id){
    var new_pwd = document.getElementById(new_pwd_id).value;
    var con_pwd = document.getElementById(con_pwd_id).value;
    var user_id = document.getElementById(user_id).value;
    
    var openURL = "includes/update_pwd.php?user_id="+user_id+"&new_pwd="+new_pwd+"&con_pwd="+con_pwd;
    
    if(window.XMLHttpRequest){
        // IE7+, FF, Chrome, Opera and Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // IE5 and IE6
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
            document.getElementById("pwd_output").innerHTML = xmlhttp.responseText; // Put the result back to the page
        }
    }
    
    xmlhttp.open("GET",openURL,true);
    xmlhttp.send();
}

function reset_pwd_block_admin() {
    var inner_code = "";

    inner_code = inner_code + "<div align=\"center\">";
    inner_code = inner_code + "<table cellpadding=\"0\" cellspacing=\"3\" border=\"0\">";
    inner_code = inner_code + "    <tr>";
    inner_code = inner_code + "        <td align=\"left\"><span style=\"font-weight: bold; font-size: 8pt;\">New Password:</span><br /><input type=\"password\" name=\"new_pwd\" id=\"new_pwd\" maxlength=\"15\" style=\"width: 150px;\" /></td>";
    inner_code = inner_code + "    </tr>";
    inner_code = inner_code + "    <tr>";
    inner_code = inner_code + "        <td align=\"left\"><span style=\"font-weight: bold; font-size: 8pt;\">Confirm Password:</span><br /><input type=\"password\" name=\"con_pwd\" id=\"con_pwd\" maxlength=\"15\" style=\"width: 150px;\" /></td>";
    inner_code = inner_code + "    </tr>";
    inner_code = inner_code + "    <tr>";
    inner_code = inner_code + "        <td align=\"center\"><input type=\"button\" value=\"  Update  \" onclick=\"update_pwd_admin('new_pwd','con_pwd','user_id');\" />&nbsp;<input type=\"button\" onclick=\"popup('popUpDiv',300,200);\" value=\"  Cancel  \" /></td>";
    inner_code = inner_code + "    </tr>";
    inner_code = inner_code + "</table>";
    inner_code = inner_code + "</div>";

    document.getElementById("pwd_output").innerHTML = inner_code;
    popup('popUpDiv',300,200);
}

function reset_pwd_block() {
    var inner_code = "";

    inner_code = inner_code + "<div align=\"center\">";
    inner_code = inner_code + "<table cellpadding=\"0\" cellspacing=\"3\" border=\"0\">";
    inner_code = inner_code + "    <tr>";
    inner_code = inner_code + "        <td align=\"left\"><span style=\"font-weight: bold; font-size: 8pt;\">Current Password:</span><br /><input type=\"password\" name=\"cur_pwd\" id=\"cur_pwd\" maxlength=\"25\" style=\"width: 150px;\" /></td>";
    inner_code = inner_code + "    </tr>";
    inner_code = inner_code + "    <tr>";
    inner_code = inner_code + "        <td align=\"left\"><span style=\"font-weight: bold; font-size: 8pt;\">New Password:</span><br /><input type=\"password\" name=\"new_pwd\" id=\"new_pwd\" maxlength=\"25\" style=\"width: 150px;\" /></td>";
    inner_code = inner_code + "    </tr>";
    inner_code = inner_code + "    <tr>";
    inner_code = inner_code + "        <td align=\"left\"><span style=\"font-weight: bold; font-size: 8pt;\">Confirm Password:</span><br /><input type=\"password\" name=\"con_pwd\" id=\"con_pwd\" maxlength=\"25\" style=\"width: 150px;\" /></td>";
    inner_code = inner_code + "    </tr>";
    inner_code = inner_code + "    <tr>";
    inner_code = inner_code + "        <td align=\"center\"><input type=\"button\" value=\"Update\" onclick=\"update_pwd('cur_pwd','new_pwd','con_pwd');\" />&nbsp;<input type=\"button\" onclick=\"popup('popUpDiv',300,200);\" value=\"Cancel\" /></td>";
    inner_code = inner_code + "    </tr>";
    inner_code = inner_code + "</table>";
    inner_code = inner_code + "</div>";

    document.getElementById("pwd_output").innerHTML = inner_code;
    popup('popUpDiv',300,200);
}

function update_pwd(cur_pwd_id,new_pwd_id,con_pwd_id){
    var cur_pwd = document.getElementById(cur_pwd_id).value;
    var new_pwd = document.getElementById(new_pwd_id).value;
    var con_pwd = document.getElementById(con_pwd_id).value;

	var openURL = "nx-app/includes/aj_update_pwd.php?cur_pwd="+cur_pwd+"&new_pwd="+new_pwd+"&con_pwd="+con_pwd;

    if(window.XMLHttpRequest){
        // IE7+, FF, Chrome, Opera and Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // IE5 and IE6
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
            document.getElementById("pwd_output").innerHTML = xmlhttp.responseText; // Put the result back to the page
        }
    }

	xmlhttp.open("GET", openURL, true);
	xmlhttp.send(null);
}

function reset_email_block(new_email) {
    var email_block = document.getElementById("email_block");
    var inner_code = "";

    if(new_email.length > 0) { email_block.innerHTML = new_email; }

    inner_code = inner_code + "<div align=\"center\">";
    inner_code = inner_code + "<table cellpadding=\"0\" cellspacing=\"3\" border=\"0\">";
    inner_code = inner_code + "    <tr>";
    inner_code = inner_code + "        <td align=\"left\"><span style=\"font-weight: bold; font-size: 8pt;\">New Email:</span><br /><input type=\"text\" name=\"new_email\" id=\"new_email\" maxlength=\"80\" style=\"width: 150px;\" /></td>";
    inner_code = inner_code + "    </tr>";
    inner_code = inner_code + "    <tr>";
    inner_code = inner_code + "        <td align=\"center\"><input type=\"button\" value=\"  Update  \" onclick=\"update_email('new_email');\" />&nbsp;<input type=\"button\" onclick=\"popup('popUpDiv2',300,200);\" value=\"  Cancel  \" /></td>";
    inner_code = inner_code + "    </tr>";
    inner_code = inner_code + "</table>";
    inner_code = inner_code + "</div>";

    document.getElementById("email_output").innerHTML = inner_code;
    popup('popUpDiv2',300,200);
}

function update_email(email){
    var new_email = document.getElementById(email).value;
	var openURL = "nx-app/includes/update_email.php?email="+new_email;

    if(window.XMLHttpRequest){
        // IE7+, FF, Chrome, Opera and Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // IE5 and IE6
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
            document.getElementById("email_output").innerHTML = xmlhttp.responseText; // Put the result back to the page
        }
    }

	xmlhttp.open("GET", openURL, true);
	xmlhttp.send(null);
}

function toggle(div_id) {
	var el = document.getElementById(div_id);
	if( el.style.display == 'none' ) {
		el.style.display = 'block';
	} else {
		el.style.display = 'none';
	}
}

function blanket_size(popUpDivVar,size_x) {
	var half_size = size_x / 2;
	
	if(typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if(document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	var blanket = document.getElementById('blanket');
	blanket.style.height = blanket_height + 'px';
	var popUpDiv = document.getElementById(popUpDivVar);
	popUpDiv_height=blanket_height/2-half_size;//150 is half popup's height
	popUpDiv.style.top = popUpDiv_height + 'px';
}

function window_pos(popUpDivVar,size_x) {
	var half_size = size_x / 2;
	
	if(typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if(document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	var popUpDiv = document.getElementById(popUpDivVar);
	window_width=window_width/2-half_size;//150 is half popup's width
	popUpDiv.style.left = window_width + 'px';
}

function popup(windowname,size_x,size_y) {
	var div_obj = document.getElementById(windowname);
	div_obj.style.width = size_x + 'px';
	div_obj.style.height = size_y + 'px';
	
	blanket_size(windowname,size_x);
	window_pos(windowname,size_x);
	toggle('blanket');
	toggle(windowname);		
}

function check_user(objUser){
    var check_user;
    var user_id = objUser.value;
    
    var openURL = "includes/check_user.php?user_id="+user_id;
    
    if(window.XMLHttpRequest){
        // IE7+, FF, Chrome, Opera and Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // IE5 and IE6
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
            if(xmlhttp.responseText == "Y"){
                var user_check = "Y";
                var user_output = "<span style='color: #008100; font-family: arial; font-weight: bold; font-size: 10pt;'>&nbsp;&nbsp;Available</span>";
            } else {
                var user_check = "N";
                var user_output = "<span style='color: #C70000; font-family: arial; font-weight: bold; font-size: 10pt;'>&nbsp;&nbsp;Not Available</span>";
            }
            
            document.getElementById("user_check").value = user_check;
            document.getElementById("user_output").innerHTML = user_output;
        }
    }
    
    xmlhttp.open("GET",openURL,true);
    xmlhttp.send();
}

function add_to_cart(id) {
    var qtyObj = document.getElementById("qty"+id);
    var qty = qtyObj.value;
    var openURL = "nx-app/includes/add_to_cart.php?id="+id+"&qty="+qty;
    var item_addedObj = document.getElementById("item_added");
    
    qtyObj.value = "1";

    if(window.XMLHttpRequest) {
        // IE7+, FF, Chrome, Opera and Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // IE5 and IE6
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("cart_summary").innerHTML = xmlhttp.responseText; // Put the result back to the page
            if(qty > 1) {
                item_addedObj.innerHTML = "Items Added";
            } else {
                item_addedObj.innerHTML = "Item Added";
            }
            $('#item_added').fadeIn(500, function(){}).delay(2500).fadeOut(500, function(){});
        }
    }
    
    xmlhttp.open("GET",openURL,true);
    xmlhttp.send();
}

function clear_cart(item) {
    var choice;
    var message;
    message = "Are you sure you wish to clear all cart contents?";
    
    choice = confirm(message)
    if(choice == true) {
        window.location='cart.php?action=clear';
        return true;
    } else {
        return false;
    }
}

function check_email(objEmail){
    var check_email;
    var email = objEmail.value;
    
    var openURL = "nx-app/includes/check_email.php?email="+email;
    
    if(window.XMLHttpRequest){
        // IE7+, FF, Chrome, Opera and Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // IE5 and IE6
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
            if(xmlhttp.responseText == "Y"){
                var email_check = "Y";
                var email_output = "<br /><span style='color: #008100; font-family: arial; font-weight: bold; font-size: 10pt;'>Email Address OK.</span>";
            } else if(xmlhttp.responseText == "N") {
                var email_check = "N";
                var email_output = "<br /><span style='color: #C70000; font-family: arial; font-weight: bold; font-size: 10pt;'>Email Address Already in Use.</span>";
            } else {
                var email_check = "N";
                var email_output = "<br /><span style='color: #C70000; font-family: arial; font-weight: bold; font-size: 10pt;'>Email Address is Invalid.</span>";
            }
            
            document.getElementById("email_check").value = email_check;
            document.getElementById("email_output").innerHTML = email_output;
        }
    }
    
    xmlhttp.open("GET",openURL,true);
    xmlhttp.send();
}

function load_cart() {
    var choice;
    var message;

    message = "Are you sure you wish to load this saved cart? Any items that are currently in your cart will be lost.";
    
    choice = confirm(message)
    if(choice == true) {
        return true;
    } else {
        return false;
    }
    
}

function product_search(search_id){
    var search_string = document.getElementById(search_id).value;
    
    var openURL = "nx-app/includes/set_search.php?s="+search_string;
    
    if(window.XMLHttpRequest){
        // IE7+, FF, Chrome, Opera and Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // IE5 and IE6
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
            var set_result = xmlhttp.responseText;
            if(set_result == "true") {
                window.location = "search.php";
                return true;
            } else {
                alert("Search Error!");
                return false;
            }
        }
    }
    
    xmlhttp.open("GET",openURL,true);
    xmlhttp.send();
}

function set_search_box(action, eleObj, evt) {
    switch(action) {
        case "focus":
            if(eleObj.value == "Search Products") {
                eleObj.value = "";
            }
            eleObj.style.color = "#000000";
            eleObj.style.textAlign = "left";
            var charCode = (evt.which) ? evt.which : event.keyCode
            if(charCode == 13){ product_search("search_string"); }
            break;
            
        case "blur":
            if(eleObj.value == "") {
                eleObj.value = "Search Products";
                eleObj.style.color = "#aaaaaa";
                eleObj.style.textAlign = "center";
            }
            break;
        
        default:
            break;
    }
}

function load_brand(brand_id){
    var openURL = "/app/includes/load_brand.php?id="+brand_id;

    if(window.XMLHttpRequest){
        // IE7+, FF, Chrome, Opera and Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // IE5 and IE6
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
            document.getElementById("item_content").innerHTML = xmlhttp.responseText; // Put the result back to the page
        }
    }
    
    xmlhttp.open("GET",openURL,true);
    xmlhttp.send();
}

