// JavaScript Document
function NewWindow(mypage, myname, w, h, scroll, resizable, status) 
{
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable='+resizable+',status='+status+'';
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) 
	{ 
		win.window.focus(); 
	}
}

function makeInstance() 
{
	var http_request = false;
	
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
	} 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('Giving up sad.gif Cannot create an XMLHTTP instance');
		return false;
	}
	
	return http_request;
	
}

/***
* Submits the form given by objForm and validates it through the file given by fileValidate.
* If validation failed, then fileValidate will return the appropriate error message that will be displayed within 
* txtStatusContainer, otherwise if validation is successful then objForm will be submitted. 
*
* method: submit_form
* param: Form objForm, File filevalidate, String txtStatusContainer
* return: void
*
*/
function validate(objForm, fileValidate, txtStatusContainer)
{
	ajax = makeInstance();
	ajax.onreadystatechange = function()
	{
	
		if(ajax.readyState == 1) 
		{	
			// display message to indicate validation is in progress
			document.getElementById(txtStatusContainer).innerHTML = '<span id="gray">Validating Submission...</span>';		
		}
		
		// validation complete
		if((ajax.readyState == 4)&&(ajax.status == 200)) 
		{
			// validation successful
			if(ajax.responseText == 0) 
			{	
				document.getElementById(txtStatusContainer).innerHTML = '<span class="green">Submission Valid! Processing...</span><br/>';

				// submit the form
				objForm.submit(); 			
			}
			
			// validation failed
			else
			{
				document.getElementById(txtStatusContainer).innerHTML = '<span class="red">' + ajax.responseText + '</span><br/>';
			}
		}				
	}
	ajax.open('POST', fileValidate, true);
	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajax.send(getRequestBody(objForm));
}


function getRequestBody(fobj) { 
	 var str = ""; 
	 var ft = ""; 
	 var fv = ""; 
	 var fn = ""; 
	 var els = ""; 
	 for(var i = 0;i < fobj.elements.length;i++) { 
	  els = fobj.elements[i]; 
	  ft = els.title; 
	  fv = els.value; 
	  fn = els.name; 
	 switch(els.type) { 
	  case "text": 
	  case "hidden": 
	  case "password": 
	  case "textarea":
		case "file":

	  str += fn + "=" + encodeURI(fv) + "&"; 
	  break;  
	 
	  case "checkbox": 
	  case "radio": 
	   if(els.checked) str += fn + "=" + encodeURI(fv) + "&"; 
	  break;     
	 
	 	case "select-one":
	    str += fn + "=" + 
	    els.options[els.selectedIndex].value + "&"; 
		break;
		
	  case "select-multiple":
		
			for(j=0; j<els.length; j++)
			{
				if(els.options[j].selected)
				{
					str += fn + "=" + 
					els.options[j].value + "&";
				}
			}
			
	  break; 
	  } // switch 
	 } // for 
	 str = str.substr(0,(str.length - 1)); 
return str; 
} 


/*** Shopping Cart Related ***/

// add item to shopping cart
function addToCart(intProductID, decPrice)
{
	ajax = makeInstance();
	ajax.onreadystatechange = function()
	{
		if((ajax.readyState == 4)&&(ajax.status == 200)) 
		{	
			//alert(ajax.responseText);
			window.location.href = "view-cart.php";
		}
	}
	ajax.open('GET','./ajax/cart/addtocart.php?intProductID='+intProductID+'&decPrice='+decPrice, true);
	ajax.send(null);
}

// update item in shopping cart
function updateCart(intProductID, intQty)
{
	ajax = makeInstance();
	ajax.onreadystatechange = function()
	{
		if((ajax.readyState == 4)&&(ajax.status == 200)) 
		{	
			//alert(ajax.responseText);
			document.getElementById('cartHolder').innerHTML = ajax.responseText;
		}
	}
	ajax.open('GET','./ajax/cart/updatecart.php?intProductID='+intProductID+'&intQty='+intQty, true);
	ajax.send(null);
}

function confirmSelected()
{
	return confirm('Are you sure you want to perform the actions on the selected items?');
}

function mLogin()
{
	ajax = makeInstance();
	ajax.onreadystatechange = function()
	{
	
		if(ajax.readyState == 1) 
		{	
			// display message to indicate validation is in progress
			document.getElementById('frmLoginStatus').innerHTML = '<span id="gray">Validating Submission...</span>';		
		}
		
		// validation complete
		if((ajax.readyState == 4)&&(ajax.status == 200)) 
		{
			// validation successful
			if(ajax.responseText == 0) 
			{	
				//refersh current page
				history.go(0);			
			}
			
			// validation failed
			else
			{
				document.getElementById('frmLoginStatus').innerHTML = '<span class="red">' + ajax.responseText + '</span><br/>';
			}
		}				
	}
	ajax.open('POST', 'ajax/mlogin.php', true);
	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajax.send(getRequestBody(document.frmLogin));
}

function mLogout()
{
	if(confirm('Are you sure you want to logout?'))
	{
		ajax = makeInstance();
		ajax.onreadystatechange = function()
		{
			if((ajax.readyState == 4)&&(ajax.status == 200))
			{
				//refersh current page
				history.go(0);	
			}
		}
		ajax.open('GET', 'ajax/mlogout.php', true);
		ajax.send(null);
	}
}

function calcCharLeft(objTextarea, intCharLimit)
{
	var intCharLeft;
	
	if(objTextarea.value.length > intCharLimit) // if too long... trim it!
	{
		objTextarea.value = objTextarea.value.substring(0, intCharLimit);
		intCharLeft = 0;
	}
	else // else update characters left counter
	{
		intCharLeft = intCharLimit - objTextarea.value.length;
	}
	
	$(objTextarea).parent().find('#charleft').html(intCharLeft + ' character(s) left.');
//	document.getElementById('charleft').innerHTML = intCharLeft + ' character(s) left.';

}

if( document.addEventListener )
	document.addEventListener( 'DOMContentLoaded', cmxform, false);

function cmxform(){
  // Hide forms
  $('form.cssform').hide().end();

  // Processing
  $('form.cssform').find( 'li/label' ).not( '.nocss' ).each( function( i ){
    var labelContent = this.innerHTML;
    var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
    var labelSpan = document.createElement( 'span' );
        labelSpan.style.display = 'block';
        labelSpan.style.width = labelWidth;
        labelSpan.innerHTML = labelContent;
    this.style.display = '-moz-inline-box';
    this.innerHTML = null;
    this.appendChild( labelSpan );
  } ).end();

  // Show forms
  $('form.cssform' ).show().end();
}