/****************************************************
* JS FUNCTIONS  (Provided By - raZZzz PHP FrameWorks)
*****************************************************/
function confirmDelete(name,url){	
	if(confirm("Are you sure you want to delete \'"+ name +"\' ?")){
		location.replace(url);	
	}
	return false;
}
function confirmCancel(url){
	if(confirm("Are you sure you want to cancel & go back ?")){
		location.replace(url);
	}
}
function confirmReset(form){
	if(confirm("Are you sure you want to reset ?")){
		form.reset();
	}	
}

function confirmLogout(url){	
	if(confirm("Are you sure you want to logout ?")){
		location.replace(url);	
	}
}

function goTo(url){
	if(url == ""){
		alert("url is empty");
	}else{
		window.location	= url;
	}
}
function getIndex(what,form){
    for (var i = 0; i < form.length; i++)
        if (what == form[i]){
            return i;
		}
    return -1;
}

function checkAll(form){
	//alert(form);	
	//alert(form.length);
	// precaution: to make this function flexible , make pcb_delete & cb_delete[] dynamic		
	if(form.pcb_delete.checked == true){
		//alert("Check All");
		for(var i = 0; i < form.length; i++){
			if(form.elements[i].name == "cb_delete[]"){
				form.elements[i].checked = true;
			}
		}
	}
	else if(form.pcb_delete.checked == false){
		//alert("Uncheck All");
		for(var i = 0; i < form.length; i++){
			if(form.elements[i].name == "cb_delete[]"){
				form.elements[i].checked = false;
			}
		}
	}
}

function confirmCbDelete(form){
	var counter = 0;
	for(var i = 0; i < form.length; i++){
		if(form.elements[i].name == "cb_delete[]"){
			if(form.elements[i].checked == true){
				counter++;
			}
		}
	}
	if(counter == 0){
		alert("Please select at least one data to delete.");
		return false;
	}
	else{
		if(confirm("Are you sure to delete the selected datas ?")){
			form.mode.value = "DELETE";			
			form.submit();
			return true;
		}else{
			return false;
		}
	}
}
/************************************************************
* AJAX FUNCTIONS - Start
************************************************************/
function getXmlHttpObj() {
	// Get the proper xmlHttpRequest Object
	var xmlHttpObj;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttpObj = new XMLHttpRequest();
	}catch (e){
		// Internet Explorer
		try{
			xmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
		  try{
		  	xmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		  }catch (e){
				alert("Your browser does not support AJAX!");
				return false;
		  }
		}
	}
	return xmlHttpObj;
}

function getXmlHttpData_bak(elementID, page){
	var xmlHttp = getXmlHttpObj();
	// Get data sent from the server
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4){
			if (xmlHttp.status == 200) {
				// Processing goes here..
            	document.getElementById(elementID).innerHTML = xmlHttp.responseText;
				
			} else {
				alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
			}			
		}
	}	
	// Send request to the server
	xmlHttp.open("GET", page , true);
	xmlHttp.send(null);	
}
/************************************************************
* AJAX FUNCTIONS - End
************************************************************/

