/**
 * Pathname to Root Directory of Website
 */
var rootUrl = "http://www.ribbons.sg/";
var contentUrl = "wp-content/themes/rbsg/pics/";

var shutterSettings = {
	msgLoading: "L O A D I N G",
	msgClose: "Click to Close",
	imageCount: "1"
};

/**
 * Create a new Ajax Object. An error will be returned if the object cannot be created.
 */
function GetXmlHttpObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	}
	
	if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	return null;
}

/**
 * Perform an Ajax HTTP Request
 * @param {Object} url
 * @param {Object} target
 * @param {Object} method
 * @param {Object} params
 */
function AjaxRequest(url, target, method, params) {
	var ajaxObj = GetXmlHttpObject();
	
	if (ajaxObj == null) {
		alert("Browser does not support HTTP Request");
		return;
	}
	
	if (method == "GET") {
		ajaxObj.open(method, url, true);
		ajaxObj.onreadystatechange = function() {
			if (ajaxObj.readyState == 4) {
				document.getElementById(target).innerHTML = ajaxObj.responseText;
			} else {
				document.getElementById(target).innerHTML = "<div class=''><img src='" + rootUrl + contentUrl +"loading.gif' /></div>";
			}
		}
		ajaxObj.send(null);
	} else if (method == "POST") {
		ajaxObj.open(method, url, true);
		ajaxObj.sendRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajaxObj.sendRequestHeader("Content-length", params.length);
		ajaxObj.sendRequestHeader("Connection", "close");
		
		ajaxObj.onreadystatechange = function() {
			if (ajaxObj.readyState == 4) {
				document.getElementById(target).innerHTML = ajaxObj.responseText;
			} else {
				document.getElementById(target).innerHTML = "<div class=''><img src='" + rootUrl + contentUrl +"loading.gif' /></div>";
			}
		}
		ajaxObj.send(params);
	}
}

/**
 * Selecting an Object and deselects the others
 * @param {ID of the object to select} id
 * @param {Total number of objects that } total_obj
 * @param {Name of the object series} name
 * @param {Class Name when selected} select_class
 * @param {Class Name when unselected} unselect_class
 */
function SelectObject(id, total_obj, name, select_class, unselect_class) {
	for (i = 1; i <= total_obj; i++) {
		var component = name + "-" + i;
		document.getElementById(component).className = unselect_class;
	}
	
	document.getElementById(name + "-" + id).className = select_class;
}

/**
 * Connect to an URL
 * @param {string} url
 * @param {Object} type
 * @param {Object} value
 */
function GoToLocation(url, type, value) {
	window.location = url + "?" + type + "=" + value;
} 

function ReadForm(obj1, tst) {
	// Read the user form
	var i, j, pos;
	val_total = "";
	val_combo = "";
	
	for (i = 0; i < obj1.length; i++) {
		// run entire form
		obj = obj1.elements[i]; // a form element
		if (obj.type == "select-one") { // just selects
			if (obj.name == "quantity" ||
			obj.name == "amount") 
				continue;
			pos = obj.selectedIndex; // which option selected
			val = obj.options[pos].value; // selected value
			val_combo = val_combo + "(" + val + ")";
		}
	}
	// Now summarize everything we have processed above
	val_total = obj1.product_tmp.value + val_combo;
	obj1.product.value = val_total;
}

/**
 * Show or hide a component
 * Corresponding name of the component to be hid is named target_id and its
 * test field should be named target_id_text
 * @param {Name of component} target_id
 */
function ShowDiv(target_id) {
	var target = document.getElementById(target_id);
	var target_text = document.getElementById(target_id + "_text");
	
	if (target.style.display == "none") {
		target.style.display = "block";
		target_text.innerHTML = "Hide";
	} else {
		target.style.display = "none";
		target_text.innerHTML = "Show";
	}
}

/**
 * ShowDiv With Ajax HTTP Request
 * @param {Object} target_id
 * @param {Object} url
 * @param {Object} method
 * @param {Object} target
 */
function ShowDivXHR(target_id, func, params) {
	var target = document.getElementById(target_id);
	var target_text = document.getElementById(target_id + "_text");
	
	AjaxRequest(rootUrl + "run/?func=" + func + "&" + params, target_id, "GET", "");
}

/* Ajax Upload Functions */
function $m(theVar){
	return document.getElementById(theVar)
}

function remove(theVar){
	var theParent = theVar.parentNode;
	theParent.removeChild(theVar);
}

function addEvent(obj, evType, fn){
	if(obj.addEventListener)
	    obj.addEventListener(evType, fn, true)
	if(obj.attachEvent)
	    obj.attachEvent("on"+evType, fn)
}

function removeEvent(obj, type, fn){
	if(obj.detachEvent){
		obj.detachEvent('on'+type, fn);
	}else{
		obj.removeEventListener(type, fn, false);
	}
}

function isWebKit(){
	return RegExp(" AppleWebKit/").test(navigator.userAgent);
}

function ajaxUpload(form,url_action,id_element,html_show_loading,html_error_http){
	var detectWebKit = isWebKit();
	form = typeof(form)=="string"?$m(form):form;
	var erro="";
	if(form==null || typeof(form)=="undefined"){
		erro += "The form of 1st parameter does not exists.\n";
	}else if(form.nodeName.toLowerCase()!="form"){
		erro += "The form of 1st parameter its not a form.\n";
	}
	if($m(id_element)==null){
		erro += "The element of 3rd parameter does not exists.\n";
	}
	if(erro.length>0){
		alert("Error in call ajaxUpload:\n" + erro);
		return;
	}
	var iframe = document.createElement("iframe");
	iframe.setAttribute("id","ajax-temp");
	iframe.setAttribute("name","ajax-temp");
	iframe.setAttribute("width","0");
	iframe.setAttribute("height","0");
	iframe.setAttribute("border","0");
	iframe.setAttribute("style","width: 0; height: 0; border: none;");
	form.parentNode.appendChild(iframe);
	window.frames['ajax-temp'].name="ajax-temp";
	var doUpload = function(){
		removeEvent($m('ajax-temp'),"load", doUpload);
		var cross = "javascript: ";
		cross += "window.parent.$m('"+id_element+"').innerHTML = document.body.innerHTML; void(0);";
		$m(id_element).innerHTML = html_error_http;
		$m('ajax-temp').src = cross;
		if(detectWebKit){
        	remove($m('ajax-temp'));
        }else{
        	setTimeout(function(){ remove($m('ajax-temp'))}, 250);
        }
    }
	addEvent($m('ajax-temp'),"load", doUpload);
	form.setAttribute("target","ajax-temp");
	form.setAttribute("action",url_action);
	form.setAttribute("method","post");
	form.setAttribute("enctype","multipart/form-data");
	form.setAttribute("encoding","multipart/form-data");
	if(html_show_loading.length > 0){
		$m(id_element).innerHTML = html_show_loading;
	}
	form.submit();
}
