
var Common = function() {
	
	this.openBrowser = function(url, width, height) {
		var iLeft = (screen.width  - width) / 2 ;
		var iTop  = (screen.height - height) / 2 ;
	
		var sOptions = "toolbar=no, scrollbars=yes,status=no,resizable=no,dependent=yes" ;
		sOptions += ",width=" + width ;
		sOptions += ",height=" + height ;
		sOptions += ",left=" + iLeft ;
		sOptions += ",top=" + iTop ;
	
		window.open(url, "", sOptions);
	}
	
	this.getImageSize = function(url, width, height) {
		var image = new Image();
		
		image.src = url;
		
		var realWidth = image.width;
		var realHeight = image.height;
		var maxWidth = (realWidth > width) ? realWidth : width;
		var maxHeight = (realHeight > height) ? realHeight : height;
		var scale = (maxWidth > maxHeight) ? (width / maxWidth) : (height / maxHeight);
		
		realWidth = realWidth * scale;
		realHeight = realHeight * scale;
		
		return new Array(realWidth, realHeight);
	}
	
	this.submitForm = function(form, param) {
		var frm = document.getElementById(form);
		
		if (!frm) {
			return;
		}
		
		if (param) {
			frm.action += param;	
		}
		
		frm.submit();
	}
	
	this.confirmDelete = function(param) {
		if (confirm("Are you sure you want to delete this " + param + "?")) {
			return true;
		}
		
		return false;
	}
	
	this.closeWindow = function() {
		window.close();
	}
	
	this.changeColor = function(element, color) {
		if (!element) {
			return;
		}
		
		element.style.backgroundColor = color;
	}
	
	/* 
	 * Check and enable an element
	 */
	this.enableElement = function(source, widget) {
		if (!source || !widget) {
			return;
		}
	
		var widgetObj = document.getElementById(widget);
		if (!widget) {
			return;
		}
		
		if (source.checked == true) {
			widgetObj.style.enable = true;
		} else {
			widgetObj.style.enable = false;
		}
	}
	
	this.checkUserForm = function() {
		var username = new String(document.frm_user.username.value);
		var password = document.frm_user.password.value;
		var retype_password = document.frm_user.retype_password.value;
		var email = new String(document.frm_user.email.value);
		var fullname = document.frm_user.fullname.value;
		
		if (!username.match(/^([-\d\w][-.\d\w]*)?[-\d\w]$/)) {
			alert("Invalid username! Please try again.");
			document.frm_user.username.focus();
			
			return false;
		}
		
		if (password.length < 6) {
			alert("Password must be at least 6 characters long!");
			document.frm_user.password.focus();
			
			return false;
		}
		
		if (password != retype_password) {
			alert("Password do not match! Please try again.");
			document.frm_user.retype_password.focus();
			
			return false;
		}
		
		if (!email.match(/^([-\d\w][-.\d\w]*)?[-\d\w]@([-\w\d]+\.)+[a-zA-Z]{2,6}$/)) {
			alert("Invalid email! Please try again.");
			document.frm_user.email.focus();
			
			return false;
		}
		
		if (!fullname) {
			alert("Please type in your full name!");
			document.frm_user.fullname.focus();
			
			return false;
		}
		
		return true;
	}
	
	this.checkCheckoutForm = function() {
		var ba_firstname = document.frm_checkout.ba_firstname.value;
		var ba_lastname = document.frm_checkout.ba_lastname.value;
		var ba_email = new String(document.frm_checkout.ba_email.value);
		var ba_street = document.frm_checkout.ba_street.value;
		var ba_city = document.frm_checkout.ba_city.value;
		var ba_country = document.frm_checkout.ba_country.value;
		var ba_phone = document.frm_checkout.ba_phone.value;
		
		var chk_same = document.frm_checkout.chk_same.checked;
		
		var sa_firstname = document.frm_checkout.sa_firstname.value;
		var sa_lastname = document.frm_checkout.sa_lastname.value;
		var sa_email = new String(document.frm_checkout.sa_email.value);
		var sa_street = document.frm_checkout.sa_street.value;
		var sa_city = document.frm_checkout.sa_city.value;
		var sa_country = document.frm_checkout.sa_country.value;
		var sa_phone = document.frm_checkout.sa_phone.value;
		
		if (!ba_firstname) {
			alert("Please type in your first name!");
			document.frm_checkout.ba_firstname.focus();
			
			return false;
		}

		if (!ba_lastname) {
			alert("Please type in your last name!");
			document.frm_checkout.ba_lastname.focus();
			
			return false;
		}
		
		if (!ba_email.match(/^([-\d\w][-.\d\w]*)?[-\d\w]@([-\w\d]+\.)+[a-zA-Z]{2,6}$/)) {
			alert("Invalid email! Please try again.");
			document.frm_checkout.ba_email.focus();
			
			return false;
		}
		
		if (!ba_street) {
			alert("Please type in your street address!");
			document.frm_checkout.ba_street.focus();
			
			return false;
		}

		if (!ba_city) {
			alert("Please type in your city/province name!");
			document.frm_checkout.ba_city.focus();
			
			return false;
		}

		if (!ba_country) {
			alert("Please type in your country name!");
			document.frm_checkout.ba_country.focus();
			
			return false;
		}

		if (!ba_phone) {
			alert("Please type in your phone number!");
			document.frm_checkout.ba_phone.focus();
			
			return false;
		}
		
		if (!chk_same) {
			if (!sa_firstname) {
				alert("Please type in your first name!");
				document.frm_checkout.sa_firstname.focus();
				
				return false;
			}
	
			if (!sa_lastname) {
				alert("Please type in your last name!");
				document.frm_checkout.sa_lastname.focus();
				
				return false;
			}
			
			if (!sa_email.match(/^([-\d\w][-.\d\w]*)?[-\d\w]@([-\w\d]+\.)+[a-zA-Z]{2,6}$/)) {
				alert("Invalid email! Please try again.");
				document.frm_checkout.sa_email.focus();
				
				return false;
			}
			
			if (!sa_street) {
				alert("Please type in your street address!");
				document.frm_checkout.sa_street.focus();
				
				return false;
			}
	
			if (!sa_city) {
				alert("Please type in your city/province name!");
				document.frm_checkout.sa_city.focus();
				
				return false;
			}
	
			if (!sa_country) {
				alert("Please type in your country name!");
				document.frm_checkout.sa_country.focus();
				
				return false;
			}
	
			if (!sa_phone) {
				alert("Please type in your phone number!");
				document.frm_checkout.sa_phone.focus();
				
				return false;
			}
		}

		return true;
	}	
	
	this.checkUserUpdateForm = function() {
		var email = new String(document.frm_user.email.value);
		var fullname = document.frm_user.fullname.value;
		
		if (!email.match(/^([-\d\w][-.\d\w]*)?[-\d\w]@([-\w\d]+\.)+[a-zA-Z]{2,6}$/)) {
			alert("Invalid email! Please try again.");
			document.frm_user.email.focus();
			
			return false;
		}
		
		if (!fullname) {
			alert("Please type in your full name!");
			document.frm_user.fullname.focus();
			
			return false;
		}
		
		return true;
	}

	this.checkCategoryForm = function() {
		var name = document.frm_category.name.value;
		
		if (!name) {
			alert("Invalid category name! Try again...");
			
			document.frm_category.name.focus();
			
			return false;
		}
		
		return true;
	}
	
	this.checkGemForm = function() {
		var name = document.frm_gem.name.value;
		
		if (!name) {
			alert("Invalid gem name! Try again...");
			
			document.frm_gem.name.focus();
			
			return false;
		}
		
		return true;
	}
	
	this.checkProductForm = function() {
		var name = document.frm_product.name.value;
		var gems_length = document.frm_product.gems.options.length;
		
		var width = document.frm_product.width.value;
		var height = document.frm_product.height.value;
		var price = document.frm_product.price.value;
		
		var gems_selected = 0;
		var selected_gems = "";
		
		for (i = 0; i < gems_length; i ++) {
			if (document.frm_product.gems.options[i].selected) {
				gems_selected ++;
				selected_gems += document.frm_product.gems.options[i].value + "|";
			}
		}
		
		if (!name) {
			alert("Invalid product name! Try again...");
			document.frm_product.name.focus();
			
			return false;
		}
		
		if (gems_selected < 1) {
			alert("You must select at least one gemstone!");
			document.frm_product.gems.focus();
			
			return false;
		}
		
		if (isNaN(width)) {
			alert("Product width must be a number!");
			document.frm_product.width.focus();
			
			return false;
		}

		if (isNaN(height)) {
			alert("Product height must be a number!");
			document.frm_product.height.focus();
			
			return false;
		}

		if (isNaN(price)) {
			alert("Product price must be a number!");
			document.frm_product.price.focus();
			
			return false;
		}

		// Assign values to hidden input type.
		document.frm_product.selected_gems.value = selected_gems;
		
		return true;
	}
	
	
}
// JavaScript Document