/** ADD / DELETE ROWS **/
var count = 0;
function createRow(rowEle, data){
	//alert(data);
	count++;
	var obj 		= document.getElementById(rowEle);
	var new_obj  	= document.createElement('div');	
	var div_id 		= "row_id"+count;	
	new_obj.setAttribute("id", div_id);
	new_obj.setAttribute("style", "padding-top:3px");
	var contents = '';	
	contents 	+= '<input type="file" name="product_photo[]" /> <a href="javascript:void(0)" onclick="deleteRow(\''+rowEle+'\',\''+div_id+'\')">[X]</a>';	
	new_obj.innerHTML = contents;
	obj.appendChild(new_obj);		
}
/********************************************
*Create/Delete Product Image Rows - Start
********************************************/
var pdt_image_count = 0;
function createPdtImageRow(rowEle, data, start_from){
	//alert(data);
	if(pdt_image_count == 0){
		pdt_image_count = start_from;
	}
	pdt_image_count++;
	var obj 		= document.getElementById(rowEle);
	var new_obj  	= document.createElement('div');	
	var div_id 		= "row_pdt_image_id"+pdt_image_count;	
	new_obj.setAttribute("id", div_id);
	//new_obj.setAttribute("style", "padding-top:3px");
	var contents = '';
	contents	+= ' <table width="200" border="0" cellpadding="1" cellspacing="1">\r\n';
	contents	+= '<tr>\r\n';
    contents	+=  '<td>'+pdt_image_count+'</td>\r\n';
    contents	+=  '<td><input type="text" name="caption[]" class="shortTxtBox"/></td>\r\n';
    contents	+=  '<td><input type="file" name="product_photo[]" /></td>\r\n';
    contents	+=  '<td><!--<input type="radio" name="is_primary" value="1" />--></td>\r\n';   
	contents 	+=  '<td><a href="javascript:void(0)" onclick="deletePdtImageRow(\''+rowEle+'\',\''+div_id+'\')">[X]</a></td>\r\n';	
	contents	+=  '</tr>\r\n';
	contents	+=  '</table>\r\n';
	new_obj.innerHTML = contents;
	obj.appendChild(new_obj);		
}
function deletePdtImageRow(parentElem, childElem){
	//alert("parnet:"+pelem);
	//alert("clild:"+celem);	
	var parentObj = document.getElementById(parentElem);
	var childObj = document.getElementById(childElem);
	parentObj.removeChild(childObj);	
	pdt_image_count--;
}
/********************************************
*Create/Delete Product Image Rows - End
********************************************/
/********************************************
*Create/Delete Color Rows - Start
********************************************/
var color_count = 0;
function createColorRow(rowEle, data, start_from){
	//alert(data);
	if(color_count == 0){
		color_count = start_from;
	}
	color_count++;
	var obj 		= document.getElementById(rowEle);
	var new_obj  	= document.createElement('div');	
	var div_id 		= "row_color_id"+color_count;	
	new_obj.setAttribute("id", div_id);
	//new_obj.setAttribute("style", "padding-top:3px");
	var contents = '';
	contents	+= ' <table width="200" border="0" cellpadding="1" cellspacing="1">\r\n';
	contents	+= '<tr>\r\n';
    contents	+=  '<td>'+color_count+'</td>\r\n';
    contents	+=  '<td><input type="text" name="color[]" class="shortTxtBox"/></td>\r\n';
    contents	+=  '<td><input type="file" name="color_photo[]" /></td>\r\n';   
	contents 	+=  '<td><a href="javascript:void(0)" onclick="deleteColorRow(\''+rowEle+'\',\''+div_id+'\')">[X]</a></td>\r\n';	
	 contents	+=  '</tr>\r\n';
	  contents	+=  '</table>\r\n';
	new_obj.innerHTML = contents;
	obj.appendChild(new_obj);		
}
function deleteColorRow(parentElem, childElem){
	//alert("parnet:"+pelem);
	//alert("clild:"+celem);	
	var parentObj = document.getElementById(parentElem);
	var childObj = document.getElementById(childElem);
	parentObj.removeChild(childObj);	
	color_count--;
}
/********************************************
*Create/Delete Color Rows - End
********************************************/
/********************************************
*Create/Delete Discount Rows - Start
********************************************/
var discount_count = 0;
function createDiscountRow(rowEle, data, start_from){
	//alert(data);
	if(discount_count == 0){
		discount_count = start_from;
	}
	discount_count++;
	var obj 		= document.getElementById(rowEle);
	var new_obj  	= document.createElement('div');	
	var div_id 		= "row_discount_id"+discount_count;	
	new_obj.setAttribute("id", div_id);
	//new_obj.setAttribute("style", "padding-top:3px");
	var contents = '';
	contents	+= ' <table width="200" border="0" cellpadding="1" cellspacing="1">\r\n';
	contents	+= '<tr>\r\n';
    contents	+=  '<td>'+discount_count+'</td>\r\n';
    contents	+=  '<td><input type="text" name="quantity_from[]" class="shortTxtBox"/></td>\r\n';
     contents	+=  '<td><input type="text" name="quantity_to[]" class="shortTxtBox"/></td>\r\n';
    contents	+=  '<td><input type="text" name="discount[]" class="shortTxtBox"/></td>\r\n';   
	contents 	+=  '<td><a href="javascript:void(0)" onclick="deleteDiscountRow(\''+rowEle+'\',\''+div_id+'\')">[X]</a></td>\r\n';	
	contents	+=  '</tr>\r\n';
	contents	+=  '</table>\r\n';
	new_obj.innerHTML = contents;
	obj.appendChild(new_obj);		
}
function deleteDiscountRow(parentElem, childElem){
	//alert("parnet:"+pelem);
	//alert("clild:"+celem);	
	var parentObj = document.getElementById(parentElem);
	var childObj = document.getElementById(childElem);
	parentObj.removeChild(childObj);	
	discount_count--;
}
/********************************************
*Create/Delete Dicount Rows - End
********************************************/

