﻿// JavaScript Document

function autoFill(id, v){
	$(id).css({ color: "#C4C4C4" }).attr({ value: v }).focus(function(){
		if($(this).val()==v){
			//$(this).val("").css({ color: "#4F4F4F" });
			$(this).val(v).css({ color: "#4F4F4F" });
		}
	}).blur(function(){
		if($(this).val()==""){
			$(this).css({ color: "#C4C4C4" }).val(v);
		}
	});
}

$(function() {
	$('input.ms-long').css({backgroundColor:"#FFFFFF"});
	$('input.ms-long').focus(function(){
		$(this).css({backgroundColor:"#B9ECFF"});
	});
	$('input.ms-long').blur(function(){
		$(this).css({backgroundColor:"#FFFFFF"});
	});
});

function isValidEmail(email) {
	strSearch = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+$/
	if (strSearch.test(email)) return true;
	else return false;
}

function isNumeric(string) {
	if (string.search) {
		if (( string.search(/\D/) != -1)) return false;
	}
	return true;
}

function isPhone(string)
{
	var valid = "0123456789-+() ";

	for (var i=0; i < string.length; i++) {
		//put in temp variable each character, one at a time.
		temp = "" + string.substring(i, i+1);
		//check index of a phone character in the "valid" variable.
		// if temp contains a character which is not in "valid" variable,
		//then valid.indexOf(temp) will be -1, otherwise it may be 0.1.2.3.4.5.6.7.8 or 9
	
		if (valid.indexOf(temp) == "-1") {
			//alert("Invalid characters in your phone. Please try again.")
			return false;
		}
	}
	//if all conditions are passed, then return true
	return true
}

$(document).ready(function()
{
	

});

function submitInfo() {
	
	var name = $("input#ctl00_content_g_cb9b69fb_9ffb_4ba5_8b9d_6ee735c5e5ae_ff1_new_ctl00_ctl00_TextField").val();
	if (name == "" || name == "Full Name" || name == "Obligatory" || name.length < 3) {
		autoFill($("#ctl00_content_g_cb9b69fb_9ffb_4ba5_8b9d_6ee735c5e5ae_ff1_new_ctl00_ctl00_TextField"), "Full Name");
		$(this).css({Color:"#FF0000"});
		alert('Insert: Full Name!');
		var success = 'no';
		$("input#ctl00_content_g_cb9b69fb_9ffb_4ba5_8b9d_6ee735c5e5ae_ff1_new_ctl00_ctl00_TextField").focus();
		return false;
	}
	var email = $("input#ctl00_content_g_cb9b69fb_9ffb_4ba5_8b9d_6ee735c5e5ae_ff2_new_ctl00_ctl00_TextField").val();
	if (email == "" || email == "Your Email" || email == "Obligatory") {
		autoFill($("#ctl00_content_g_cb9b69fb_9ffb_4ba5_8b9d_6ee735c5e5ae_ff2_new_ctl00_ctl00_TextField"), "Your Email");
		$(this).css({Color:"#FF0000"});
		alert('Insert: E-mail!');
		var success = 'no';
		$("input#ctl00_content_g_cb9b69fb_9ffb_4ba5_8b9d_6ee735c5e5ae_ff2_new_ctl00_ctl00_TextField").focus();
		return false;
	}
	var email = $("input#ctl00_content_g_cb9b69fb_9ffb_4ba5_8b9d_6ee735c5e5ae_ff2_new_ctl00_ctl00_TextField").val();
	if (!isValidEmail(email)) {
		//autoFill($("#ctl00_content_g_cb9b69fb_9ffb_4ba5_8b9d_6ee735c5e5ae_ff2_new_ctl00_ctl00_TextField"), "Vaš pravilni e-poštni naslov");
		$(this).css({Color:"#FF0000"});
		alert('Insert real e-mail! E.g.: name@domain.com');
		var success = 'no';
		$("input#ctl00_content_g_cb9b69fb_9ffb_4ba5_8b9d_6ee735c5e5ae_ff2_new_ctl00_ctl00_TextField").focus();
		return false;
	}

	return true;
};