var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_mozilla = (agt.indexOf('gecko') != -1);
var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var m_space = 2;
//if(is_mozilla) m_space = 0;
//if(is_ie) m_space = 100;
var menu_on_load = '';
var timeoutPop;
function findPosXY( obj )
{
	var curleft = 0;
	var curtop = 0;

	try
	{

			if (obj.offsetParent) {

				while (obj.offsetParent) {

					if(obj.offsetParent.offsetParent==null)
						break;

					curleft += obj.offsetLeft;
					curtop  += obj.offsetTop;
					obj = obj.offsetParent;

				}
			}
			else
			{
				if (obj.x) curleft += obj.x;
				if (obj.y) curtop  += obj.y;
			}
	}catch(ex){}

	var retObj = new Object();
	retObj.left = curleft;
	retObj.top  = curtop;

	return retObj;
}

function findPosX(obj)
{
	var curleft = 0;

	try
	{
			if (obj.offsetParent) {
				while (obj.offsetParent) {
					if(obj.offsetParent.offsetParent==null)
						break;
					curleft += obj.offsetLeft;
					obj = obj.offsetParent;
				}
			} else if (obj.x) curleft += obj.x;
	}catch(ex){}

	return curleft;
}

function parseStylePX( size )
{
	if( typeof(size) == "string" )
	{
		if( size.charAt(size.length -2) + "" + size.charAt(size.length -1) == "px" )
			size = parseInt(size.substr( 0, size.length -2 ));
		else
			size = parseInt(size);
	}
	if( isNaN(size) ) size = 0;
	return 	size;
}