// PopUp Function
function openPopUp(file_src, window_name, width, height){	
	var width = parseInt(width) + 30;
	var height = parseInt(height) + 80;
	var left_pos = (screen.width / 2) - (width / 2);
	var top_pos = (screen.height / 2) - (height / 2);
	window.open(file_src, window_name, "toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0,width="+width+",height="+height+", left="+left_pos+",top="+top_pos+"");
}

/******************************************************************************************************
* CUSTOM ALERT
******************************************************************************************************/
var ALERT_TITLE = "Oops!";
var ALERT_BUTTON_TEXT = "OK";
function customAlert(txt) {
	// shortcut reference to the document object
	d = document;

	// if the alertDiv object already exists in the DOM, bail out.
	if(d.getElementById("alertDiv")) return;
	if(d.getElementById("transparent_background")) return;
	
	// create Transparent Background
	background_element = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
	background_element.id = "transparent_background";
	background_element.style.position = "absolute";
	background_element.style.zIndex = "1";
	background_element.style.width = "100%";
	background_element.style.height = document.documentElement.scrollTop + document.documentElement.scrollHeight + "px";//"100%";
	background_element.style.top = "0px";
	background_element.style.left = "0px";
	background_element.style.opacity = ".50";
	background_element.style.background = "#000000";
	background_element.style.filter = "alpha(opacity=40)";
	background_element.style.MozOpacity = "0.4";
	background_element.style.backgroundImage = "url(../images/tp.gif)"; // required for IE
	
	// create the alertDiv div as a child of the BODY element
	mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
	mObj.id = "alertDiv";
	 // make sure its as tall as it needs to be to overlay all the content on the page
	mObj.style.height = document.documentElement.scrollHeight + "px";

	// create the DIV that will be the alert 
	alertObj = mObj.appendChild(d.createElement("div"));
	alertObj.id = "alertBox";
	// MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
	if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px";
	// center the alert box
	alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";

	// create an H1 element as the title bar
	h1 = alertObj.appendChild(d.createElement("h1"));	
	h1.appendChild(d.createTextNode(ALERT_TITLE));

	// create a paragraph element to contain the txt argument
	msg = alertObj.appendChild(d.createElement("p"));	
	msg.appendChild(d.createTextNode(txt));

	// create an anchor element to use as the confirmation button.
	btn = alertObj.appendChild(d.createElement("a"));
	btn.id = "closeBtn";
	btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT));
	btn.href = "javascript:void(0)";
	// set up the onclick event to remove the alert when the anchor is clicked
	btn.onclick = function() { removeCustomAlert();return false; }

	
}

// removes the custom alert from the DOM
function removeCustomAlert() {
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("transparent_background"));
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("alertDiv"));
}

/******************************************************************************************************
* CUSTOM CONFIRM
******************************************************************************************************/
var CONFIRM_TITLE = "Confirm ?";
var CONFIRM_BUTTON_TEXT1 = "Yes";
var CONFIRM_BUTTON_TEXT2 = "No";
function customConfirm(txt, url) {
	// shortcut reference to the document object
	d = document;

	// if the alertDiv object already exists in the DOM, bail out.
	if(d.getElementById("alertDiv")) return;
	if(d.getElementById("transparent_background")) return;
	
	// create Transparent Background
	background_element = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
	background_element.id = "transparent_background";
	background_element.style.position = "absolute";
	background_element.style.zIndex = "1";
	background_element.style.width = "100%";
	background_element.style.height = document.documentElement.scrollTop + document.documentElement.scrollHeight + "px";//"100%";
	background_element.style.top = "0px";
	background_element.style.left = "0px";
	background_element.style.opacity = ".50";
	background_element.style.background = "#000000";
	background_element.style.filter = "alpha(opacity=40)";
	background_element.style.MozOpacity = "0.4";
	background_element.style.backgroundImage = "url(../images/tp.gif)"; // required for IE
	
	// create the alertDiv div as a child of the BODY element
	mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
	mObj.id = "alertDiv";
	 // make sure its as tall as it needs to be to overlay all the content on the page
	mObj.style.height = document.documentElement.scrollHeight + "px";

	// create the DIV that will be the alert 
	alertObj = mObj.appendChild(d.createElement("div"));
	alertObj.id = "alertBox";
	// MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
	if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px";
	// center the alert box
	alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";

	// create an H1 element as the title bar
	h1 = alertObj.appendChild(d.createElement("h1"));	
	h1.appendChild(d.createTextNode(CONFIRM_TITLE));

	// create a paragraph element to contain the txt argument
	msg = alertObj.appendChild(d.createElement("p"));	
	msg.appendChild(d.createTextNode(txt));

	// create an anchor element to use as the confirmation button.
	btn1 = alertObj.appendChild(d.createElement("a"));
	btn1.id = "closeBtn";
	btn1.appendChild(d.createTextNode(CONFIRM_BUTTON_TEXT1));
	btn1.href = "#";
	// create an anchor element to use as the confirmation button.
	btn2 = alertObj.appendChild(d.createElement("a"));
	btn2.id = "closeBtn";
	btn2.appendChild(d.createTextNode(CONFIRM_BUTTON_TEXT2));
	btn2.href = "#";
	// set up the onclick event to remove the alert when the anchor is clicked
	btn1.onclick = function() { takeToDeleteConfirmation(url);return true;}
	btn2.onclick = function() { removeCustomConfirm();return false; }

	
}

