/*
	Written by Jonathan Snook, http://www.snook.ca/jonathan
	Add-ons by Robert Nyman, http://www.robertnyman.com
*/

/*
To get all a elements in the document with a “info-links” class.
    getElementsByClassName(document, "a", "info-links");
To get all div elements within the element named “container”, with a “col” class.
    getElementsByClassName(document.getElementById("container"), "div", "col");
To get all elements within in the document with a “click-me” class.
    getElementsByClassName(document, "*", "click-me");
*/

function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/-/g, "\-");
	var oRegExp = new RegExp("(^|\s)" + strClassName + "(\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function updateSessionForKeepingUserLoggedIn() {
    ajax = new sack('index.php?section=ajx_refresh_session');
//	ajax.element = ‘myexamplediv’;
	ajax.runAJAX();
}

function confirmFunc(url, confirmMsg) {
	if (confirm(confirmMsg)) {
		location.href = url;
	}
	return false;
}

function validatePriceFild(El) {
  fildValue = parseFloat(El.value);
  El.value = isNaN(fildValue) ? 0 : fildValue;
}

function trim(stringToTrim) { // malisa
	ret = stringToTrim.replace(/^\s+|\s+$/g, '');
	return ret.replace('\&amp;', '&');
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


function sendToFriend(turl){
	ajax = new sack();
	var sender_email = document.getElementById('sender_email').value;
	ajax.setVar('sender_email', sender_email);
	var sender_email = document.getElementById('friend_mail').value;
	ajax.setVar('friend_mail', sender_email);
	var sender_email = document.getElementById('custom_message').value;
	ajax.setVar('custom_message', sender_email);
	var address = document.getElementById('address').value;
	ajax.setVar('address', address);
	ajax.onLoading = function(){
			$('send_friend_button').disabled = true;
			$('send_friend_button').value = "Sending...";
	}
	ajax.requestFile = turl + '&subsection=send_mail';
	ajax.onCompletion = function() {
			$('sent_to_friend_form').innerHTML = ajax.response;
			$('send_friend_button').disabled = false;
			$('send_friend_button').value = "Send";
			
	}
	ajax.runAJAX();
};



function changeLanguage(lang){
    document.location.href=locationAddArgument('language', lang);
}

function locationAddArgument(key, value){
    var loc = pound = q = namedAnchor = args = '';
    if(document.location.href.indexOf('?')!=-1){
        q = document.location.href.indexOf('?');
        loc = document.location.href.substr(0, q);
    }else if(document.location.href.indexOf('#')!=-1){
        loc = document.location.href.substr(0, pound);
    }
    if(document.location.href.indexOf('#')!=-1 && document.location.href.indexOf('#')!=document.location.href.length-1){
        pound = document.location.href.indexOf('#');
        namedAnchor = document.location.href.substr(pound);
    }
    if(q!=''){
        var length = (pound!=''?pound-q:document.location.href.length);
        args = document.location.href.substr(q, length);
        if(args.indexOf(key+'=')!=-1){
            var start = args.indexOf(key+'=');
            var end = args.indexOf('&', start);
            args = args.substr(0, start) + key+'='+value + (end!=-1?args.substr(end):'');
        }else{
            args += '&'+key+'='+value;
        }
    }else{
        args = '?'+key+'='+value;
    }
    return loc + args + namedAnchor;
}


function checkRequested () {
	var validationErrors = new Array();
	var errorIndex = 0;
	validationErrors[0] = "Required fields are marked with *.";

	elem = $(".requested");
	cont = 0;
	for(i=0; i<elem.length; i++) {
		
		str = elem[i].value;
		str = str.replace(/\s+/g," ");
		if(elem[i].tagName == 'TEXTAREA') {
			normalClass = 'forms_textarea_bg';
			errorClass = 'forms_textarea_bg textarea_not_valid'
		} else {
			normalClass = 'forms_input_bg';
			errorClass = 'forms_input_bg input_not_valid'
		}
		if(elem[i].value == '' || str == ' ') {
			cont++;
			elem[i].style.borderColor = '#ff0000';
			elem[i].parentNode.className = errorClass;
		} else {
			if(elem[i].name == 'email') {
				//if(myString.match(/^[a-z -]+$/i)) {
				myString = elem[i].value;
				if(myString.match(/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/)) {
					elem[i].style.borderColor = '#999999';
					elem[i].parentNode.className = normalClass;
				} else {
					errorIndex ++;
					validationErrors[errorIndex] = "\n Email format is not good.";
					elem[i].style.borderColor = '#ff0000';
					elem[i].parentNode.className = errorClass;
					cont++;
				}
				
			} else {
				elem[i].style.borderColor = '#999999';
				elem[i].parentNode.className = normalClass;
			}
		}
	}
	if(cont > 0) {
		err = "";
		for (i=0;i<validationErrors.length;i++) {
			//alert(validationErrors[i]);
			err = err + " " + validationErrors[i];
		}
		alert(err);
	} else {
		
		sbmelement = document.getElementById("sbm");
		sbmelement.disabled = false;
		sbmelement.click();
	}
}