function findPosY(obj) {
	var curtop = 0;

	try
	{
		if (obj.offsetParent) {
			while (obj.offsetParent) {
				if(obj.offsetParent.offsetParent.offsetParent==null)
						break;
				curtop += obj.offsetTop;
				obj = obj.offsetParent;
			}
		} else if (obj.y) curtop += obj.y;
	}catch(ex){}
	return curtop;
}
function getMouseXY(e) {
  var XY={x:0, y:0};
  var tempX, tempY;
  /*if (event.clientX){
   tempX = event.clientX + (document.documentElement.scrollLeft ?
   document.documentElement.scrollLeft :
   document.body.scrollLeft);
   tempY = event.clientY + (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop);
  }else if (evt.pageX){
	tempX = evt.pageX;
	tempY = evt.pageY
  }*/
  
  if (document.all) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  XY.x = tempX;
  XY.y = tempY;
  return XY
}
function mouseMove(e){
	var cXY = getMouseXY(e);
	document.getElementById('cX').value = cXY.x;
	document.getElementById('cY').value = cXY.y;
}
function popupMenu(el, popup){
	if(menu_on_load != '' && menu_on_load != popup && timeoutPop){
		if(document.getElementById(menu_on_load)) setStyle(document.getElementById(menu_on_load), 'display:none;');
		//clearTimeout(timeoutPop);
		//menu_on_load = '';
	}
	if(document.getElementById(popup)){
		clearTimeout(timeoutPop);
		//var posXY = findPosXY(el);
		var popupDiv = document.getElementById(popup);
		if(popupDiv.style.display == 'none'){
			var left = findPosX(el) + m_space;
			var top = el.offsetHeight + m_space;
			var textStyle = "width:"+el.offsetWidth+"px;left:"+left+"px;top:"+top+"px;display:;";
			setStyle(popupDiv, textStyle);
			popupDiv.onmouseover = function(){
				clearTimeout(timeoutPop);
				setStyle(this, 'display:');
			}
			popupDiv.onmouseout = function(){
				setStyle(this, 'display:none');
			}
		}
	}
}
function popupMenuOut(el){
	//clearTimeout(timeoutPop);
		if(document.getElementById(el)) timeoutPop = setTimeout("setStyle(document.getElementById('"+el+"'), 'display:none;')",600);
	menu_on_load = el;
	//var t=setTimeout("alert('5 seconds!')",5000)
}

