function validate_login(){
    var e = document.getElementById("login_loading");
    if(e){
        e.style.display = '';
        var u = document.getElementById("username").value; 
        var p = document.getElementById("password").value; 
        e.innerHTML = "<span class='login_msg'>Validating Login...</span>";
        $.ajax({
            url: 'ajax_request.php?action=validate_login&user='+u+'&pass='+p,
            success: function(data) {
                if(data=="YES"){
                    window.location.href = "create_account.php";
                }else{
                    e.innerHTML = "<span class='login_msg'><font color='red'><b>Invalid username/password!</b></font></span>";
                }
            }
        })
    }
}
function colorSelect(nColor)
{
	$('#product_color_selected').val(nColor);
}
function instant_addtocart(pid){
    $('#product_sizes_loading').html("<center><img src='images/loading.gif'></center>");
    if(document.getElementById("product_qty")){
        if(document.getElementById("product_qty").value=="" || isNaN(document.getElementById("product_qty").value)){
            alert("Please enter quantity");
        }else{
            qty = document.getElementById("product_qty").value;
        }
    }else{
        qty = 1; 
    }
    if(document.getElementById("product_size")){
        size = document.getElementById("product_size").value;
        size_arr = size.split("~");
        size_id = size_arr[0]; 
    }else{
        size_id = 0;
    }
    if(document.getElementById("product_color_selected")){
        color_id = document.getElementById("product_color_selected").value;
    }else{
        color_id = 0;
    }
    $.ajax({
        url: 'ajax_request.php?action=add_to_cart&cid='+color_id+'&sid='+size_id+'&pid='+pid+'&qty='+qty,
        success: function(data) {
            $('#cart_content').html(data);
            $('#product_sizes').html("<center><b><br/><br/>Product added successfully!</b></center>");
            $('#product_sizes_loading').html("");
            window.setTimeout(hidepopup, 3000);
            
        }
    })
}
function show_colors(colors, pid){
    $('#product_colors').html("<center><img src='images/loading.gif'></center>");
    $.ajax({
      url: 'ajax_request.php?action=show_colors&pid='+pid+'&colors='+colors,
      success: function(data) {
            $('#product_colors').html(data);
      }
    })
}
function addtocart(pid, qty){
    $.ajax({
      url: 'ajax_request.php?action=is_define_size&pid='+pid,
      success: function(data) {
    		centerPopup('product_sizes');
    		//load popup
    		loadPopup('product_sizes');
            $('#product_sizes').html("<center><img src='images/loading.gif'></center>");
            $('#product_sizes').show('slow');
            if(data=="Yes"){
                $.ajax({
                    url: 'ajax_request.php?action=product_sizes&pid='+pid,
                    success: function(data) {
                        $('#product_sizes').html(data);
                		centerPopup('product_sizes');
                		//load popup
                		loadPopup('product_sizes');
        
                    	//CLOSING POPUP
                    	//Click the x event!
                    	$("#popupContactClose").click(function(){
                    		disablePopup('product_sizes');
                    	});
                    	//Click out event!
                    	$("#backgroundPopup").click(function(){
                    		disablePopup('product_sizes');
                    	});
                    	//Press Escape event!
                    	$(document).keypress(function(e){
                    		if(e.keyCode==27 && popupStatus==1){
                    			disablePopup('product_sizes');
                    		}
                    	});
                        
                    }
                });
            }else{
                $.ajax({
                    url: 'ajax_request.php?action=add_to_cart&cid=0&sid=0&pid='+pid+'&qty='+qty,
                    success: function(data) {
                        $('#cart_content').html(data);
                        $('#product_sizes').html("<center><b><br/><br/>Product added successfully!</b></center>");
                        window.setTimeout(hidepopup, 3000);

                    }
                });
            }
      }
   });
}
//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

function hidepopup(){
    disablePopup('product_sizes');
}
//loading popup with jQuery magic!
function loadPopup(div_id){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#"+div_id).fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(div_id){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#"+div_id).fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(div_id){
	//request data for centering
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var popupHeight = $("#"+div_id).height();
	var popupWidth = $("#"+div_id).width();
    var scrollTop = $(window).scrollTop();
	//centering
	$("#"+div_id).css({
		"position": "absolute",
		"top": scrollTop + windowHeight/2-popupHeight/2-70,
		"left": windowWidth/2-popupWidth/2-250
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight,
		"width": windowWidth
	});
	
}
function forceNumber(){
    var ev = window.event;
    if((ev.keyCode>=48 && ev.keyCode<=57) || (ev.keyCode>=35 && ev.keyCode<=40) || (ev.keyCode>=96 && ev.keyCode<=105) || (ev.keyCode==46 || ev.keyCode==8)){
        return true;
    }
    return false;
}
gsAjaxDivID = "";
function SendAjaxRequest(sURL, sDivID){
    gsAjaxDivID = sDivID;
    document.getElementById(gsAjaxDivID).innerHTML = "<br/><center><img src='images/loading.gif'/></center>";
    http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
     http_request = new XMLHttpRequest();
     if (http_request.overrideMimeType) {
        http_request.overrideMimeType('text/xml');
     }
    } else if (window.ActiveXObject) { // IE
     try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
        try {
           http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
     }
    }
    if (!http_request) {
     alert('Cannot create XMLHTTP instance');
     return false;
    }
    http_request.onreadystatechange = alertContents;
    http_request.open('GET', sURL, true);
    http_request.send(null);
}
function alertContents() {
  if (http_request.readyState == 4) {
     if (http_request.status == 200) {
        var sResponseText = http_request.responseText;
        document.getElementById(gsAjaxDivID).innerHTML = sResponseText; 
     } else {
        alert('There was a problem with the request.');
     }
  }
}