// removes the custom alert from the DOM
function removeCustomConfirm() {
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("alertDiv"));
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("transparent_background"));
}

// take to delete confirmation
function takeToDeleteConfirmation(url){
	location.replace(url);	
}
// Products related javascripts
function showHideAttribs(element, div_id){
	if(element.checked){
		document.getElementById(div_id).style.display = "block";
	}else{
		document.getElementById(div_id).style.display = "none";
	}
}
// checkbox related showHides
function checkBoxShowHide(checkbox, element_id){	
	if(checkbox.checked){		
		document.getElementById(element_id).style.display = "none";
	}else{		
		document.getElementById(element_id).style.display = "block";
	}
}

// checkbox related showHides for billing details
function billingDetailsShowHide(element_id){		
	var checkbox = document.forms['frm_shipping_billing_detail'].elements['billing_same_as_shipping'];
	//alert(checkbox.checked);
	if(checkbox.checked){	
		document.getElementById("billing_full_name").setAttribute("lang", "");
		document.getElementById("billing_company").setAttribute("lang", "");
		document.getElementById("billing_phone1").setAttribute("lang", "");
		document.getElementById("billing_phone2").setAttribute("lang", "");
		document.getElementById("billing_fax").setAttribute("lang", "");
		document.getElementById("billing_email").setAttribute("lang", "");
		document.getElementById("billing_zip_code").setAttribute("lang", "");
		document.getElementById("billing_address").setAttribute("lang", "");
		document.getElementById("billing_city").setAttribute("lang", "");
		document.getElementById("billing_country_id").setAttribute("lang", "");
		
		document.getElementById(element_id).style.display = "none";
	}else{
		document.getElementById("billing_full_name").setAttribute("lang", "required");
		document.getElementById("billing_company").setAttribute("lang", "required");
		document.getElementById("billing_phone1").setAttribute("lang", "required");
		document.getElementById("billing_phone2").setAttribute("lang", "required");
		document.getElementById("billing_fax").setAttribute("lang", "required");
		document.getElementById("billing_email").setAttribute("lang", "required");
		document.getElementById("billing_zip_code").setAttribute("lang", "required");
		document.getElementById("billing_address").setAttribute("lang", "required");
		document.getElementById("billing_city").setAttribute("lang", "required");
		document.getElementById("billing_country_id").setAttribute("lang", "select");
		
		document.getElementById(element_id).style.display = "block";
	}
}

// function to show hide filter attributes (admin_orders/lists)
function showHideFilterFields(option_value){
	//alert(option_value);
	if(option_value == "by_date"){
		document.getElementById("tbl_date").style.display = "block";
		document.getElementById("tbl_go").style.display = "none";
	}else if(option_value == "by_pending_status" || option_value == "by_processing_status" || option_value == "by_delivered_status"){
		document.getElementById("tbl_date").style.display = "none";
		document.getElementById("tbl_go").style.display = "block";	
	}else{
		document.getElementById("tbl_date").style.display = "none";
		document.getElementById("tbl_go").style.display = "block";
	}	
}