var menu_on_load2 = '';
function rightMenu(el, popup){
	if(menu_on_load2 != '' && menu_on_load2 != popup && timeoutPop){
		if(document.getElementById(menu_on_load2)) setStyle(document.getElementById(menu_on_load2), 'display:none;');
		//clearTimeout(timeoutPop);
		//menu_on_load2 = '';
	}
	if(document.getElementById(popup)){
		clearTimeout(timeoutPop);
		//var posXY = findPosXY(el);
		var popupDiv = document.getElementById(popup);
		if(popupDiv.style.display == 'none'){
			var left = findPosX(el) + el.offsetWidth + m_space + 180;
			var top = findPosY(el) + m_space + 20;
			var textStyle = "left:"+left+"px;top:"+top+"px;display:;";
			//var textStyle = "width:"+el.offsetWidth+"px;left:"+left+"px;top:"+top+"px;display:;";
			setStyle(popupDiv, textStyle);
			popupDiv.onmouseover = function(){
				clearTimeout(timeoutPop);
				setStyle(this, 'display:');
			}
			popupDiv.onmouseout = function(){
				setStyle(this, 'display:none');
			}
		}
	}
}
function rightMenuOut(el){
	//clearTimeout(timeoutPop);
		if(document.getElementById(el)) timeoutPop = setTimeout("setStyle(document.getElementById('"+el+"'), 'display:none;')",600);
	menu_on_load2 = el;
	//var t=setTimeout("alert('5 seconds!')",5000)
}
function setStyle(el, prop) {
	if (document.all) {
		var props = prop.split(';');
		for (var i = 0; i < props.length; i++) {
			var attrib = props[i].split(':');
			if(attrib[0] != '' && attrib[0] != 'undefined') eval("el.style."+attrib[0]+"='"+attrib[1]+"';");
		}
	}else{
			var cssText = el.style.cssText;
			var cssTexts = cssText.split(';');
			var props = prop.split(';');
			var strcssText = '';
			for (var i = 0; i < cssTexts.length; i++) {
				var cssTextattrib = cssTexts[i].split(':');
				var usecssTextattrib = 1;
				for (var j = 0; j < props.length; j++) {
					var attrib = props[j].split(':');
					if(attrib[0] == '' || attrib[0] == ' ' || attrib[0] == 'undefined'){
						continue;
					}else if(Trim(attrib[0]) == Trim(cssTextattrib[0])){
						usecssTextattrib = 0;
						break;
					}
				}
				if(usecssTextattrib == 1 && cssTextattrib[0] != '' && cssTextattrib[0] != ' ' && cssTextattrib[0] != 'undefined'){
					strcssText += cssTextattrib[0] + ':' + cssTextattrib[1] + ';';
				}
			}
			el.setAttribute("style", strcssText + prop);
	}
}
function Trim(text){
    return text.replace(/^\s*|\s*$/g,'');
}

function LTrim(text){
    return text.replace(/^\s*/g,'');
}

function RTrim(text){
    return text.replace(/\s*$/g,'');
}
function takeYear(theDate)
{
	var x = theDate.getYear();
	var y = x % 100;
	y += (y < 38) ? 2000 : 1900;
	return y;
}

// Popup

var popUp = null;

function pop(url)
{
	if (popUp && !popUp.closed)
		popUp.location.href = url;
	else
		popUp = window.open(url,'popUp','height=500,width=700,scrollbars=yes,resizable=yes,toolbar=yes,location=yes');
	popUp.focus();
	return false;
}

// Cookies

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

function checkItLocal(string)
{
	placeOfDetect = detect.indexOf(string) + 1;
	thestring = string;
	return placeOfDetect;
}

function getElementsByTagNames(list,obj)
{
	if (!obj) var obj = document;
	var tagNames = list.split(',');
	var resultArray = new Array();
	for (var i=0;i<tagNames.length;i++)
	{
		var tags = obj.getElementsByTagName(tagNames[i]);
		for (var j=0;j<tags.length;j++)
		{
			resultArray.push(tags[j]);
		}
	}
	var testNode = resultArray[0];
	if (testNode.sourceIndex)
	{
		resultArray.sort(function (a,b) {
				return a.sourceIndex - b.sourceIndex;
		});
	}
	else if (testNode.compareDocumentPosition)
	{
		resultArray.sort(function (a,b) {
				return 3 - (a.compareDocumentPosition(b) & 6);
		});
	}
	return resultArray;
}

// push and shift for IE5

function Array_push() {
	var A_p = 0
	for (A_p = 0; A_p < arguments.length; A_p++) {
		this[this.length] = arguments[A_p]
	}
	return this.length
}

if (typeof Array.prototype.push == "undefined") {
	Array.prototype.push = Array_push
}

function Array_shift() {
	var A_s = 0
	var response = this[0]
	for (A_s = 0; A_s < this.length-1; A_s++) {
		this[A_s] = this[A_s + 1]
	}
	this.length--
	return response
}

if (typeof Array.prototype.shift == "undefined") {
	Array.prototype.shift = Array_shift
}
//document.onmousemove = popupMenu;
//document.onmousedown =  popupMenu;
//document.oncontextmenu = function(){ return false; };