/********************************************************
*
*  Show and hide divs (will soon be changed)
*
*********************************************************/
var ids=new Array('a1','a2','a3','a4','showcoupon1','showcoupon2','showcoupon3','showcartoption','showcartoption2','showcartoption3','showstate1','showstate2','showcat1','showcat2','hidecat1');

function switchid(id){	
	hideallids();
	showdiv(id);
}

function hideallids(){
	//loop through the array and hide each element by id
	for (var i=0;i<ids.length;i++){
		hidediv(ids[i]);
	}		  
}

function hidediv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function showdiv(id) {
	//safe function to show an element with a specified id
		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}



/********************************************************
*
*  Show a popup (will soon be changed)
*
*********************************************************/
var win2=null;

function NewWindow(mypage,myname,w,h,scroll,pos){
	if(pos=="random"){
		LeftPosition = (screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
		TopPosition = (screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
	}
	if(pos=="center"){
		LeftPosition = (screen.width)?(screen.width-w)/2:100;
		TopPosition = (screen.height)?(screen.height-h)/2:100;
	}
	else if((pos!="center" && pos!="random") || pos==null){
		LeftPosition = 0;TopPosition=20
	}
	
	settings = 'width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	
	win2 = window.open(mypage,myname,settings);
}





/********************************************************
*
*  Tools
*
*********************************************************/

//check if a_number is a number
function numberOnly(a_number){
	var numericExpression = /^[0-9]+$/;
	if(a_number.match(numericExpression)){
		return true;
	}else{
		return false;
	}
}


//remove text from a textfield
function deleteText(a_element, a_text){
	element = document.getElementById(a_element);
		
	if(element.value == a_text)
		element.value = "";
}


//trim text
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}



/********************************************************
*
*  Disable/Enable keys
*
*********************************************************/
//disable the "enter" key (coupon code section)
function handleEnter (field, event){
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		var i;
		for (i = 0; i < field.form.elements.length; i++)
			if (field == field.form.elements[i])
				break;
		i = (i + 1) % field.form.elements.length;
		field.form.elements[i].focus();
		return false;
	} 
	else
	return true;
}  

//disable the letters keys
function numbersonly(myfield, e, dec){
	var key;
	var keychar;
	
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	   
	keychar = String.fromCharCode(key);
	
	// control keys
	if ((key==null) || (key==0) || (key==8) || 
		(key==9) || (key==13) || (key==27) )
	   return true;
	
	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
	   return true;
	
	// decimal point jump
	else if (dec && (keychar == ".")){
	   myfield.form.elements[dec].focus();
	   return false;
	}
	
	else
	   return false;
}





/********************************************************
*
*  Validation entries (will change for the alert)
*
*********************************************************/

//remove item from the cart question
function deleteItemFromCart(code) {
	var answer = confirm("Are you sure you want to remove \nthis item from your cart?");
	
	if (answer){
		window.location = "index.php?section=cart&action=1&code="+code;
	}
	else{
		return false;
	}
}


//check if no option is missing
function validate_poption(ff,cust_txt,col,rev,size) {
	var error = "";
	var qtyError = false;
	
	if(cust_txt == 1 && ff.custom_text.value == "") 
		error += "-Enter your text.\n";
		
	if(size == 1 && ff.size.value == -1)
		error += "-Select a size.\n";
	
	if(col == 1 && ff.color.value == -1) 
		error += "-Select a color.\n";
	
	if(ff.qty.value < 1 || !numberOnly(ff.qty.value)) {
		error += "-Quantity must be at least 1.";
		qtyError = true;
	}	
	
	if(error != ""){
		alert(error);
		
		if(qtyError){ //set a correct quantity
			quantityText = document.getElementById("qty");
			quantityText.value = "1";	
		}
		
		return false;
	}else{	
		return true;	
	}
}


//make a category is selected
function validate_cat_selection(a_category) 
{
	if (a_category == -1) {
		alert("-Please select a category.");
		return false;
	}else{
		return true;
	}
}


//validate only the quantity
function validateQuantity(a_element, a_maxQty, a_msgError){
	var error = false;
	element = document.getElementById(a_element);
	
	if(a_maxQty == -1 && element.value < 1)
		error = true;
	
	if(a_maxQty != -1 && (element.value < 1 || element.value > a_maxQty))
		error = true;
		
	if(!numberOnly(element.value))
		error = true;
	
	if (error) {
		alert(a_msgError);
		element.value = "1";
		return false;
	}else{
		return true;
	} 
}

//validate only the quantity
function validateCartQuantity(a_element, a_maxQty, a_msgError){
	var error = false;
	
	if(a_maxQty == -1 && a_element.qty.value < 1)
		error = true;
	
	if(a_maxQty != -1 && (a_element.qty.value < 1 || a_element.qty.value > a_maxQty))
		error = true;
		
	if(!numberOnly(a_element.qty.value))
		error = true;
	
	if(error){
		alert(a_msgError);
		return false;
	}else{
		return true;
	} 
}


//show help content
function openHelp(a_id){
	$.ajax({
		type: "GET",
		url: "help.php?id=" + a_id,
		success: function(html){
			$("#helpContent").html(html);
			
		}
	});
	
	$("#helpContent").modal({
		closeHTML: "<div class=\"helpBoxClose\"><a href=\"#close\" onclick=\"$.modal.close();\">"+
				   "<img src=\"images/close_help.png\" style=\"width:115px; height:30px; border:0px;\" /></a></div>",
		opacity:25,
		overlayCss: {backgroundColor:"#000"}
	});
}

//show video
function openVideo(a_id){
	$.ajax({
		type: "GET",
		url: "tools.php?tool=video&id=" + a_id,
		success: function(html){
			$("#videoContent").html(html);
		}
	});
	
	$("#videoContent").modal({
		overlayClose:true,
		opacity:50,
		overlayCss: {backgroundColor:"#000"}
	});
}