function confirmDelete() {
	var potrditev = confirm('Are you sure you want to delete this article?')
	if (potrditev) {
		return true
	}
	else {
		return false
	}
}


var Soap = {
    createEnvelope: function(action, ns, parameters)
    {
        var soap = '<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:Body>';
        soap += '<' + action + ' xmlns="' + ns + '">';
        soap += Soap.__parseParameters(parameters);
        soap += '</' + action + '></soap12:Body></soap12:Envelope>';
        return soap;
    },
    
    __parseParameters: function(parameters)
    {
        var params = "";
        if (typeof parameters == 'object')
        {
            // check if we were provided an array or an object
            if (typeof parameters.push == 'function')
            {
                for (var i = 0, length = parameters.length; i < length; i += 2)
                {
                    params += "<" + parameters[i] + ">" + parameters[i+1] + "</" + parameters[i] + ">";
                }
            }
            else
            {
                a$H(parameters).each(
                    function(pair)
                    {
                        params += "<" + pair.key + ">" + pair.value + "</" + pair.key + ">";
                    });
            }
        }

        return params;
    }
}


function saveMe()
{
	
	var title = $("input#title").val();
	var content = $("textarea#content").val();
	
	var test = "Hallo SOAP!";
	
    // create xml that will create a new list item
    //var batch = '<Batch OnError="Continue"><Method ID="1" Cmd="New"><Field Name="ID">New</Field><Field Name="Title">'+title+'</Field><Field Name="content"><![CDATA['+content+']]><Field Name="layout">1</Field></Field></Method></Batch>';
    var batch = '<Batch OnError="Continue"><Method ID="1" Cmd="New"><Field Name="ID">New</Field><Field Name="Title">'+test+'</Field></Method></Batch>';
    //var batch = '<Batch OnError="Continue"><Method ID="2" Cmd="Update"><Field Name="ID">1</Field><Field Name="Title">test</Field></Method></Batch>';

    // build parameter object
    var parameters = 
    {
        listName: "contents",
        updates: batch       // xml created earlier
    }
    
    // create soap envelope
    var soap = Soap.createEnvelope(
        "UpdateListItems",
        "http://schemas.microsoft.com/sharepoint/soap/",
        parameters);
    
	
	// get the base url for asmx - ajax.request
	var url = window.location.href;
	var urlin = url.substring(7);
	var urlsplit = urlin.split("/");
	var dolzina = urlsplit.length - 1;
	var naslov = new Array();
	for(i = 0; i < dolzina; i++){
		var naslov = naslov + urlsplit[i] + "/";
	}
	var asmx =  "http://" + naslov + "_vti_bin/lists.asmx";
	
	
    // call web service
    new Ajax.Request(
        asmx,
        {
            method: "post",
            contentType: "application/soap+xml",
            postBody: soap,
            onSuccess: function(transoprt) { alert("Success: " + transport.responseText); }, 
            onFailure: function(transport) { alert("Error: " + transport.responseText); } 
        });
}


function moveUp(x,i,p) {
	
	var newx = parseFloat(x);
	var previd = newx - 1;
	var prev = "idpos" + previd;
	var prevel = $("input#"+prev).val();
	//alert(prevel);
	var prevsplit = prevel.split("-");
	
	var curId = i;
	var prevId = prevsplit[0];
	var curPos = p;
	var prevPos = prevsplit[1];
	//alert(curId + " - " + curPos + " : " + nextId + " - " + nextPos);
	
	
	var batch = '<Batch OnError="Continue"><Method ID="2" Cmd="Update"><Field Name="ID">'+curId+'</Field><Field Name="position">'+prevPos+'</Field></Method><Method ID="2" Cmd="Update"><Field Name="ID">'+prevId+'</Field><Field Name="position">'+curPos+'</Field></Method></Batch>';

    // build parameter object
    var parameters = 
    {
        listName: "contents",
        updates: batch       // xml created earlier
    }
    
    // create soap envelope
    var soap = Soap.createEnvelope(
        "UpdateListItems",
        "http://schemas.microsoft.com/sharepoint/soap/",
        parameters);
    
	
	// get the base url for asmx - ajax.request
	var url = window.location.href;
	var urlin = url.substring(7);
	var urlsplit = urlin.split("/");
	var dolzina = urlsplit.length - 1;
	var naslov = new Array();
	for(i = 0; i < dolzina; i++){
		var naslov = naslov + urlsplit[i] + "/";
	}
	var asmx =  "http://" + naslov + "_vti_bin/lists.asmx";
	
	
    // call web service
    new Ajax.Request(
        asmx,
        {
            method: "post",
            contentType: "application/soap+xml",
            postBody: soap,
            onSuccess: function(transoprt) { alert("Success: " + transport.responseText); }, 
            onFailure: function(transport) { alert("Error: " + transport.responseText); } 
        });
	
	var mysite = "http://" + naslov + "default.aspx";
	
	//setTimeout($(location).attr('href',mysite),2000);
	//window.location.reload();
	//setTimeout(window.location.replace(mysite),5000);
	//window.location.replace(mysite);
		
	pause();
	
	
}


