/////////////////////////////////////////////////////////////////////
//
// Cows common client scripting functions
//
/////////////////////////////////////////////////////////////////////

function  insertHTMLonEditorImage(imgsrc) {
    if (typeof(imgsrc) != "undefined" && imgsrc.length > 0)
        tinyMCE.execCommand("mceInsertContent", false, '<img src="' + imgsrc + '" border="1" />');
    return false;
}

function cows_submit(pformname, pnewaction, pnewmethod, pquestion) {
    var newaction = String(pnewaction);
    var newmethod = String(pnewmethod);
    var question  = String(pquestion);
    var theform = null;
    theform = window.document.getElementById(pformname);
    if (theform == null) return false;
    if ((question != "undefined") && (question != ""))
    	if (!window.confirm(question)) return false;
    if ((newaction != "undefined") && (newaction != "")) theform.action = newaction;
    if ((newmethod != "undefined") && (newmethod != "")) theform.method = newmethod;
    if (theform.onsubmit != null) theform.onsubmit();
    theform.submit();
    return true;
}

function cows_confirm(pquestion, purl) {
	if (window.confirm(pquestion)) window.location = purl;
	return false;
}

function cows_openchildwindow(pURL, pWindowName) {
	//scrollbars=no
	window.open(pURL, pWindowName, 'height=450, width=700, status=yes, toolbar=no, menubar=no, scrollbars=yes, location=no');
}

function cows_resizewindow(pwidth, pheight) {
    window.resizeTo(pwidth, pheight);
}

function cows_centerwindow() {
	var vleft = (window.screen.availWidth - window.document.body.offsetWidth) / 2;
	if (vleft <= 0) vleft = 0;
	var vtop = (window.screen.availHeight - window.document.body.offsetHeight) / 2
	if (vtop <= 0) vtop = 0;
    window.moveTo(vleft, vtop);
}

function cows_image_preview(purl) {
	cows_openchildwindow(purl, '_wimagepreview');
}

function cows_imagebank_open(pinputid) {
    var inputid = new String(pinputid);
	if (inputid == "undefined") inputid = "";
	cows_openchildwindow('_imagebank.php' + '?ve=' + inputid + '&vp=' + window.location.pathname, '_wimagebank');
	return false;
}

function cows_imagebank_writeonopener(ve, vp, pTXT) {
    var elpadreurl = new String(vp);
    var elelemento = new String(ve);
    var vReturn = true;
    var vDebug = 0;
    if ((window.opener != null) && (elpadreurl != "") && (elelemento != "")) {
        vReturn = false;
        vDebug = 1;
        if (!window.opener.closed) {
            vDebug = 2;
            if (elpadreurl == window.opener.location.pathname) {
	            vDebug = 3;
                var eltxt = window.opener.document.getElementById(elelemento);
                if (eltxt != null) {
                    eltxt.value = pTXT;
                    vReturn = true;
                }
                var eltxt = null;
            }
        }
    }
    window.close();
}

function cows_textarealength(pElement, pLength) {
    if (parseInt(pLength) < pElement.value.length) {
        pElement.value = strLeft(pElement.value, parseInt(pLength));
        return false;
    } else {
        return true;
    }
}

var _cows_banners_arr = new Array();

function cows_banners_insertarea(areaname) {
	returnstr = "";
	if (isArray(_cows_banners_arr)) {
		for (i = 0 ; i < _cows_banners_arr.length ; i++) {
			if (isArray(_cows_banners_arr[i]) && (_cows_banners_arr[i].length == 2) && (_cows_banners_arr[i][0] == areaname)) {
				area = _cows_banners_arr[i][1];
				if (isArray(area) && area.length > 0) {
					for (k = 0 ; k < area.length ; k++) {
						returnstr = returnstr + area[k];
					}
				}
				break;
			}
		}
	}
	if (returnstr != "") {
		document.write(returnstr);
	}
}


function isArray(obj) {
   return ((typeof(obj) == "object") && (obj != null) && (obj.constructor.toString().indexOf("Array") != -1)) ? true : false ;
}

function strLeft(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function strRight(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