function moveDown(x,i,p) {
	
	var newx = parseFloat(x);
	var nextid = newx + 1;
	var next = "idpos" + nextid;
	var nextel = $("input#"+next).val();
	//alert(nextel);
	var nextsplit = nextel.split("-");
	
	var curId = i;
	var nextId = nextsplit[0];
	var curPos = p;
	var nextPos = nextsplit[1];
	//alert(curId + " - " + curPos + " : " + nextId + " - " + nextPos);
	
	
	var batch = '<Batch OnError="Continue"><Method ID="2" Cmd="Update"><Field Name="ID">'+curId+'</Field><Field Name="position">'+nextPos+'</Field></Method><Method ID="2" Cmd="Update"><Field Name="ID">'+nextId+'</Field><Field Name="position">'+curPos+'</Field></Method></Batch>';

    // build parameter object
    var parameters = 
    {
        listName: "contents",
        updates: batch       // xml created earlier
    }
    
    // create soap envelope
    var soap = Soap.createEnvelope(
        "UpdateListItems",
        "http://schemas.microsoft.com/sharepoint/soap/",
        parameters);
    
	
	// get the base url for asmx - ajax.request
	var url = window.location.href;
	var urlin = url.substring(7);
	var urlsplit = urlin.split("/");
	var dolzina = urlsplit.length - 1;
	var naslov = new Array();
	for(i = 0; i < dolzina; i++){
		var naslov = naslov + urlsplit[i] + "/";
	}
	var asmx =  "http://" + naslov + "_vti_bin/lists.asmx";
	
	
    // call web service
    new Ajax.Request(
        asmx,
        {
            method: "post",
            contentType: "application/soap+xml",
            postBody: soap,
            onSuccess: function(transoprt) { alert("Success: " + transport.responseText); }, 
            onFailure: function(transport) { alert("Error: " + transport.responseText); } 
        });
	
	var mysite = "http://" + naslov + "default.aspx";
	
	//setTimeout($(location).attr('href',mysite),2000);
	//window.location.reload();
	//setTimeout(window.location.replace(mysite),5000);
	//window.location.replace(mysite);
		
	pause();
	
	
}

function pause() {
	timer = setTimeout("redirect()",3000); // 3 secs
	return false;
}

function redirect() {
	var url = window.location.href;
	location.href = url;
	return false;
}


function shraniMe() {
	autoFill($("#g_e929ba13_6aa0_4e9d_8a84_bce9316c0fa5_ff1_new_ctl00_ctl00_TextField"), "Test: shraniMe");
	__doPostBack('g_e929ba13_6aa0_4e9d_8a84_bce9316c0fa5','__commit');
	
	var url = "http://b4c.balavec.net/tumblr/";
	$(location).attr('href',url);

}

function menuUp(x,i,p,v,t) {
	
	var newx = parseFloat(x);
	var previd = newx - 1;
	var prev = "idpos" + previd;
	var prevel = $("input#"+prev).val();
	//alert(prevel);
	var prevsplit = prevel.split("-");
	
	var curId = i;
	var prevId = prevsplit[0];
	var curPos = p;
	var prevPos = prevsplit[1];
	//alert(curId + " - " + curPos + " : " + nextId + " - " + nextPos);
	
	
	var batch = '<Batch OnError="Continue"><Method ID="2" Cmd="Update"><Field Name="ID">'+curId+'</Field><Field Name="position">'+prevPos+'</Field></Method><Method ID="2" Cmd="Update"><Field Name="ID">'+prevId+'</Field><Field Name="position">'+curPos+'</Field></Method></Batch>';

    // build parameter object
    var parameters = 
    {
        listName: "structure",
        updates: batch       // xml created earlier
    }
    
    // create soap envelope
    var soap = Soap.createEnvelope(
        "UpdateListItems",
        "http://schemas.microsoft.com/sharepoint/soap/",
        parameters);
    
	
	// get the base url for asmx - ajax.request
	var url = window.location.href;
	var urlin = url.substring(7);
	var urlsplit = urlin.split("/");
	var dolzina = urlsplit.length - 1;
	var naslov = new Array();
	for(i = 0; i < dolzina; i++){
		var naslov = naslov + urlsplit[i] + "/";
	}
	var asmx =  "http://" + naslov + "_vti_bin/lists.asmx";
	
	
    // call web service
    new Ajax.Request(
        asmx,
        {
            method: "post",
            contentType: "application/soap+xml",
            postBody: soap,
            onSuccess: function(transoprt) { alert("Success: " + transport.responseText); }, 
            onFailure: function(transport) { alert("Error: " + transport.responseText); } 
        });
	
	var mysite = "http://" + naslov + "default.aspx";
	
	//setTimeout($(location).attr('href',mysite),2000);
	//window.location.reload();
	//setTimeout(window.location.replace(mysite),5000);
	//window.location.replace(mysite);
		
	//pause();
	
	__doPostBack('ctl00$content$g_b6d1aa97_1d53_4738_a6d2_5e0ddd957d4a','__cancel;v={'+v+'};t={'+t+'}');
	
}


function menuDown(x,i,p,v,t) {
	
	var newx = parseFloat(x);
	var nextid = newx + 1;
	var next = "idpos" + nextid;
	var nextel = $("input#"+next).val();
	//alert(nextel);
	var nextsplit = nextel.split("-");
	
	var curId = i;
	var nextId = nextsplit[0];
	var curPos = p;
	var nextPos = nextsplit[1];
	//alert(curId + " - " + curPos + " : " + nextId + " - " + nextPos);
	
	
	var batch = '<Batch OnError="Continue"><Method ID="2" Cmd="Update"><Field Name="ID">'+curId+'</Field><Field Name="position">'+nextPos+'</Field></Method><Method ID="2" Cmd="Update"><Field Name="ID">'+nextId+'</Field><Field Name="position">'+curPos+'</Field></Method></Batch>';

    // build parameter object
    var parameters = 
    {
        listName: "structure",
        updates: batch       // xml created earlier
    }
    
    // create soap envelope
    var soap = Soap.createEnvelope(
        "UpdateListItems",
        "http://schemas.microsoft.com/sharepoint/soap/",
        parameters);
    
	
	// get the base url for asmx - ajax.request
	var url = window.location.href;
	var urlin = url.substring(7);
	var urlsplit = urlin.split("/");
	var dolzina = urlsplit.length - 1;
	var naslov = new Array();
	for(i = 0; i < dolzina; i++){
		var naslov = naslov + urlsplit[i] + "/";
	}
	var asmx =  "http://" + naslov + "_vti_bin/lists.asmx";
	
	
    // call web service
    new Ajax.Request(
        asmx,
        {
            method: "post",
            contentType: "application/soap+xml",
            postBody: soap,
            onSuccess: function(transoprt) { alert("Success: " + transport.responseText); }, 
            onFailure: function(transport) { alert("Error: " + transport.responseText); } 
        });
	
	var mysite = "http://" + naslov + "default.aspx";
	
	//setTimeout($(location).attr('href',mysite),2000);
	//window.location.reload();
	//setTimeout(window.location.replace(mysite),5000);
	//window.location.replace(mysite);
		
	pause();
	
	
}


